I want to change the text of the list item (ListView). The element is formed dynamically in the adapter.

public View getView(int position, View convertView, ViewGroup parent) { textView = new TextView(context); textView.setText(Title[position]); textView.setTextSize(30); return textView; } 

In the handler I want to change its text when I click on an item

Trying to do so but failing

 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view.findFocus()).setText("asdasdasda"); } 

Please tell me how you can solve this problem. Thank you in advance.

  • Returning a new view to the getView adapter each time is very, very bad. Use the convertView parameter. - falstaf

2 answers 2

You need to set the setOnItemClickListener () handler:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) { TextView txtView = (TextView)view; txtView.setText("blah blah blah"); } } 

    List of which view consists of? If stupid TextView, then the same as you, only without .findFocus (), if a custom view - implement the method of changing the text.