Note that there are some explanatory texts on larger screens.

plurals
  1. POsetText() of TextView in list_row.xml
    primarykey
    data
    text
    <p>I have:</p> <ol> <li>One <code>MyListActivity class extends ListActivity</code> <code>setContentView(R.layout.mylist)</code>;</li> <li>Two xml files: <code>mylist.xml</code> and <code>list_row.xml</code>. </li> <li><code>mylist.xml</code> is the layout and <code>list_row.xml</code> is how each row looks like. </li> <li><code>list_row.xml</code> contains 3 <code>TextViews(t1,t2,t3)</code>. </li> </ol> <p>I want to change some text of t2 in <code>MyListActivity</code> class. Because the content view is mylist.xml so I can't simply use <code>findViewById</code>. So I used <code>LayoutInflater</code>.</p> <pre><code> LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.list_row, null); textView2 = (TextView) v.findViewById(R.id.t2); textView2.setText("ccccc"); </code></pre> <p>The problem is that the text of t2 is never changed. I have tried many times and the text remains the text I set in the <code>list_row.xml</code>. I can't figure out why.. Could someone help please. Thanks!</p> <p>=====<strong>Solution</strong>:=====</p> <p>Create my own <code>SimpleCursorAdapter</code> class and override the <code>getView()</code> method.</p> <pre><code>private class MySimpleCursorAdapter extends SimpleCursorAdapter { Cursor c; public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.c = c; } @Override public View getView(int position, View convertView, ViewGroup parent) { super.getView(position, convertView, parent); LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_row, parent, false); TextView textview1 = (TextView) rowView.findViewById(R.id.text1); textView1.setText(c.getString(1));// whatever should be in textView1 textView2 = (TextView) rowView.findViewById(R.id.text2); textView2.setText("ccccc"); return rowView; } } </code></pre> <p>The <code>super.getView(position, convertView, parent);</code> is quite important because if I don't have that line, <code>textView1</code> will always show the value of first row(e.g. if <code>textView1</code> shows the id, then this is always 1). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload