删除后刷新ListView

时间:2015-10-25 23:51:57

标签: android listview android-listview onlongclicklistener

我检查了很多堆栈溢出问题,但没有一个帮助。 使用onLongClickListener删除项目后如何刷新listView? 您还会看到adapter.notifyDataSetChanged()listView.invalidateViews()也无法工作。

这是通过必要的方法来实现想法的实现。

public class MainActivity extends AppCompatActivity {
public FloatingActionButton fabAddWord;
public Toolbar toolbar;
public ListView listView;
private RjecnikCursorAdapter adapter;

private RjecnikDB dbRjecnik;
private SQLiteDatabase db;
private Cursor cursor;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fabAddWord = (FloatingActionButton) findViewById(R.id.fabAddWord);
    listView = (ListView) findViewById(R.id.listView);

    dbRjecnik = new RjecnikDB(this);
    db = dbRjecnik.getWritableDatabase();
    String query = "SELECT * FROM " + RjecnikDB.TABLE;
    cursor = db.rawQuery(query, null);

    adapter = new RjecnikCursorAdapter(this, cursor);
    listView.setAdapter(adapter);

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            deleteOnLongClick(cursor.getString(cursor.getColumnIndex(RjecnikDB.COLUMN_RIJEC)));

            adapter.notifyDataSetChanged();
            listView.invalidateViews();

            return true;
        }
    });
}

public void deleteOnLongClick(String rijec) {
    SQLiteDatabase db = dbRjecnik.getWritableDatabase();
    db.delete(RjecnikDB.TABLE, RjecnikDB.COLUMN_RIJEC + " = ?", new String[] {rijec} );

    this.adapter.notifyDataSetChanged();
    this.listView.invalidateViews();

    db.close();
}

CustomAdapter

public class RjecnikCursorAdapter extends CursorAdapter {

public RjecnikCursorAdapter (Context context, Cursor cursor) {
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.item_word, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tvSingleLineWord = (TextView) view.findViewById(R.id.tvSingleLineWord);

    // Extract properties from cursor
    String rijec = cursor.getString(cursor.getColumnIndex(RjecnikDB.COLUMN_RIJEC));

    // Populate fields with extracted properties
    tvSingleLineWord.setText(rijec);
}

}

2 个答案:

答案 0 :(得分:0)

你必须使用notifyDataSetChanged()来刷新listView数据,invalidateViews()只会重绘可见项,但它们上没有任何内容发生了变化(例如更改字体)。

请注意,建议在UI线程上运行notifyDataSetChanged()。

编辑2:您可以使用BaseAdapter代替CursorAdapter,如下所示

public class RjecnikCursorAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<String> rjecnikList;


    public RjecnikCursorAdapter(Activity activity, List<String> rjecnikList) {
        this.activity = activity;
        this.rjecnikList = rjecnikList;
    }

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

    @Override
    public Object getItem(int location) {
        return rjecnikList.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.item_word, null);

        TextView tvSingleLineWord = (TextView) view.findViewById(R.id.tvSingleLineWord);

        tvSingleLineWord.setText(String.valueOf(rjecnikList.get(position)));


        return convertView;
    }

}

在你的活动中:

    public class MainActivity extends AppCompatActivity {
    public FloatingActionButton fabAddWord;
    public Toolbar toolbar;
    public ListView listView;
    public  List<String> mylist;
    private RjecnikCursorAdapter adapter;

    private RjecnikDB dbRjecnik;
    private SQLiteDatabase db;
    private Cursor cursor;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        fabAddWord = (FloatingActionButton) findViewById(R.id.fabAddWord);
        listView = (ListView) findViewById(R.id.listView);

        dbRjecnik = new RjecnikDB(this);
        db = dbRjecnik.getWritableDatabase();
        String query = "SELECT * FROM " + RjecnikDB.TABLE;
        cursor = db.rawQuery(query, null);
        mylist = new ArrayList<>();

        if (cursor.moveToFirst()){

 do{
//change this with your column data
      String data = cursor.getString(cursor.getColumnIndex("data");

      mylist.add(data);

   }while(cursor.moveToNext());

}

cursor.close();
        adapter = new RjecnikCursorAdapter(this, mylist);
         listView.setAdapter(adapter);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                deleteOnLongClick(cursor.getString(cursor.getColumnIndex(RjecnikDB.COLUMN_RIJEC)));
                mylist.remove(position);
                adapter.notifyDataSetChanged();
                listView.invalidateViews();

                return true;
            }
        });
    }

    public void deleteOnLongClick(String rijec) {
        SQLiteDatabase db = dbRjecnik.getWritableDatabase();
        db.delete(RjecnikDB.TABLE, RjecnikDB.COLUMN_RIJEC + " = ?", new String[] {rijec} );

        this.adapter.notifyDataSetChanged();
        this.listView.invalidateViews();

        db.close();
    }

答案 1 :(得分:0)

适配器具有数据缓存,因此当数据库中的数据发生更改时,ListView中的数据不会更改。你应该改变光标。

@Override
public void changeCursor(Cursor cursor) {
    mIndexer.setCursor(cursor);
    super.changeCursor(cursor);
}

将代码放在OnItemLongClickListener

下面
cursor = db.rawQuery(query, null);
adapter.changeCurosr(cursor);