如何从ListView中删除已检查的项目

时间:2012-03-31 07:50:39

标签: android android-listview android-adapter

我有一个listview,它是从sqlite数据库中获取的数据填充的。对于每个列表项我膨胀以下布局

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<CheckBox
    android:id="@+id/delete_or_not"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1.0" >

<TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chat_Date"
        android:textSize="10dp"
         />
<TextView
        android:id="@+id/summaryofchat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chat_Summary"
        android:textSize="20dp" />




</LinearLayout>

目前iam扩展了baseadapter。但是pblm是复选框正在被回收,所以当我删除一些项目并调用notifydataset()时,一些复选框被选中,而我没有这样做。 对于列表视图我有一个页脚,其中有一个删除按钮...所以在用户滚动列表视图并选择要删除的列表项(通过单击复选框)后,我需要将它们删除在数据库中我想要listview显示新数据....我该怎么做.....我不需要代码但告诉我我可以实现它的方式....

我发布了我的自定义baseadapter的代码

    import java.io.FileOutputStream;
     import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;

    import com.Raja.BlueTooth.DataBaseRelated.BlueToothDataBaseHelper;

      import android.content.Context;
    import android.content.Intent;
      import android.database.Cursor;
     import android.database.sqlite.SQLiteDatabase;
     import android.provider.ContactsContract.Contacts.Data;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
     import android.widget.BaseAdapter;
      import android.widget.CheckBox;
          import android.widget.CompoundButton;
       import android.widget.CompoundButton.OnCheckedChangeListener;
        import android.widget.LinearLayout;
       import android.widget.ListView;
     import android.widget.TextView;
      import android.widget.Toast;

public class ListofSavedChatsAdapter extends BaseAdapter {

private Context mContext;
SQLiteDatabase dbinstance;
private String[] dates;
private String[] chats;
private ArrayList<Boolean> mChecked;
BlueToothDataBaseHelper  retriever;
ListView lv1;
View footer;
Cursor c;

private ListofSavedChatsAdapter adapterInstance;

public ListofSavedChatsAdapter(Context context, ListView lv, View v)
{
    this.lv1 = lv;
    this.mContext = context;
    retrieveFromDataBase();
    this.footer = v;


}


public void retrieveFromDataBase() {
    retriever = new BlueToothDataBaseHelper(mContext);
    dbinstance = retriever.getReadableDatabase();
    c = dbinstance.query(BlueToothDataBaseHelper.CHAT_TABLE, new String[] {
            BlueToothDataBaseHelper.CHAT_TABLE_ID_COLUMN,
            BlueToothDataBaseHelper.CHAT_TABLE_DATE_COLUMN,
            BlueToothDataBaseHelper.CHAT_TABLE_DATA_COLUMN }, null, null,
            null, null, null);
    Log.d("count :", String.valueOf(c.getCount()));
    mChecked = new ArrayList<Boolean>();
    for (int i1 = 0; i1 < c.getCount(); i1++) {
        mChecked.add(i1, false);
        Log.d("RetrieveFromDataBase:item at " + String.valueOf(i1),
                "is set" + String.valueOf(mChecked.get(i1)));
    }
    dates = new String[c.getCount()];
    chats = new String[c.getCount()];
    c.moveToFirst();
    int i = 0;
    while (!c.isAfterLast()) {
        dates[i] = c.getString(c.getColumnIndex(BlueToothDataBaseHelper.CHAT_TABLE_DATE_COLUMN));
        chats[i] = c.getString(c.getColumnIndex(BlueToothDataBaseHelper.CHAT_TABLE_DATA_COLUMN));
        Log.d("date:", dates[i]);
        Log.d("chat:", chats[i]);
        c.moveToNext();
        i++;
    }
    c.close();
}

public class ViewHolder
{
    TextView DATE;
    TextView DATA;
    CheckBox check;
}


public void itemsSelected()
{
    for(int j=0;j<mChecked.size();j++)
    {

        if(mChecked.get(j))
        {

            Log.d("ItemSelected:item at " +String.valueOf(j) , "is checked");
            //String ssss = """ + chats[j]+""";
            Boolean dora = dbinstance.delete(BlueToothDataBaseHelper.CHAT_TABLE, BlueToothDataBaseHelper.CHAT_TABLE_DATA_COLUMN + " = '"+chats[j]+"'", null) > 0;
            Log.d("Delete", String.valueOf(dora));

        }

    }

}




public void delete()
{
    dbinstance = retriever.getWritableDatabase();
    itemsSelected();
    dbinstance.close();

    retrieveFromDataBase();
    notifyDataSetChanged();
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return dates.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView,  ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.d("position", String.valueOf(position));
    Log.d("items in cursor", String.valueOf(c.getCount()));
    if(convertView == null)
    {
        ViewHolder mViewHolder1 = new ViewHolder();
        LayoutInflater mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mLayoutInflater.inflate(R.layout.savedchatselementlayout, null);
        mViewHolder1.DATA = (TextView)convertView.findViewById(R.id.summaryofchat);
        mViewHolder1.DATE = (TextView)convertView.findViewById(R.id.date);
        mViewHolder1.check = (CheckBox)convertView.findViewById(R.id.delete_or_not);
        MyCheckedListener kk = new MyCheckedListener(position);
        mViewHolder1.check.setOnCheckedChangeListener(kk);
        mChecked.set(position, kk.getCheckedorNot());

        mViewHolder1.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                LayoutInflater mLayoutInflater1 = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //  LinearLayout temp = (LinearLayout)mLayoutInflater1.inflate(R.layout.listofsavedchats, null).findViewById(R.id.buttonbar);
                if(buttonView.isChecked())
                {
                    Log.d("came to checked loop", "why");
                    Log.w("MCHECKED_LENGTH_A", ""+mChecked.size());
                    Log.w("MCHECKED_POSITION_A", ""+position);
                    Log.w("ADAPTER_LENGTH_A", ""+getCount());

                    mChecked.set(position, true);
                    Log.d("Checkedloop:item at "+position, String.valueOf(mChecked.get(position)));
                    //lv1.addFooterView(footer);
                    //temp.setVisibility(View.VISIBLE);
                    // (mLayoutInflater1.inflate(R.layout.listofsavedchats, null)).findViewById(R.id.buttonbar).setVisibility(View.VISIBLE);

                    Toast.makeText(mContext, "checkbox at "+position + "is checked" , Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Log.d("position in else loop is:", String.valueOf(position));
                    Log.d("came to checked else loop", "why");
                    Log.w("MCHECKED_LENGTH_B", ""+mChecked.size());
                    Log.w("MCHECKED_POSITION_B", ""+position);
                    Log.w("ADAPTER_LENGTH_A", ""+getCount());
                    mChecked.set(position, false);
                    Log.d("UnCheckedloop:item at "+position, String.valueOf(mChecked.get(position)));
                    //lv1.removeFooterView(footer);
                //  temp.setVisibility(View.GONE);
                    //mLayoutInflater1.inflate(R.layout.listofsavedchats, null).findViewById(R.id.buttonbar).setVisibility(View.INVISIBLE);

                    Toast.makeText(mContext, "checkbox at "+position + "is unchecked" , Toast.LENGTH_SHORT).show();
                }

            }
        });
        convertView.setTag(mViewHolder1);
    }
    ViewHolder mViewHolder2 = (ViewHolder)convertView.getTag();
    mViewHolder2.DATA.setText(chats[position]);
    mViewHolder2.DATE.setText(dates[position]);
    Log.d("mChecked["+String.valueOf(position)+"]", String.valueOf(mChecked.get(position)));
    Log.d("i accessed:", String.valueOf(mChecked.get(position)));
    //Log.d("mChecked", mChecked.)
    Log.d("checkbox at "+String.valueOf(position)+" is:", String.valueOf(mViewHolder2.check.isChecked()));
    mViewHolder2.check.setChecked(false);

    return convertView;
}

}

     D/create string is :(  234): create table Chat_Table( _id integer primary key, Date DATETIME not null, Data Text not null, Checked Integer);

