9

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

Akash Agarwal
  • 2,326
  • 1
  • 27
  • 57
Akmal Rasool
  • 496
  • 2
  • 7
  • 17

3 Answers3

39

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
Sam
  • 86,580
  • 20
  • 181
  • 179
0

to obtain the textView value, try this.

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView text = (TextView)view.findViewById(R.id.correoEncuestador);
            String message= text.getText().toString();
            Toast.makeText(getApplication(), message,  Toast.LENGTH_LONG).show();
        }
    }
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
-2
View parentView = (View) view.getParent();
    textview1 = ((TextView) parentView
            .findViewById(R.id.textview1)).getText().toString();

    textview2 = ((TextView) parentView
            .findViewById(R.id.textview2)).getText().toString();

    textview3 = ((TextView) parentView
            .findViewById(R.id.textview3)).getText().toString();