Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TextView values from a listView Items?

Tags:

android

I tried to get answer from different similar questions here but didn't get any answer. Actually I am trying to get values from multiple textViews of any listview item(s). Here is the code
ListView listView = getListView();

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {               
        String contactId = ((TextView) findViewById(R.id.cid)).getText().toString();
        String contactName= ((TextView) findViewById(R.id.contactName)).getText().toString();
        String contactNo= ((TextView) findViewById(R.id.contactNo)).getText().toString();
        Intent i = new Intent(getApplicationContext(), editContactActivity.class);
        i.putExtra(TAG_ID, contactId );
        i.putExtra(TAG_NAME, contactName);
        i.putExtra(TAG_CONTACT_NO, contactNo);
        startActivityForResult(i, 100);     
    }   
}); 

What result i am getting, I am getting textviews value of first item(Position) in listview, either i am clicking any item but i am getting textviews value of first item(Position) in listview, Here is the code how i am getting values on other activity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_contact);

    btnSave = (Button) findViewById(R.id.btnSaveContact);
    btnDelete = (Button) findViewById(R.id.btnDeleteContact);

    Intent i = getIntent();
    cId = i.getStringExtra(TAG_CONTACT_ID);
    cNameTxt = i.getStringExtra(TAG_CONTACT_NAME);
    cNumberTxt = i.getStringExtra(TAG_CONTACT_NUMBER);

    //Log.d("Value of contact name", cNameTxt);
    //Log.d("Value of contact name", cNumberTxt);


    TextView contactIdTxtView = (TextView) findViewById(R.id.cid);
    cName = (EditText) findViewById(R.id.editCName);
    cNumber = (EditText) findViewById(R.id.editCNumber);


    cName.setText(cId);
    cNumber.setText(cNumberTxt);

}

I will be very thankful if anyone will help in detail Thanks Regard Akmal Rasool

like image 662
Akmal Rasool Avatar asked Feb 12 '13 18:02

Akmal Rasool


1 Answers

I am getting textviews value of first item(Position) in listview

Every time you use findViewById() tell it to look in this row only:

(TextView) view.findViewById(R.id.cid)
//Add this ^^^^^  

All together:

@Override
public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {
    String contactId = ((TextView) view.findViewById(R.id.cid)).getText().toString();
    //             Again use view  ^^^^^
    // Repeat this for contactName and contactNo
like image 54
Sam Avatar answered Nov 03 '22 18:11

Sam