Tony
Tony

Reputation: 2748

How do I pass listView data to another activity?

I have a listView in Activity A, where all the data are retrieved from SQLite. Now I want to pass the data to another new activity when list is clicked. How can I achieve this ?

Activity A

enter image description here

There are two list in Activity A, assume first list is clicked, I want it pass data to B.

  listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding listview_item_row in the result set

                Intent intent=new Intent(getActivity(),B.class);
                startActivity(intent);


            }
        });

Some part of my Activity B

public class Edit_Details extends AppCompatActivity {

    EditText Description,TimeIn,TimeOut;
    String description;
    SeekBar seekBar;
    TextView progressText;
    int progress=0;
    SQLiteDatabase database;
    MyDatabaseHelper dbHelper;
    Cursor cursor;
    private com.example.project.myapplication.API.WorkDetailsAPI WD;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_details);
        Project=(Spinner)findViewById(R.id.SpinnerProject);
        final String ID=getIntent().getStringExtra("ID"); // should be position?
        WD = new com.example.project.myapplication.API.WorkDetailsAPI(getApplication());
        Description=(EditText)findViewById(R.id.editTextWorkDescription);
        TimeIn=(EditText)findViewById(R.id.TimeIn);
        TimeOut=(EditText)findViewById(R.id.TimeOut);
        seekBar=(SeekBar)findViewById(R.id.seekBarPercentage);
        progressText=(TextView)findViewById(R.id.textProgress);
        progressText.setText("Covered:" + "" + seekBar.getProgress() + "/" + seekBar.getMax());
        Log.e("ID", ID);
        RetrieveDetails(ID); // how to get the position ?

    }

    public void RetrieveDetails(long ID)
    {
        final long id=ID;
        database=dbHelper.getWritableDatabase();
        cursor=database.rawQuery("SELECT SubContractors, NumberOfPerson, NumberOfHours FROM " + MyDatabaseHelper.TABLE_WORKFORCE+ " WHERE _id= ? ",
                new String[]{String.valueOf(id)}, null);

        Details d=new Details();
        if(cursor!=null) {
            while (cursor.moveToNext())
            {
                description=cursor.getString(cursor.getColumnIndexOrThrow(MyDatabaseHelper.WorkDescription));
                d.setWorkDescription(description); 
                Description.setText(description);  // display learn java on editText

            }
        }
    }
}

I need to know how to get the new activity to remember the list item that was clicked and then pull all the info from A and display in new activity. if that makes sense.. thanks!

Eg. Remember lean java and display on B

Upvotes: 0

Views: 7547

Answers (1)

Suhas
Suhas

Reputation: 1471

In Activity A, add data to intent like this

Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);

In Activity B, retrieve data like this

String data = getIntent().getExtras().getString("keyName");

You can add multiple key-value pairs.

EDIT:

If you have an Object say Data which holds values of description, progress,..., you could get it using below code inside onItemClick() of listViewUpdate.setOnItemClickListener(...).

Data data = (Data) listView.getAdapter().getItem(position);

and pass the whole object in the intent.

If you are not familiar as to how we can pass an object in intent, this SO post might help you.

Upvotes: 4

Related Questions