简体   繁体   中英

Passing data from ListView to other Activity

I have a ListView where each ListItem has 2 TextViews in it's layout. I'd like to pass the values of those TextViews to another activity when the ListItem is clicked. All my attempts have failed, so i ask you how can i do that? How can i pass the SQLite ROW_ID, the values of R.id.text1 and R.id.text2?

    Cursor cursor = database.getAllNames();
    adapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor,
            new String[] { Database.KEY_DATE , Database.KEY_NAME },
            new int[] {R.id.text1, R.id.text2}, 0);

    listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(SendingData.this, ReceivingData.class);
            intent.putExtra("id", id); //this should pass the SQLite ROW_ID
            intent.putExtra("date", date); //this should pass the value of R.id.text1
            intent.putExtra("name", name); //this should pass the value of R.id.text2
            startActivity(intent);  
        }
        });

Use this to retrieve the data from the SQLLite database.

cursor.getString(c.getColumnIndex(Database.columns[i])) // i is the index of the column whose details you want to fetch

And then you can pass them as parameters in intent.putExtra.

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cursor = null;
            cursor = (Cursor) parent.getItemAtPosition(position);
            Intent intent = new Intent(SendingData.this, ReceivingData.class);
            intent.putExtra("id", cursor.getInt(cursor.getColumnIndex(Database.columns[0]))); //assuming that the first column in your database table is the row_id
            intent.putExtra("date", cursor.getString(cursor.getColumnIndex(Database.columns[1]))); //assuming that the second column in your database table is the text1
            intent.putExtra("name", cursor.getString(cursor.getColumnIndex(Database.columns[2]))); //assuming that the third column in your database table is the text2
            startActivity(intent);  
        }
        });

You can do it by taking the data directly from the view without having to access the cursor (and using the row id passed in the onItemClick method as you are already doing).:

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

        TextView text1 = (TextView) view.findViewById(R.id.text1);
        TextView text2 = (TextView) view.findViewById(R.id.text1);
        String date = text1.getText().tostring();
        String name = text2.getText().tostring();

        Intent intent = new Intent(SendingData.this, ReceivingData.class); 
        intent.putExtra("id", id); //this should pass the SQLite ROW_ID 
        intent.putExtra("date", date); //this should pass the value of R.id.text1 
        intent.putExtra("name", name); //this should pass the value of R.id.text2 
        startActivity(intent);   
    } 
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM