Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Stuck on updating textView in custom adapter
    primarykey
    data
    text
    <p>I'm new to programming for Android (and Java) and have managed to get a custom adapter working, but am stuck on how to update textViews inside of it. Below you will see my score_save function with the code I want, but I'm not sure how to apply this to all rows of my adapter at once.</p> <pre><code>public class MainActivity extends Activity { private ListView listView1; private int currentPlayer; private int currentScore; ArrayList&lt;Integer&gt; allScoreChange = new ArrayList&lt;Integer&gt;(); ArrayList&lt;Integer&gt; allScore = new ArrayList&lt;Integer&gt;(); Button button_add, button_add_big, button_sub, button_sub_big, button_add_num, button_save; TextView name; TextView score; TextView scoreChange; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Score score_data[] = new Score[] { new Score("NameA"), new Score("NameB"), new Score("NameC"), new Score("NameD"), new Score("NameE") }; //sets score lists to 0 for (int i = 0; i &lt; 5; i++) { allScoreChange.add(0); allScore.add(0); } ScoreAdapter adapter = new ScoreAdapter(this, R.layout.listview_item_row, score_data); listView1 = (ListView) findViewById(R.id.listView1); listView1.setAdapter(adapter); listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { currentScore = allScoreChange.get(position); // Gets fields from current row score = (TextView) view.findViewById(R.id.txtScore); scoreChange = (TextView) view.findViewById(R.id.txtScoreChange); currentPlayer = position; } }); button_save.setOnClickListener(new OnClickListener() { public void onClick(View v) { score_save(); } }); } public void score_save() { // this needs to go through all rows of listview currentPlayer = 0; // update array with new score allScore.set(currentPlayer, allScore.get(currentPlayer) + allScoreChange.get(currentPlayer)); // display new score score.setText(allScore.get(currentPlayer)); // reset score change to zero allScoreChange.set(currentPlayer, 0); // display score change as blank scoreChange.setText(""); } } </code></pre> <p>Here's the XML for the row, I want to update txtScoreChange and txtScore with the values out of the allScoreChange and allScore ArrayLists. I cut out the code that defines these lists, in case you're wondering.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp" &gt; &lt;TextView android:id="@+id/txtTitle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:layout_weight="70" android:text="Name" android:textColor="#000000" android:textSize="22sp" android:textStyle="bold" /&gt; &lt;TextView android:id="@+id/txtScoreChange" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_weight="15" android:textColor="#ff0000" android:textSize="22sp" android:gravity="right" /&gt; &lt;TextView android:id="@+id/txtScore" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_weight="15" android:text="1" android:textColor="#666666" android:textSize="22sp" android:gravity="right" /&gt; </code></pre> <p></p> <p>Here's my adapter:</p> <pre><code>public class ScoreAdapter extends ArrayAdapter&lt;Score&gt;{ Context context; int layoutResourceId; Score data[] = null; public ScoreAdapter(Context context, int layoutResourceId, Score[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; ScoreHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ScoreHolder(); holder.txtScore = (TextView)row.findViewById(R.id.txtScore); holder.txtScoreChange = (TextView)row.findViewById(R.id.txtScoreChange); holder.txtName = (TextView)row.findViewById(R.id.txtName); row.setTag(holder); } else { holder = (ScoreHolder)row.getTag(); } Score scoreData = data[position]; holder.txtName.setText(scoreData.name); holder.txtScore.setText(scoreData.score); return row; } static class ScoreHolder { TextView txtScore; TextView txtName; TextView txtScoreChange; } } </code></pre>
    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.
 

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