【问题标题】:Android Eclipse - Selected Item on Listview looses his Backgroundcolor when update ListviewAndroid Eclipse - Listview 上的选定项目在更新 Listview 时失去了背景颜色
【发布时间】:2014-07-09 10:46:52
【问题描述】:

经过几天的尝试和错误,我不得不向您寻求帮助。

我有一个带有 Listview 和一个 Button 的 Activity。当我在 Listview 上选择一个项目时,颜色会发生变化。一切顺利。当我按下按钮选择的项目来更新他的值..这也很好,但是从列表视图项目中选择的颜色消失了。当我更新列表视图时,我希望在项目上保持颜色:

"list.setAdapter(new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,data));"

这是我在该活动中的完整代码: `public class AllActivity 扩展 Activity {

int i;

ListView list;

ArrayList<String> data;

int index;

int top;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


setContentView(R.layout.activity_all);

list = (ListView) findViewById(R.id.lv_select2);

DatabaseCounter db = new DatabaseCounter(this);

ArrayList<Counter> counterList = db.getAllCounters();

data = new ArrayList<String>();

for (Counter counter : counterList){

    data.add(counter.getCounterName() + " : " + String.valueOf(counter.getCount()));

}

list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,data));

list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub

        i = Integer.parseInt(String.valueOf(id));

        index = list.getFirstVisiblePosition();

        View v = list.getChildAt(0);

        top = (v == null) ? 0 : v.getTop();


        for (int j = 0; j < parent.getChildCount(); j++)

            parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);


            view.setBackgroundColor(Color.MAGENTA);


    }

});

}

public void onButtonClick(View v) {
    // TODO Auto-generated method stub
    DatabaseCounter db = new DatabaseCounter(this);


    Counter counter;

    switch(v.getId()){


    case R.id.btn2_plus:


        counter = db.getCounter(i+1);

        counter.setCount(counter.getCount()+counter.getIncValue());

        db.updateCounter(counter);

        data.set(i, counter.getCounterName() + " : " + String.valueOf(counter.getCount()));

        list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,data));

        list.getChildAt(0).setBackgroundColor(Color.MAGENTA);

        list.setSelectionFromTop(index, top);



        break;

    }
}

} `

【问题讨论】:

  • When I select an Item on the Listview the Color changes. ?你的意思是When I click an Item on the Listview I change the Color. 吗? BUT the selected color from the listview item dissappear好的。但是你忘了说什么时候了。

标签: android listview colors


【解决方案1】:

n如果您想为列表项视图设置一个永久颜色,那么您不仅应该在单击时输入该颜色,还应该告诉您的适配器该项目应该具有该颜色。当用户滚动列表视图时,就会看不见。当视图返回时,颜色已经消失。您必须在 getView() 中再次设置颜色。为此,您必须向适配器添加另一种颜色或布尔数组。在 onclick 中为该位置上的项目设置正确的值。

【讨论】:

    猜你喜欢
    • 2016-03-13
    • 2017-05-22
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多