onClick监听器没有在预定义的listview布局上工作?

时间:2014-01-24 05:19:37

标签: android listview sms android-arrayadapter onclicklistener

我正在使用预定义的listview布局将所有本机消息sms显示到我的application.Code中我在onCreate方法中定义:

lvInb = (ListView) findViewById(R.id.lvInb);
    lvInb.setOnCreateContextMenuListener(this);
    data = fetchInbox();
    if(data!=null)
    {
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , data);
        lvInb.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvInb.setAdapter(adapter);
    }

for fetchInbox()我创建了另一种方法:

    public ArrayList<String> fetchInbox()
{
     ArrayList<String> sms = new ArrayList<String>();
     Uri uriSms = Uri.parse("content://sms/inbox");
     Cursor cr = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
     cr.moveToFirst();
        while  (cr.moveToNext())
        {
             sms.add(cr.getString(1)+"\n"+cr.getString(3)+"\n");
        }
        cr.close();
        return sms;
    }

删除listview中的行项目的代码是:

private void openDelete() {
     removeListViewItem();

}
public void removeListViewItem() {
    if(intListViewItemPosition != -1){
        lvInb.removeViewAt(intListViewItemPosition);
        adapter.notifyDataSetInvalidated();
        intListViewItemPosition = -1;
    }else{
        Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();

    }
}

活动代码:

ListView lvInb;
ArrayAdapter<String> adapter;
int intListViewItemPosition = -1;
ArrayList<String> data;

外部onCreate方法:

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, view, menuInfo);
    CreateMenu (menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    MenuChoice(item);
    return true;
}


private void CreateMenu(Menu menu)
{
    MenuItem mnu1 = menu.add(0,0,0,"Delete");
    {
        mnu1.setIcon(R.drawable.ic_launcher);
    }}
private boolean MenuChoice(MenuItem item)
{
    switch (item.getItemId()) {
    case 0:
        removeListViewItem();
        break;
}
    return false;
}
public void removeListViewItem() {
    intListViewItemPosition = lvInb.getSelectedItemPosition();
    if(intListViewItemPosition != -1){
        //lvInb.removeViewAt(intListViewItemPosition);
        data.remove(intListViewItemPosition);
        adapter.notifyDataSetInvalidated();
        intListViewItemPosition = -1;
    }else{
        Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();

    }
}
我在菜单选项中提供了

删除选项。但是当我选择一个行项目并单击删除选项时,它显示没有选择项目

2 个答案:

答案 0 :(得分:0)

嘿,您将所选项目位置值分配到“intListViewItemPosition”

如果没有使用,请在listview OnItemClickListener中实现,

intListViewItemPosition= listView.getSelectedItemPosition();

您正试图从列表视图中删除所选项目吗?因为你已经从lisview点击了listview项目,所以你需要为listview实现onClickListener

你的代码应该是这样的,

 your_listView_name.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
           intListViewItemPosition= listView.getSelectedItemPosition();
        }
    });

多数民众赞成......

答案 1 :(得分:0)

首先只调用fetchIndex一次:

在activity中声明ArrayList字符串:

ArrayList<String> data;
ArrayAdapter<String> adapter

并在onCreate:

lvInb = (ListView) findViewById(R.id.lvInb);
    lvInb.setOnCreateContextMenuListener(this);
    data= fetchInbox();
    if(data!=null)
    {
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , data);
        lvInb.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvInb.setAdapter(adapter);
    }

现在将更改removeListViewItem更改为:

public void removeListViewItem() {
    intListViewItemPosition= lvInb.getSelectedItemPosition();
    if(intListViewItemPosition != -1){
        //lvInb.removeViewAt(intListViewItemPosition);
        data.remove(intListViewItemPosition);
        adapter.notifyDataSetInvalidated();
        intListViewItemPosition = -1;
    }else{
        Toast.makeText(this, "No item selected", Toast.LENGTH_SHORT).show();

    }
}

还有适配器的类级变量,以便您可以在removeListViewItem中访问它。

ArrayAdapter<String> adapter;

并在onCreate中说:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1 , data);

如果你在onCreate中再次声明它,那么类级别为null,当你removeListViewItem被调用时,你可能会得到空指针。