Android-动态添加对象到列表视图
我的目标是在启用复选框时将列表项推到列表末尾。我的列表项是一个对象“任务”。
ListView lv=this.getListView();
int size=lv.getCount();
Myarrayadapter adapter = new Myarrayadapter(this,getTasks());
lv.setAdapter(adapter);
lv.invalidateViews();
private List<Task> getTasks() {
List<Task> list = new ArrayList<Task>();
for(int k=0;k<taskname.length;k++)//taskname is an array of tasknames
{
list.add(new Task(taskname[k],status[k]));
return list;
}
Myarrayadapter 类如下所示。我的目的是检查复选框是否已启用。如果是,请进行一些更改(文本颜色和按钮文本)并将此更改的视图移动到最后一个位置。
public class Myarrayadapter extends ArrayAdapter<Task> {
private final Activity context;
private final List<Task> list;
public Myarrayadapter(Activity context, List<Task> list) {
super(context, R.layout.list_item, list);
this.context = context;
this.list=list;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item, null, true);
final TextView text = (TextView) rowView.findViewById(R.id.label);
CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.cb);
final Button btnChild = (Button) rowView.findViewById(R.id.directions);
text.setText(list.get(position).getName());
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
text.setTextColor(Color.parseColor("#468765"));
btnChild.setText("Revert");
Task taskItem= list.get(position);
list.remove(position);
list.add(taskItem);
}
}});
return rowView;
}
它没有移动我想要的确切列表项。
提前致谢!!!!
My aim is to push the list item to the end of the list when the check box is enabled. My list item is an object "Task".
ListView lv=this.getListView();
int size=lv.getCount();
Myarrayadapter adapter = new Myarrayadapter(this,getTasks());
lv.setAdapter(adapter);
lv.invalidateViews();
private List<Task> getTasks() {
List<Task> list = new ArrayList<Task>();
for(int k=0;k<taskname.length;k++)//taskname is an array of tasknames
{
list.add(new Task(taskname[k],status[k]));
return list;
}
Myarrayadapter class is shown below.my aim is to check whether checkbox is enabled.If yes, make some changes(text Colour and button text) and move this changed view to the last position.
public class Myarrayadapter extends ArrayAdapter<Task> {
private final Activity context;
private final List<Task> list;
public Myarrayadapter(Activity context, List<Task> list) {
super(context, R.layout.list_item, list);
this.context = context;
this.list=list;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item, null, true);
final TextView text = (TextView) rowView.findViewById(R.id.label);
CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.cb);
final Button btnChild = (Button) rowView.findViewById(R.id.directions);
text.setText(list.get(position).getName());
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
text.setTextColor(Color.parseColor("#468765"));
btnChild.setText("Revert");
Task taskItem= list.get(position);
list.remove(position);
list.add(taskItem);
}
}});
return rowView;
}
Its not moving the exact listitem I wanted.
Thanks in Advance!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要调用notifyDataSetChanged()来更新列表视图:
You need to call notifyDataSetChanged() to update the listview:
试试这个(没有编写整个适配器,但您看到需要替换的部分):
要获得按钮和颜色的新更改,您需要 setColor 和 setText (创建您自己的 setColor() 和 setText () 在 Task.class 中)删除任务后,添加它并在适配器中创建 if else 语句(通过使用 getColor() 和 getText()),以检查任务具有哪种颜色和哪种文本。
Try this (didn't write your whole adapter but you see the parts you need to replace):
To get the new changes of the button and color you'll need to need to setColor and setText (create your own setColor() and setText() in Task.class) on the Task after removing it, then add it and make if else statements in adapter (by using getColor() and getText()), to check which color the task has and which text.
您应该重写 ArrayAdapter 中的某些方法
You should Override some method in your ArrayAdapter