Android-动态添加对象到列表视图

发布于 2024-12-11 21:04:55 字数 1917 浏览 0 评论 0原文

我的目标是在启用复选框时将列表项推到列表末尾。我的列表项是一个对象“任务”。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏舞 2024-12-18 21:04:55

您需要调用notifyDataSetChanged()来更新列表视图:

list.remove(position);
list.add(taskItem);
notifyDataSetChanged();   // add this line after adding task again at last position

You need to call notifyDataSetChanged() to update the listview:

list.remove(position);
list.add(taskItem);
notifyDataSetChanged();   // add this line after adding task again at last position
指尖上的星空 2024-12-18 21:04:55

试试这个(没有编写整个适配器,但您看到需要替换的部分):

        View v = convertView;

        if(v == null){
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }

        final Task task = getItem(position);
        if(isChecked)
        {
              text.setTextColor(Color.parseColor("#468765"));
              btnChild.setText("Revert");
              list.remove(task);
              list.add(task);
              notifyDataSetChanged();
        }
       return v;

要获得按钮和颜色的新更改,您需要 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):

        View v = convertView;

        if(v == null){
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }

        final Task task = getItem(position);
        if(isChecked)
        {
              text.setTextColor(Color.parseColor("#468765"));
              btnChild.setText("Revert");
              list.remove(task);
              list.add(task);
              notifyDataSetChanged();
        }
       return v;

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.

冷…雨湿花 2024-12-18 21:04:55

您应该重写 ArrayAdapter 中的某些方法

@Override
public int getCount() { 
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

You should Override some method in your ArrayAdapter

@Override
public int getCount() { 
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文