0

On item selection I am able to show the value of a column through toast. Now I want that value to get assigned to a variable. I tried with the commented line but get some junk information like:

android.widget.TextView{41f04c60 V.ED..}.

CODE:

 public void onItemClick(AdapterView<?> parent, 
                          View view, int position, long thislist) {
        TextView selectedTitle = (TextView)view.findViewById(R.id.textView);
        Toast.makeText(this, selectedTitle.getText() , Toast.LENGTH_LONG).show();
        //selectedTitleID = String.valueOf((TextView) 
                                              view.findViewById(R.id.textView));

        }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
user3144078
  • 145
  • 2
  • 9

4 Answers4

0

Your selected item will be:

Object selectedItem = parent.getAdapter().getItem(position);

Now You can cast this item to any class and get selectedTitleId;

Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
0
 TextView selectedTitle = (TextView)view.findViewById(R.id.textView);
 String selectedTitleID  = selectedTitle.getText().toString();
Alpha
  • 1,754
  • 3
  • 21
  • 39
0

I think you are trying to get from adapter.You need to mention like this.

   @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
    String title= (String) ((TextView) view
            .findViewById(R.id.textView)).getText(); //from adapter
    Toast.makeText(this,"Title is"+ title , Toast.LENGTH_LONG).show();

    }
} 

}

Shadow
  • 6,864
  • 6
  • 44
  • 93
0

Just do one thing in your itemclick

String str = parent.getItemAtPosition(position).toString();

You can also find your answer in :

android listview get selected item

Community
  • 1
  • 1
Darsh Patel
  • 1,015
  • 9
  • 15