mardi 20 octobre 2015

How To set the fetched data in listview adapter of SubcategoryActivity class

I Have a Two Listview and i load datas from sqlite..In One Listview I have Title Column names and In other Listview i want to display Subtitle Column names

In title listview i use setOnItemClickListener and get the selectedItem and the selctionItemPosition and forward it to next activity using Intent

Now i want to add the fetched data using selectedData inside Oncreate() method and i dont know how to do it

Here is My Code:

MainActivity:

listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
    dbHelper.openDataBase();
} catch (SQLException e) {
    e.printStackTrace();
}

sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.gettitles(sqLiteDatabase);

String[] from = new String[] { dbHelper.TITLE };
int[] to = new int[] {R.id.title };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.title_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

         String selectedItem =(String) (listView.getItemAtPosition(position));

int selctionItemPosition=position;

Intent intent = new Intent(MainActivity.this, SubtitleActivity.class); 
 intent.putExtra("selectedItem", selectedItem);
  startActivity(intent)
    }
});

}

SubTitleActivity:

listView2= (ListView) findViewById(R.id.listView2);

 String selectedData = intent.getStringExtra("selectedItem");

dbHelper = new SqlLiteDbHelper(this);
try {
    dbHelper.openDataBase();
} catch (SQLException e) {
    e.printStackTrace();
}

sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase,sub);

String[] from = new String[] { dbHelper.SUBCATEGORY };
int[] to = new int[] {R.id.subcategory };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.subcategory_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView2.setAdapter(adapter);

DatabaseClass:

 public Cursor gettitles(SQLiteDatabase db)
    {
        db = this.getReadableDatabase();
        Cursor cursor;
        cursor = db.query(true, "record", new String[]{TITLE,ITEMNO + " as _id"}, null, null, TITLE, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

     public Cursor getsubcategory(SQLiteDatabase db,String sub)
    {
        db=this.getReadableDatabase();
        Cursor cursor;
        cursor = db.query("record",new String[]{SUBCATEGORY},TITLE + "=" +sub,null,null,null,null);
        return cursor;
    }

Aucun commentaire:

Enregistrer un commentaire