代码之家  ›  专栏  ›  技术社区  ›  Suraj

使用与之关联的按钮从ListView中删除行

  •  0
  • Suraj  · 技术社区  · 8 年前

    我有一个listview,我在上面显示数据库中的数据,现在我想根据与每一行相关联的按钮删除行。

    每当我按下“完成”按钮时,CustomAdapter类中就会出现异常。

    public class CustomAdapter extends CursorAdapter {
      TextView task,daate;
      Button del;
      public static int id;
      Context ct;
      public CustomAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }
    
    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ct=context;
        return LayoutInflater.from(context).inflate(R.layout.adapter, parent, false);
    
    }
    
    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final DatabaseHelper help=new DatabaseHelper(ct);
        del = (Button) convertView.findViewById(R.id.deleteBtn);
        del.setOnClickListener( new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                      help.deleteRecordWithId(position);
                                    }
                                }
        );
        return super.getView(position, convertView, parent);
    }
    
    
    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // Find fields to populate in inflated template
            task = (TextView) view.findViewById(R.id.dynamicTask);
            daate = (TextView) view.findViewById(R.id.dynamicDate);
            id=cursor.getInt(cursor.getColumnIndex("_id"));
        String Task=cursor.getString(cursor.getColumnIndex("task"));
        String Daate=cursor.getString(cursor.getColumnIndex("ddate"));
        task.setText(Task);
        daate.setText(Daate);
    }
    

    }

    我的数据库函数deleteRecordWithId()是:

     public boolean deleteRecordWithId(int id) {
        SQLiteDatabase db=this.getWritableDatabase();
        long rows=db.delete(TABLE_NAME,"_id=?",new String[] {String.valueOf(id)});
        if(rows>0) {
            return  true;
        }
        else {
            return false;
        }
    }
    

    我得到了一个例外:

      java.lang.NullPointerException: Attempt to invoke virtual method 
      'android.view.View android.view.View.findViewById(int)' on a null object 
      reference
    

    这段代码有什么错误。请帮我纠正一下。

    4 回复  |  直到 8 年前
        1
  •  1
  •   Muhammad Saad Rafique    8 年前

    替换此:

    help.deleteRecordWithId(position);
    

    使用:

    CustomAdapter.this.remove(CustomAdapter.this.getItem(position));
    CustomAdapter.this.notifyDataSetChanged();
    

    它将从适配器中删除数据,notifydatasetchanged将相应地更新listview。

        2
  •  0
  •   Umut ADALI    8 年前

    第一种方法:你这样描述

    public class CustomAdapter extends CursorAdapter {
    
    TextView task,daate;
    Button del;
    public static int id;
    Context ct;
    public CustomAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }
    
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ct=context;
        return LayoutInflater.from(context).inflate(R.layout.adapter, parent, false);
    
    }
    
    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // Find fields to populate in inflated template
        task = (TextView) view.findViewById(R.id.dynamicTask);
        daate = (TextView) view.findViewById(R.id.dynamicDate);
        id=cursor.getInt(cursor.getColumnIndex("_id"));
        String Task=cursor.getString(cursor.getColumnIndex("task"));
        String Daate=cursor.getString(cursor.getColumnIndex("ddate"));
        task.setText(Task);
        daate.setText(Daate);
        final DatabaseHelper help=new DatabaseHelper(ct);
        del = (Button) convertView.findViewById(R.id.deleteBtn);
    }}
    

    主要活动:

    public class MainActivity{
    private CustomAdapter cursorAdapter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        cursorAdapter = new CustomAdapter(this, null);
    }
    public void deleteRecordWithId(View view) {
        View parentRow = (View) view.getParent().getParent();
        ListView listView = (ListView) view.getParent().getParent().getParent();
        int position = listView.getPositionForView(parentRow);
        long id = cursorAdapter.getItemId(position);
    
        SQLiteDatabase db=this.getWritableDatabase();
    long rows=db.delete(TABLE_NAME,"_id=?",new String[] {String.valueOf(id)});
    if(rows>0) {
        return  true;
    }
    else {
        return false;
    }
    }
    }
    

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/task_outer_container"
                    android:layout_width="match_parent"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:gravity="center_vertical"
                    android:background="@drawable/background_transparent"
                    android:layout_height="wrap_content"
                    android:clipToPadding="false"
                    android:clipChildren="false"
        >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:id="@+id/delete_button"
            android:onClick="deleteRecordWithId"
            app:srcCompat="@drawable/remove_icon"
            android:background="@drawable/background_transparent"
            android:layout_gravity="center_vertical"
            android:adjustViewBounds="false"
            android:paddingStart="7dp"
            android:paddingEnd="7dp"
            android:layout_marginEnd="-5dp"/>
    
    </RelativeLayout>
    

    第二种方法:不应该在Cursoradapter中使用getView。您需要在bindview中执行所有操作。因此,uou无法使用getview访问id。

    例如: https://github.com/rodoggx/w4d1_Simple_CursorAdapter/blob/master/app/src/main/java/com/example/sqlitecursoradapter/CustomAdapter.java

        3
  •  0
  •   Flutterian    8 年前

    设置 setOnItemClickListener

    yourListView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                yourListView.remove(position);
                database.removeItem(id);//create removemethod in database class
    
            }
        });
    

    定义removeItem方法

    public void remove(long id){
            String str =String.valueOf(id);
            database.execSQL("DELETE FROM <TABLE_NAME> WHERE _id = '" + str + "'");
        }
    
        4
  •  0
  •   UdeshUK    8 年前

    ListView并没有提供一种直接的方法来实现项内视图的操作。相反,使用RecyclerView,它比列表视图更具可定制性,对性能也更好。

    Create Lists and Cards - Android Developer