2013-01-13 65 views
0

我是新來的Android編程(和Java),並設法得到一個自定義適配器的工作,但堅持如何更新其中的textViews。下面你會看到我的score_save函數和我想要的代碼,但我不知道如何一次性將它應用到我的適配器的所有行。Android:卡在更新自定義適配器中的textView

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

    } 

} 

下面是行,我想更新txtScoreChange和txtScore的價值觀出了allScoreChange和allScore的ArrayList的XML。我刪除了定義這些列表的代碼,以防你想知道。

<?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; 
    } 
} 
+0

我看到的只是一堆代碼,沒有問題。 – syb0rg

回答

1

呼叫adapter.notifyDataSetChanged()。這將導致它重新填充列表視圖,在所有可見視圖上調用adapter.getView。讓getView()爲其行設置正確的值。

+0

如果我使用listView1.notifyDataSetChanged();它告訴我「方法notifyDataSetChanged()未定義類型ListView」 – GrilledCheese

+1

@MichaelG,這是因爲'notifyDataSetChanged()'是適配器的方法,而不是ListView。 –

+0

謝謝,那是我的問題......太多的「分數」和「適配器」在周圍浮動。 – GrilledCheese

相關問題