0

我是 Android(和 Java)编程的新手,并且已经设法让自定义适配器工作,但我坚持如何更新其中的 textViews。下面你会看到我的 score_save 函数和我想要的代码,但我不确定如何一次将它应用到我的适配器的所有行。

public class MainActivity extends Activity {

private ListView listView1;
private int currentPlayer;
private int currentScore;
ArrayList<Integer> allScoreChange = new ArrayList<Integer>();
ArrayList<Integer> allScore = new ArrayList<Integer>();

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 < 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<?> 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("");

    }

}

这是该行的 XML,我想使用 allScoreChange 和 allScore ArrayLists 中的值更新 txtScoreChange 和 txtScore。我删掉了定义这些列表的代码,以防你想知道。

<?xml version="1.0" encoding="utf-8"?>
<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" >

<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" />

<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" />

<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" />

这是我的适配器:

public class ScoreAdapter extends ArrayAdapter<Score>{

    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;
    }
}
4

1 回答 1

1

调用 adapter.notifyDataSetChanged()。这将导致它重新填充列表视图,在所有可见视图上调用 adapter.getView。让 getView() 为其行设置正确的值。

于 2013-01-13T01:12:35.953 回答