1

これに関するいくつかの投稿を見てきましたが、問題を解決するのに十分なほどフォローしていないようです。

レコードを更新または削除した後、ListView を更新しようとしています。現在、notifyDataSetChanged() を使用していますが、削除しても更新されません。削除できます。その後、元に戻してhistory.javaをリロードすると、すべてのデータをリロードしているため、更新が表示されます。

ここに私のHistoryAdapter.javaがあります

public class HistoryAdapter extends BaseAdapter {

    private Context mContext;
    Cursor cursor;
    history historyClass = new history();
    MySQLiteHelper db;

    public HistoryAdapter(Context context, Cursor cur){
        super();
        mContext = context;
        cursor = cur;
        db = new MySQLiteHelper(context);
    }

    public int getCount(){
        // return the number of records in cursor
        return cursor.getCount();
    }

    // getView method is called for each item of ListView
    public View getView(final int position, View view, ViewGroup parent){

        // inflate the layout for each item of listView
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.history_list_item, null);

        // move the cursor to required position
        cursor.moveToPosition(position);

        final String id = cursor.getString(cursor.getColumnIndex("_id"));
        final long deleteId = Long.parseLong(id);

        // fetch the information for each card
        String pricePerGallon = cursor.getString(cursor.getColumnIndex("pricePerGallon"));
        String gallons = cursor.getString(cursor.getColumnIndex("gallons"));
        String odometer = cursor.getString(cursor.getColumnIndex("odometer"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String filledOrNot = cursor.getString(cursor.getColumnIndex("filledOrNot"));
        String comments = cursor.getString(cursor.getColumnIndex("comments"));
        //String milesPerGallon = cursor.getString(cursor.getColumnIndex("miledPerGallon"));
        String totalSpent = cursor.getString(cursor.getColumnIndex("totalSpent"));

        // get the reference of TextViews
        TextView textViewPricePerGallon = (TextView) view.findViewById(R.id.cardPrice);
        TextView textViewGallons = (TextView) view.findViewById(R.id.cardGallons);
        TextView textViewOdometer = (TextView) view.findViewById(R.id.cardOdometer);
        TextView textViewDate = (TextView) view.findViewById(R.id.cardDate);
        TextView textViewFilledOrNot = (TextView) view.findViewById(R.id.cardFilledOrNot);
        TextView textViewComments = (TextView) view.findViewById(R.id.cardComments);
        //TextView textViewMilesPerGallon = (TextView) view.findViewById(R.id.mpg);
        TextView textViewTotalSpent = (TextView) view.findViewById(R.id.usd);
        TextView textViewDeleteButton = (TextView) view.findViewById(R.id.deleteButton);

        // Set the data to each TextView
        textViewPricePerGallon.setText(pricePerGallon);
        textViewGallons.setText(gallons);
        textViewOdometer.setText(odometer);
        textViewDate.setText(date);
        textViewFilledOrNot.setText(filledOrNot);
        textViewComments.setText(comments);
        //textViewMilesPerGallon.setText(milesPerGallon);
        textViewTotalSpent.setText(totalSpent);


        final HistoryAdapter historyAdapter = this;
        textViewDeleteButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
              Log.d("History Adapter", "" + deleteId);
              //need to delete here
              deleteRecord(deleteId);
              historyAdapter.notifyDataSetChanged();


            }
     });

        return view;

    }

    public Object getItem(int position){
        return position;
    }

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

    private void deleteRecord(long id){
        db.deleteGasLog(id);
    }

}

ここに、アダプターを設定してリストビューを作成するhistory.javaがあります

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);

        context = this;
        initViews();

        cursor = db.getAllLogs();

        // Create the Adapter
        historyAdapter = new HistoryAdapter(this, cursor);

        // Set the adapter to ListView
        listContent.setAdapter(historyAdapter);

    }
4

3 に答える 3

1

新しいカーソルを取得する必要があると思います。

これを動かしてみる

cursor = db.getAllLogs();

アダプターに挿入し、呼び出しの前に再度notifyDataSetChanged()呼び出します。

于 2014-02-21T22:05:31.787 に答える
0

行を削除していますが、アダプターがリストのレイアウトに使用する結果セットを持つ新しいカーソルを更新または取得することはありません。行を削除した後、アダプターに新しいカーソルを与えてから、notifyDatasetChanged() を呼び出す必要があります。BaseAdapter の代わりに SimpleCursorAdapter を使用する場合、そのswapCursor ()メソッドを使用して新しいカーソルを設定できます。

于 2014-02-21T22:12:33.383 に答える