[英]Bug - ListView -> item -> button OnClick : Change background color of parent
I try to change background color of a button in item of my listView, but when I try to change the background, the background change each 9 items. 我尝试更改listView项中按钮的背景颜色,但是当我尝试更改背景时,背景会更改每9个项目。 When I try to change the orientation of my phone, the background change each 5 items... 当我尝试更改手机的方向时,背景每5个项目都会更改一次...
--- IMAGE TO UNDERSTAND: --- 了解图片:
http://image.noelshack.com/fichiers/2014/09/1393436440-problem.png http://image.noelshack.com/fichiers/2014/09/1393436440-problem.png
I don't understand, is so strange. 我不明白,真是奇怪。
I have an Adapter created. 我创建了一个适配器。
Java.java (Not my adapter file) 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 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 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;
}
}
I don't understand what the problem is :/ 我不明白问题是什么:/
Thanks, 谢谢,
To improve performance, ListView uses old views to inflate new ones when you scroll, that's why you see a repeated action in other ones. 为了提高性能,ListView使用旧的视图在滚动时将新的视图充气,这就是为什么您在其他视图中看到重复的动作的原因。
To fix your problem, i recommand you to set a boolean variable as Tag for the current item button. 为了解决您的问题,我建议您为当前项目按钮设置一个布尔变量作为Tag。
Given that, your row item contains (firstName, lastname), add a new attribut Button like that. 鉴于此,您的行项目包含(名字,姓氏),请添加一个新的属性按钮。
static class WeatherHolder
{
TextView firstName;
TextView lastName;
Button button
}
init it at your GetView as you do for other items, and then when you retreive Student details, Check if the button has a tag equals to True (=> that means already clicked) And in your clickPresent method just set a True tag when you click and False when you unclick. 像在其他项目上一样在GetView上初始化它,然后在获取学生详细信息时,检查按钮是否具有等于True的标记(=>表示已单击),并且在clickPresent方法中,当您设置单击,然后单击False。
NB: if tag equals false reset the color. 注意:如果标签等于假,请重置颜色。
public void clickPresent(View v)
{
v.setBackgroundColor(Color.BLUE);
v.setTag(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.