如何在android中删除自定义列表视图项

时间:2016-11-25 12:24:08

标签: android listview

我想删除列表中的listview项目..但是当我点击On Button时没有发生任何事情。请告诉我我做错了...这段代码可以添加项目,但只删除项目...

// listview java

    public class MemberActivity extends AppCompatActivity implements BaseColumns {
ListView mylist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_member);

    final ListView listview;
    final ListViewAdapter adapter;
    //final ArrayList<String> items = new ArrayList<String>();


    mylist = (ListView) findViewById(R.id.listview1);
    final LinearLayout linewarLayout1 = (LinearLayout) findViewById(R.id.addmember);
    final LinearLayout linewarLayout2 = (LinearLayout) findViewById(R.id.buttongroup);


    adapter = new ListViewAdapter();


    listview = (ListView) findViewById(R.id.listview1);
    final View header = getLayoutInflater().inflate(R.layout.listview_header, null, false);

    listview.setAdapter(adapter);

    Button addButton = (Button) findViewById(R.id.add);
    addButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            mylist.setVisibility(View.INVISIBLE);
            linewarLayout2.setVisibility(View.INVISIBLE);
            linewarLayout1.setVisibility(View.VISIBLE);
        }
    });

    final EditText name = ((EditText) findViewById(R.id.etName));
    final EditText ID = ((EditText) findViewById(R.id.etID));
    final EditText Major = ((EditText) findViewById(R.id.etMajor));

    Button btnDone = (Button) findViewById(R.id.btnDone);
    btnDone.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {

            //TextView cnttxt = (TextView)findViewById(R.id.count);
            //cnttxt.setText(adapter.getCount());

            adapter.addItem(name.getText().toString(), ID.getText().toString(), Major.getText().toString());
            name.setText("");
            ID.setText("");
            Major.setText("");
            Toast.makeText(getApplicationContext(), "add.", Toast.LENGTH_LONG).show();
            mylist.setVisibility(View.VISIBLE);
            linewarLayout2.setVisibility(View.VISIBLE);
            linewarLayout1.setVisibility(View.INVISIBLE);
        }
    });

    Button btnCancel = (Button) findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            name.setText("");
            ID.setText("");
            Major.setText("");
            mylist.setVisibility(View.VISIBLE);
            linewarLayout2.setVisibility(View.VISIBLE);
            linewarLayout1.setVisibility(View.INVISIBLE);
        }
    });

    // delete button
    Button deleteButton = (Button) findViewById(R.id.delete);
    deleteButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            int count, checked;
            count = adapter.getCount();

            if (count > 0) {

                listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
                checked = listview.getCheckedItemPosition();

                if (checked > -1 && checked < count) {

                    adapter.removeitem(checked);


                    listview.clearChoices();


                    adapter.notifyDataSetChanged();
                }
            }
        }
    });

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // get item
            ListViewItem item = (ListViewItem) parent.getItemAtPosition(position);

            String nameStr = item.getname();
            String IDStr = item.getID();
            String majorStr = item.getmajor();

        }
    });



}

} //适配器

public class ListViewAdapter extends BaseAdapter {
private ArrayList<ListViewItem> listViewItemList = new ArrayList<ListViewItem>();

public ListViewAdapter() {

}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
    final Context context = parent.getContext();

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_item, parent, false);
    }

    TextView nameTextView = (TextView) convertView.findViewById(R.id.list_name);
    TextView IDTextView = (TextView) convertView.findViewById(R.id.list_ID);
    TextView majorTextView = (TextView) convertView.findViewById(R.id.list_major);

    ListViewItem listViewItem = listViewItemList.get(position);

    nameTextView.setText(listViewItem.getname());
    IDTextView.setText(listViewItem.getID());
    majorTextView.setText(listViewItem.getmajor());

    return convertView;
}

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

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

public void removeitem(int position) {
    listViewItemList.remove(position);
    notifyDataSetChanged();
}

public void addItem(String name, String ID, String major) {
    ListViewItem item = new ListViewItem();

    item.setname(name);
    item.setID(ID);
    item.setmajor(major);

    listViewItemList.add(item);

    notifyDataSetChanged();
}

}

// ListViewItem java

public class ListViewItem {
private String nameStr;
private String IDStr;
private String majorStr;

public void setname(String name) {
    nameStr = name;
}

public void setID(String ID) {
    IDStr = ID;
}

public void setmajor(String major) {
    majorStr = major;
}

public String getname() {
    return this.nameStr;
}

public String getID() {
    return this.IDStr;
}

public String getmajor() {
    return this.majorStr;
}

}

1 个答案:

答案 0 :(得分:0)

更新您的代码,如下所示。在创建listview时和设置适配器之前,将选择模式设置为CHOICE_MODE_SINGLE。从按钮单击中删除CHOICE_MODE_SINGLE

listview = (ListView) findViewById(R.id.listview1);
listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listview.setAdapter(adapter);

此外,您需要{list}中的setItemChecked onItemClick

检查以下示例代码。

public class MainActivity extends Activity {

private ListView mListView;
private String[] mData = new String[] { "xxx", "yyy", "zzz", "aaa" };
private BaseAdapter mAdapter;
private int mLastCorrectPosition = -1;
private int mButtonPosition = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mListView = (ListView) findViewById(R.id.list_view);
    mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    mAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_activated_1, mData);
    mListView.setAdapter(mAdapter);
    mListView.setSelector(new ColorDrawable(0));

    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
            if (position == mButtonPosition) {
                if (mLastCorrectPosition != -1) {
                    mListView.setItemChecked(mLastCorrectPosition, true);
                }
                else {
                    mListView.setItemChecked(mButtonPosition, false);
                }
                // here show dialog
            }
            else {
                mLastCorrectPosition = position;
                // here refresh fragment
            }
        }
    });
}
}