【发布时间】: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好的。但是你忘了说什么时候了。