错误 - ListView控件 - >项目 - >按钮的OnClick:母公司更改背景颜色 [英] Bug - ListView -> item -> button OnClick : Change background color of parent

查看:79
本文介绍了错误 - ListView控件 - >项目 - >按钮的OnClick:母公司更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变一个按钮的背景颜色在我的ListView的项目,但是当我试图改变背景,背景改变各9个项目。
当我试图改变我的手机的方向,背景改变各5个项目...

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. When I try to change the orientation of my phone, the background change each 5 items...

--- 图片就明白了:结果
http://image.noelshack.com/fichiers/2014/09/1393436440 -problem.png

我不明白,太奇怪了。

我有一个适配器创建的。

I have an Adapter created.

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

Java.java (Not my adapter file)

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

}

我不明白的问题是什么:/

I don't understand what the problem is :/

谢谢,

推荐答案

要提高性能,使用的ListView旧观点,当你滚动膨胀换新的,这就是为什么你看到其他的重复动作。

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.

要解决您的问题,我建议您设置一个布尔变量作为标签当前项目按钮。

To fix your problem, i recommand you to set a boolean variable as Tag for the current item button.

鉴于这种情况,你的行项目包含(名字,姓氏),添加一个新的attribut按钮这样的。

Given that, your row item contains (firstName, lastname), add a new attribut Button like that.

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

初​​始化它在您GetView您为其他项目做,然后当你retreive学生的详细信息,检查按钮有一个标签等于真(=那意味着已经点击>)
而在你点击present方法只设置一个真正的标签,当你点击和False,当你取消勾选。

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.

注意:如果标记等于false重设色

NB: if tag equals false reset the color.

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

}

这篇关于错误 - ListView控件 - &GT;项目 - &GT;按钮的OnClick:母公司更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