jeudi 22 octobre 2015

how to show list view record from sqlite into second listview on setOnItemClickListener

I have created list view, what I want to do is that when user clicks on first list view the selected record For that Particular Field should show in second list view,

in my code I Just get the item position of the first list view and i pass Selected item of that list view into my second Activity i dont know what i do next

so please give me the code/idea how to do this as I am new in android..

Here is My Code

MainActivity:(First List View)

    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)
    }
});

Second Activity:(Second Listview)

    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);

Database CLass:

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