如何从列表视图中删除选定的项目?

发布于 2024-12-07 11:36:44 字数 883 浏览 0 评论 0原文

对于这种混乱,我们深表歉意: 更新的问题: 我正在尝试从列表视图中删除列表项。单击该项目时,将显示警报对话框。如果我单击“确定”,则必须从列表视图中删除所选项目。 我的代码如下:

case R.id.lvinc:
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Delete Event ");

        builder.setMessage("Delete this Event ?");
        builder.setPositiveButton("Ok, Delete",
                new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                  try
                  {
                     ???? //What code to delete the selected list item?




                  }catch(Exception e)
                  {
                      e.printStackTrace();
                  }
         }
                });

        AlertDialog alert = builder.create();
        alert.show();
        displaylist();
        break;

非常感谢任何帮助,并提前致谢......

SORRY FOR THIS CONFUSION:
UPDATED QUESTION:
I'm trying to remove a list item from the listview. when the item is clicked, the alertdialog is shown. If i click OK, then the selected item must be removed from the listview.
My Code goes below:

case R.id.lvinc:
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Delete Event ");

        builder.setMessage("Delete this Event ?");
        builder.setPositiveButton("Ok, Delete",
                new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                  try
                  {
                     ???? //What code to delete the selected list item?




                  }catch(Exception e)
                  {
                      e.printStackTrace();
                  }
         }
                });

        AlertDialog alert = builder.create();
        alert.show();
        displaylist();
        break;

Any help is really appreciated and thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

一萌ing 2024-12-14 11:36:45

我尝试解决它并得到了一个解决方案,请执行以下代码:

listview.java

public class listview extends Activity implements OnItemClickListener{

     ListView list;
     ListAdapter adapter;
     ArrayList<String> nameArray;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//xml should have an ListView element
        nameArray   =   new  ArrayList<String>();
        nameArray.add("Item1");
        nameArray.add("Item2");
        nameArray.add("Item3");
        nameArray.add("Item4");
        nameArray.add("Item5");
        list = (ListView) findViewById(R.id.listView);
        list.setOnItemClickListener(listview.this);
        adapter=new ListAdapter(listview.this, nameArray);
        list.setAdapter(adapter);
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        showDialog(arg2);
    }
    @Override
    protected Dialog onCreateDialog(final int id) {
        Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Delete Event")
            .setCancelable(true)
            .setPositiveButton("Ok, Delete",
                    new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        nameArray.remove(id);
                        adapter=new ListAdapter(listview.this, nameArray);
                        list.setAdapter(adapter);
                    }
                    })
            .setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                    });
        AlertDialog dialog = builder.create();
        dialog.show();
        return super.onCreateDialog(id);
    }
}

//Adapter Class

ListAdapter.java

public class ListAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<String> name;
    private static LayoutInflater inflater=null;

    public ListAdapter(Activity a, ArrayList<String> nameArray) {
        activity = a;
        name    =   nameArray;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return name.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public TextView text;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.list_item, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.title);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        holder.text.setText(name.get(position));
        return vi;
    }
}

I tried to solve it and got one solution pls go through the code below:

listview.java

public class listview extends Activity implements OnItemClickListener{

     ListView list;
     ListAdapter adapter;
     ArrayList<String> nameArray;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//xml should have an ListView element
        nameArray   =   new  ArrayList<String>();
        nameArray.add("Item1");
        nameArray.add("Item2");
        nameArray.add("Item3");
        nameArray.add("Item4");
        nameArray.add("Item5");
        list = (ListView) findViewById(R.id.listView);
        list.setOnItemClickListener(listview.this);
        adapter=new ListAdapter(listview.this, nameArray);
        list.setAdapter(adapter);
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        showDialog(arg2);
    }
    @Override
    protected Dialog onCreateDialog(final int id) {
        Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Delete Event")
            .setCancelable(true)
            .setPositiveButton("Ok, Delete",
                    new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        nameArray.remove(id);
                        adapter=new ListAdapter(listview.this, nameArray);
                        list.setAdapter(adapter);
                    }
                    })
            .setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                    });
        AlertDialog dialog = builder.create();
        dialog.show();
        return super.onCreateDialog(id);
    }
}

//Adapter Class

ListAdapter.java

public class ListAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<String> name;
    private static LayoutInflater inflater=null;

    public ListAdapter(Activity a, ArrayList<String> nameArray) {
        activity = a;
        name    =   nameArray;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return name.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public TextView text;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.list_item, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.title);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        holder.text.setText(name.get(position));
        return vi;
    }
}
萧瑟寒风 2024-12-14 11:36:45

我不知道你用的是什么类型的数据。

我想象游标、数据库或列表,请随时告诉我们,这样会更容易提供帮助。

此示例适用于列表:

protected void onListItemClick(View v, int pos, long id) {
    Log.i(TAG, "onListItemClick id=" + id);
    //Display your Dialog
    (...)
    builder.setPositiveButton("Ok, Delete",
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            myList.remove(pos);
            myAdapter.notifyDataChanged();
     }
            });

}

I don't know what kind of data you are using.

I imagine Cursor, database or List, feel free to tell us, so it will be easier to help.

This example is for a list:

protected void onListItemClick(View v, int pos, long id) {
    Log.i(TAG, "onListItemClick id=" + id);
    //Display your Dialog
    (...)
    builder.setPositiveButton("Ok, Delete",
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            myList.remove(pos);
            myAdapter.notifyDataChanged();
     }
            });

}
忘东忘西忘不掉你 2024-12-14 11:36:45

您可能有某种适配器,可以提供某种数据。

从该列表中删除该项目,然后将 DataChanged 通知给 ListView。

最后:dialog.dismiss();

You propably have some kind of Adapter, that you have feed with some kind of data.

Remove the item from that list, and notifyDataChanged to the ListView.

And finally: dialog.dismiss();

梦途 2024-12-14 11:36:45
itemLayout.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
 indexClick = position;
}
}
if ((indexClick) == (position)) {
  itemLayout.setBackgroundResource(R.drawable.tab_select);
}

否则放置 tab_unselected 图像。

itemLayout.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View arg0) {
 indexClick = position;
}
}
if ((indexClick) == (position)) {
  itemLayout.setBackgroundResource(R.drawable.tab_select);
}

otherwise put tab_unselected image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文