lundi 9 mai 2016

Using multiple cursor OR using same cursor for multiple queries

I want to fetch data from multiple table in my activity having 2 Listviews and some EditTexts.

I want to fetch data in EditText from Table 1 And fetch data in ListView1 from Table 2 and data from Table 3 into ListView2.

Problem is : i can fetch data through cursor from table 1. But I can't fetch data from table2 using same cursor OR new Cursor.

Table have data but Cursor.MoveToFirst returns false.

public class Details extends AppCompatActivity {

TextView name,num,cty,det;
Button btn_debit,btn_credit;
SQLiteDatabase db;
Cursor c;
int id;
ListView dbList,crList;
private ArrayList<HashMap<String,String>> arrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    id = Integer.parseInt(getIntent().getExtras().getString("name"));

    name = (TextView)findViewById(R.id.textView6);
    num = (TextView)findViewById(R.id.textView7);
    cty = (TextView)findViewById(R.id.textView8);
    det = (TextView)findViewById(R.id.textView9);

    btn_debit = (Button)findViewById(R.id.jama);
    btn_credit = (Button)findViewById(R.id.udhar);

    dbList = (ListView)findViewById(R.id.ListDebit);
    crList = (ListView)findViewById(R.id.ListCredit);

    arrayList = new ArrayList<HashMap<String, String>>();

    // tv = (TextView)findViewById(R.id.textView6);
    //tv.setText(getIntent().getExtras().getString("name"));
    db = openOrCreateDatabase("AccountsDB", Context.MODE_PRIVATE,null);

    c=db.rawQuery("SELECT c_name , c_mno , c_detail , c_city FROM customers where c_id="+id,null);
    try {
        if (c!=null){
            if (c.moveToFirst()){
                name.setText(c.getString(0));
                num.setText(c.getString(1));
                cty.setText(c.getString(3));
                det.setText(c.getString(2));
            }
        }
    }catch (Exception e){

    }finally {
        c.close();
    }

    Cursor cursor = db.rawQuery("SELECT * FROM debit_master",null);
    try{
        if (cursor!=null){
            if (cursor.moveToFirst()){
                Map<String,String> tem  = new HashMap<String ,String>();
                tem.clear();
                arrayList.clear();
                dbList.setAdapter(null);
                int cnt = cursor.getCount();
                Toast.makeText(getApplicationContext(),""+cnt,Toast.LENGTH_SHORT).show();
                do {

                    tem = new HashMap<String,String>();
                    tem.clear();

                    tem.put(FIRST_COLUMN, cursor.getString(0));
                    tem.put(SECOND_COLUMN,cursor.getString(1));
                    tem.put(THIRD_COLUMN,cursor.getString(2));
                    arrayList.add((HashMap<String, String>) tem);
                }while (cursor.moveToNext());
            }
        }

    }catch (Exception ex){
        Toast.makeText(getApplicationContext(),""+ex,Toast.LENGTH_LONG).show();
    }


    ListViewAdapter adapter = new ListViewAdapter(this,arrayList);
    dbList.setAdapter(adapter);


    btn_debit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Details.this,Debit.class);
            intent.putExtra("cid",id);
            Details.this.startActivity(intent);
        }
    });

}

}

Aucun commentaire:

Enregistrer un commentaire