D/count : (  234): 6

D/RetrieveFromDataBase:item at 0(  234): is setfalse

D/RetrieveFromDataBase:item at 1(  234): is setfalse

D/RetrieveFromDataBase:item at 2(  234): is setfalse

D/RetrieveFromDataBase:item at 3(  234): is setfalse

D/RetrieveFromDataBase:item at 4(  234): is setfalse

D/RetrieveFromDataBase:item at 5(  234): is setfalse

D/date:   (  234): 2012-3-31 2:0

D/chat:   (  234): Ravi

D/date:   (  234): 2012-3-31 3:1

D/chat:   (  234): chittor

D/date:   (  234): 2012-3-31 23:23

D/chat:   (  234): idiot

D/date:   (  234): 2012-3-31 23:23

D/chat:   (  234): rascal

D/date:   (  234): 2012-3-31 23:23

D/chat:   (  234): bloody

D/date:   (  234): 2012-3-31 23:23

D/chat:   (  234): donkey

D/position(  234): 0

D/items in cursor(  234): 6

D/mChecked[0](  234): false

D/i accessed:(  234): false

D/checkbox at 0 is:(  234): true

D/position in else loop is:(  234): 7

D/came to checked else loop(  234): why

W/MCHECKED_LENGTH_B(  234): 6

W/MCHECKED_POSITION_B(  234): 7

W/ADAPTER_LENGTH_A(  234): 6

D/AndroidRuntime(  234): Shutting down VM

W/dalvikvm(  234): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

E/AndroidRuntime(  234): Uncaught handler: thread main exiting due to uncaught exception

E/AndroidRuntime(  234): java.lang.IndexOutOfBoundsException

E/AndroidRuntime(  234):    at java.util.ArrayList.set(ArrayList.java:580)

E/AndroidRuntime(  234):    at com.Raja.BlueTooth.UI.ListofSavedChatsAdapter$1.onCheckedChanged(ListofSavedChatsAdapter.java:185)

E/AndroidRuntime(  234):    at android.widget.CompoundButton.setChecked(CompoundButton.java:122)

E/AndroidRuntime(  234):    at com.Raja.BlueTooth.UI.ListofSavedChatsAdapter.getView(ListofSavedChatsAdapter.java:205)

E/AndroidRuntime(  234): 

0 个答案:

没有答案