使用android中的警告框从列表视图中删除行

时间:2016-04-19 14:23:23

标签: android sqlite listview android-sqlite sqliteopenhelper

我已将sqlite数据库中的一些数据检索到自定义ListView。现在当我在列表视图中查看数据时,我想点击一行,并且应该出现一个警告框告诉我你要删除这一行吗?如果我单击是,那么应删除特定行请在这种情况下帮助我,我正在发布我的代码

public class UserDbHelper extends SQLiteOpenHelper {
    String[] projections = {};

    private static final String DATABASE_NAME = "userinfo.db";
    private static final Integer DATABASE_VERSION = 1;
    private static final String CREAT_QUERY =
            "CREATE TABLE "+ UserContracts.NewUserInfo.TABLE_NAME+"("+ UserContracts.NewUserInfo.USER_NAME+" TEXT,"+
                    UserContracts.NewUserInfo.USER_MOB+" TEXT,"+ UserContracts.NewUserInfo.USER_EMAIL+" TEXT);";
    public UserDbHelper (Context context){    
     super(context,DATABASE_NAME,null,DATABASE_VERSION);
        Log.e("DATABASE OPERATIONS","Database created / opened...");    
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREAT_QUERY);
        Log.e("DATABASE OPERATIONS", "Table created...");
    }

    public void addInformation(String name,String mob,String email,SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(UserContracts.NewUserInfo.USER_NAME,name);
        contentValues.put(UserContracts.NewUserInfo.USER_MOB,mob);
        contentValues.put(UserContracts.NewUserInfo.USER_EMAIL,email);
        db.insert(UserContracts.NewUserInfo.TABLE_NAME, null, contentValues);
        Log.e("DATABASE OPERATIONS", "One row inserted");
    }

    public Cursor getInformation(SQLiteDatabase db){
      Cursor cursor;
        String[] projections = {UserContracts.NewUserInfo.USER_NAME, UserContracts.NewUserInfo.USER_MOB,
                UserContracts.NewUserInfo.USER_EMAIL};
        cursor=db.query(UserContracts.NewUserInfo.TABLE_NAME,projections,null,null,null,null,null);
        return cursor;
    }

    public void deleteInformation(String projections, SQLiteDatabase sqLiteDatabase) {

        String selection = UserContracts.NewUserInfo.USER_NAME+"LIKE?";
        String[] selection_args = {projections};
        sqLiteDatabase.delete(UserContracts.NewUserInfo.TABLE_NAME,selection,selection_args);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

以下是我编写警报框的另一项活动,但我没有得到所需的结果是代码

List list;
ListView listView;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
Context context;


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

    listView = (ListView)findViewById(R.id.list_view);
    listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
    listView.setAdapter(listDataAdapter);
    userDbHelper = new UserDbHelper(getApplicationContext());
    sqLiteDatabase = userDbHelper.getReadableDatabase();
    cursor = userDbHelper.getInformation(sqLiteDatabase);
    if (cursor.moveToFirst()){

        do {

            String name,mobile,email;
            name = cursor.getString(0);
            mobile = cursor.getString(1);
            email = cursor.getString(2);
            DataProvider dataProvider = new DataProvider(name,mobile,email);
            listDataAdapter.add(dataProvider);

        }while (cursor.moveToNext());

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, final int position, long arg2) {

                final AlertDialog.Builder builder = new AlertDialog.Builder(DataListActivity.this);
                builder.setMessage("Do you want to delete this from database?");
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {

                        listDataAdapter.remove(listDataAdapter.getItem(0));
                        Toast.makeText(getApplicationContext(), "Removed from list", Toast.LENGTH_SHORT).show();

                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder.show();
            }
        });
    }
}   

这是我的列表数据适配器类

public class ListDataAdapter extends ArrayAdapter {
    List list = new ArrayList();
    public ListDataAdapter(Context context, int resource) {
        super(context, resource);    
    }

    static class LayoutHandler{    
       TextView NAME,MOBILE,EMAIL;    
    }
    ListView listView;

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }    

    @Override
    public void remove(Object position) {
        super.remove(position);    
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {    
        View row = convertView;
        LayoutHandler layoutHandler;
        if (row == null){

            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.NAME = (TextView)row.findViewById(R.id.text_user_name);
            layoutHandler.MOBILE = (TextView)row.findViewById(R.id.text_user_mobile);
            layoutHandler.EMAIL = (TextView)row.findViewById(R.id.text_user_email);
            row.setTag(layoutHandler);
        }
        else {    
            layoutHandler = (LayoutHandler)row.getTag();    
        }

        DataProvider dataProvider = (DataProvider)this.getItem(position);
        layoutHandler.NAME.setText(dataProvider.getName());
        layoutHandler.MOBILE.setText(dataProvider.getMobile());
        layoutHandler.EMAIL.setText(dataProvider.getEmail());

        return row;    
    }
}

非常感谢任何帮助,谢谢:)

1 个答案:

答案 0 :(得分:0)

您可以在警告对话框中获取该行的ID。一种解决方案是在ListDataAdapter中编写删除方法:

public class ListDataAdapter extends ArrayAdapter {

  //Your existing code here

  // Remove your element from list and inform Adapter that data set has changed
  public boolean removeRow(int rowIndex){
    if(list.size() > rowIndex){
        list.remove(rowIndex);
        notifyDataSetChanged();
        return true;
    }
    return false;
  } 
} 

在您需要删除行的位置,只需将实例添加到适配器并调用removeRow方法即可。删除第3行的示例:

((ListDataAdapter)listView.getAdapter()).removeRow(2);

但要注意,notifyDataSetChanged()基本上会重绘您的ListView,因此如果这是一个常见操作,请尝试使用优化版本 - RecyclerView。起初它看起来有点复杂,但通常值得。

你可以找到一些很好的教程,比如这个: http://www.vogella.com/tutorials/AndroidRecyclerView/article.html