0

我嘗試更改listView項目中按鈕的背景顏色,但是當我嘗試更改背景時,背景會更改每個9項。 當我試圖改變我的電話的朝向,背景改變各5項...Bug - ListView - > item - > button OnClick:更改父項的背景顏色

--- IMAGE明白:
http://image.noelshack.com/fichiers/2014/09/1393436440-problem.png

我不明白,就是這麼奇怪。

我有一個適配器創建。

Java.java(不是我的適配器文件)

public void clickPresent(View v) 
{ 
    v.setBackgroundColor(Color.BLUE); 
} 
public void drawStudentsInListView() 
{ 
    for (int i = 0; i < this.listStudents.size(); i++) 
    { 
     Log.e("STUDENT", this.listStudents.get(i)._firstName); 
    } 
    if (listStudents.size() > 0) 
    { 
     Student[] weather_data; 
     weather_data = new Student[listStudents.size()]; 

     for (int i = 0; i < listStudents.size(); i++) 
     { 
      weather_data[i] = new Student(listStudents.get(i)._firstName, listStudents.get(i)._lastName); 
      Log.e("Count nbr student: ", "i = " + i); 
     } 

     WeatherAdapter adapter = new WeatherAdapter(this, R.layout.listview_item_row, weather_data); 

     listView1 = (ListView)findViewById(R.id.listView1); 
     listView1.setAdapter(adapter); 
    } 
} 

listview_item_row.xml

<Button 
     android:layout_width="100dp" 
     android:layout_height="30dp" 
     android:background="#009857" 
     android:layout_marginLeft="10dp" 
     android:textColor="#ffffff" 
     android:text="OK" 
     android:id="@+id/buttonPresent" 
     android:onClick="clickPresent" /> 

Adapter.java

public class WeatherAdapter extends ArrayAdapter<Student> 
{ 
    Context context; 
    int layoutResourceId; 
    Student data[] = null; 

    public WeatherAdapter(Context context, int layoutResourceId, Student[] 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; 
     WeatherHolder holder = null; 

     if (row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 
      holder = new WeatherHolder(); 
      holder.firstName = (TextView)row.findViewById(R.id.textFirstName); 
      holder.lastName = (TextView)row.findViewById(R.id.textLastName); 
      row.setTag(holder); 
     } 
     else 
     { 
      holder = (WeatherHolder)row.getTag(); 
     } 

     Student weather = data[position]; 
     holder.firstName.setText(weather._firstName); 
     holder.lastName.setText(weather._lastName); 

     return row; 
    } 

    static class WeatherHolder 
    { 
     TextView firstName; 
     TextView lastName; 
    } 

} 

我不明白是什麼問題:/

感謝,

+0

這不是您發佈問題的方式。重新張貼正確的一點點「代碼」 –

+0

更好? :/對不起.. – Johnny

+0

你有每個listview項目的按鈕嗎? – OWZY

回答

1

爲了提高性能,ListView控件使用舊觀點,當你滾動膨脹換新的,這就是爲什麼你看到其他的重複動作。

要解決您的問題,我建議您爲當前項目按鈕設置一個布爾變量作爲標籤。

鑑於此,您的行項目包含(firstName,lastname),添加一個新的屬性按鈕。

static class WeatherHolder 
    { 
     TextView firstName; 
     TextView lastName; 
     Button button 
    } 

初始化它在你的GetView,你的其他項目做,然後當你retreive學生的詳細信息,檢查按鈕有一個標籤等於真(這意味着已經點擊=>) 而在你的clickPresent方法只需在點擊時設置一個True標籤,在您點擊時設置False。

注意:如果標籤等於false,則重置顏色。

public void clickPresent(View v) 
{ 
    v.setBackgroundColor(Color.BLUE); 
    v.setTag(true); 

} 
+0

「爲了提高性能,ListView使用舊視圖在滾動時添加新視圖,這就是爲什麼您會在其他視圖中看到重複動作的原因。」 非常有趣! 非常感謝,我要去試試:) – Johnny