应用程序。从ListView中删除项目时崩溃

时间:2012-02-26 09:10:56

标签: android listview

我有一个SQLite表,在某个活动中,我从表中获取所有名称,并使用这些名称填充listview。

在listview侦听器中,用户可以删除所选项目。

问题是当我删除应用程序崩溃的项目时。

请查看我的代码:

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

    tv_counter = (TextView) findViewById(R.id.counter);
    tv_testCounter = (TextView) findViewById(R.id.testCounter);
    lv = (ListView) findViewById(R.id.mylist);

    mpoh = new MP_DB(this);
    db = mpoh.getWritableDatabase();
    cv = new ContentValues();


       if (hasRecords()) {
           Toast.makeText(getBaseContext(), getRowsNum()+" row(s)", Toast.LENGTH_SHORT).show();
           get_MPNames();
           arrayToArrayList();
           setListView();
           lv.setOnItemClickListener(listViewListener);
       } else {
           Toast.makeText(getBaseContext(), "NO RECORDS"+","+getRowsNum()+"rows", Toast.LENGTH_SHORT).show();
       }
}

以下是将数组转换为arraylist的方法,以及listview监听器:

private void arrayToArrayList() {
    int s = str.length;
    al = new ArrayList<String>();
    for (int i=0; i < s; i++) {
        al.add(str[i]);
    }
}

private int getRowsNum() {
    return mpoh.getCurrentRowNumber();
}

OnItemClickListener listViewListener = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        pos = arg2;
        showDialoge();          
    }
};

这里我如何从DB和ListView中删除元素:

private void deleteMPfromListView(int pos) {
    al.remove(pos);
    adapter.notifyDataSetChanged();
    Toast.makeText(getBaseContext(), al.size()+" rows left in list view", Toast.LENGTH_SHORT).show();
}

private void deleteMPFromDB(int pos) {
    mpoh.deleteMP(pos);
    Toast.makeText(getBaseContext(), getRowsNum()+" rows left in DB", Toast.LENGTH_SHORT).show();
}

private Boolean hasRecords() {
    if (getRowsNum() == 0) {
        return false;
    } else {
        return true;
    }
}

private void setListView() {

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
            android.R.id.text1, al);
    lv.setAdapter(adapter);       
}

private void get_MPNames() {

    str = new String[getRowsNum()];

    for (int i=0; i <= getRowsNum()-1; i++) {
        str[i] = mpoh.getMP_Name(i+1);
    } //tv_testCounter.setText(str[87]);
}

1 个答案:

答案 0 :(得分:1)

从数据库中删除与从ListView中删除它们无关。我没有您的实施代码,但您可以尝试这样的东西,动态添加或删除项目:

public class LVDemo extends ListActivity {

// handles the ListView data
ArrayAdapter<String> adapter;

// Items that are displayed
ArrayList<String> listItems=new ArrayList<String>();

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.main);
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
        listItems);
    setListAdapter(adapter);
}

/**
 * Remove item.
 */
public void removeItem(int index) {
    listItems.remove(index);
    adapter.notifyDataSetChanged();
}

}

一般情况下:更改包含该元素的ArrayList,然后通知适配器以获取ListView