OnClick在BaseAdapter中的notifyDataSetChanged

时间:2014-07-11 00:35:00

标签: android android-fragments baseadapter notifydatasetchanged

在我的BaseAdapter中,我正在实现一个OnClickListener,按下时会删除一条记录。删除后,我希望它刷新由片段托管的列表视图。

BaseAdapter:

public class TransactionListAdapter extends BaseAdapter {

    private Context mContext;
    static Cursor cursor;
    static DatabaseHelper db;
    String table;

    public TransactionListAdapter(Context context, Cursor cur, String tab) {
        super();
        mContext = context;
        cursor = cur;
        db = new DatabaseHelper(context);
        table = tab;
        cursor = db.getTransactions(table);
    }

    public int getCount() {
        return cursor.getCount();
    }

    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder holder;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // SMOOTH SCROLLING
        if(view == null) {
            view = inflater.inflate(R.layout.rowlayout, null);

            holder = new ViewHolder();
            holder.tvDate = (TextView) view.findViewById(R.id.tvDate);
            holder.tvMemo = (TextView) view.findViewById(R.id.tvMemo);
            holder.tvAmount = (TextView) view.findViewById(R.id.tvAmount);
            holder.ivDelete = (ImageView) view.findViewById(R.id.ivDelete);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        // MOVE CURSOR TO REQUIRED POSITION
        cursor.moveToPosition(position);

        final int id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id")));

        // FETCH THE INFORMATION FOR EACH VIEW
        String transactionDate = cursor.getString(cursor.getColumnIndex("timestamp"));
        String transactionMemo = cursor.getString(cursor.getColumnIndex("memo"));
        String transactionCredit = cursor.getString(cursor.getColumnIndex("credit"));
        String transactionDebit = cursor.getString(cursor.getColumnIndex("debit"));

        holder.tvDate.setText(transactionDate);
        holder.tvMemo.setText(transactionMemo);
        if (transactionCredit == null) {
            holder.tvAmount.setTextColor(Color.RED);
            holder.tvAmount.setText("(" + transactionDebit + ")");
        } else {
            holder.tvAmount.setTextColor(Color.BLACK);
            holder.tvAmount.setText(transactionCredit);
        }

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.deleteTransaction(table, id);
                notifyDataSetChanged();

            }

          });
        return view;

    }

    public void setTable(String tab) {
        //table = tab;

        //Log.d("adapter", table);
    }

    public static class ViewHolder {
        public TextView tvDate;
        public TextView tvMemo;
        public TextView tvAmount;
        public ImageView ivDelete;
    }

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

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

这是我的片段,它包含listview:

public class TransactionListFragment extends Fragment {

    DatabaseHelper db;
    SimpleCursorAdapter adapter;
    ListView listContent;
    Cursor cursor;
    private Context mContext;
    TransactionListAdapter transactionListAdapter;
    String table;

    public TransactionListFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.transaction_list_fragment, container, false);
        table = getActivity().getIntent().getExtras().getString("table");

        listContent = (ListView) rootView.findViewById(R.id.list);
        mContext = getActivity();
        db = new DatabaseHelper(mContext);

        transactionListAdapter = new TransactionListAdapter(mContext, cursor, table);

        listContent.setAdapter(transactionListAdapter);

        return rootView;
    }


    public void onActivityCreated() {

        table = getArguments().getString("table");
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onSaveInstanceState(Bundle savedState) {
        super.onSaveInstanceState(savedState);

    }

}

我怀疑我需要在片段中而不是在适配器内部使用notifyDataSetChanged(),但我不确定它需要去哪里或如何实现它。

任何帮助将不胜感激!感谢

2 个答案:

答案 0 :(得分:0)

尝试以下代码

public class TransactionListFragment extends Fragment {

DatabaseHelper db;
SimpleCursorAdapter adapter;
ListView listContent;
Cursor cursor;
private Context mContext;
TransactionListAdapter transactionListAdapter;
String table;
ArrayList<String> listTime;
ArrayList<String> listMemo;
ArrayList<String> listCredit;
ArrayList<String> listDebit;


public TransactionListFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.transaction_list_fragment, container, false);
    table = getActivity().getIntent().getExtras().getString("table");

    listContent = (ListView) rootView.findViewById(R.id.list);
    mContext = getActivity();
    db = new DatabaseHelper(mContext);

    listTime = new ArrayList<String>();
    listMemo = new ArrayList<String>();
    listCredit = new ArrayList<String>();
    listDebit = new ArrayList<String>();

    cursor = db.getTransactions(table);     
    cursor.moveToFirst();
    while(cursor.moveToNext()){
        listTime.add(cursor.getString(cursor.getColumnIndex("timestamp")));
        listMemo.add(cursor.getString(cursor.getColumnIndex("memo")));
        listCredit.add(cursor.getString(cursor.getColumnIndex("credit")));
        listDebit.add(cursor.getString(cursor.getColumnIndex("debit")));
    }

    transactionListAdapter = new TransactionListAdapter(mContext, listTime, listMemo, listCredit, listDebit, table);

    listContent.setAdapter(transactionListAdapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                if(view.getId() = view.findeViewById(ID FOR ivDelete IMAGEVIEW) ){
                    db.deleteTransaction(table, id);
                    cursor = db.getTransactions(table);     
                    cursor.moveToFirst();
                    while(cursor.moveToNext()){
                        listTime.add(cursor.getString(cursor.getColumnIndex("timestamp")));
                        listMemo.add(cursor.getString(cursor.getColumnIndex("memo")));
                        listCredit.add(cursor.getString(cursor.getColumnIndex("credit")));
                        listDebit.add(cursor.getString(cursor.getColumnIndex("debit")));
                    }
                    /*
                    transactionListAdapter = new TransactionListAdapter(mContext, listTime, listMemo, listCredit, listDebit, table);
                    listContent.setAdapter(transactionListAdapter);
                    */

                    ( (BaseAdapter) listContent.getAdapter()).notifyDataSetChanged();
                }


            }
    });

    return rootView;
}


public void onActivityCreated() {

    table = getArguments().getString("table");
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public void onSaveInstanceState(Bundle savedState) {
    super.onSaveInstanceState(savedState);

}

}

适配器文件 - &gt;

public class TransactionListAdapter extends BaseAdapter {

private Context mContext;
ArrayList<String> listTime;
ArrayList<String> listMemo;
ArrayList<String> listCredit;
ArrayList<String> listDebit;
String table;

public TransactionListAdapter(Context context, ArrayList<String> listTime, ArrayList<String> listMemo, 
        ArrayList<String> listCredit,ArrayList<String> listDebit, String tab) {

    super();
    mContext = context;
    this.listTime = listTime;
    this.listMemo = listMemo;
    this.listCredit = listCredit;
    this.listDebit = listDebit;
    table = tab;
}

public View getView(final int position, View view, ViewGroup parent) {
    ViewHolder holder;

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // SMOOTH SCROLLING
    if(view == null) {
        view = inflater.inflate(R.layout.rowlayout, null);

        holder = new ViewHolder();
        holder.tvDate = (TextView) view.findViewById(R.id.tvDate);
        holder.tvMemo = (TextView) view.findViewById(R.id.tvMemo);
        holder.tvAmount = (TextView) view.findViewById(R.id.tvAmount);
        holder.ivDelete = (ImageView) view.findViewById(R.id.ivDelete);

        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    // MOVE CURSOR TO REQUIRED POSITION
    cursor.moveToPosition(position);

    final int id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id")));

    // FETCH THE INFORMATION FOR EACH VIEW
    String transactionDate = listTime.get(position)
    String transactionMemo = listMemo.get(position)
    String transactionCredit = listCredit.get(position)
    String transactionDebit = listDebit.get(position)

    holder.tvDate.setText(transactionDate);
    holder.tvMemo.setText(transactionMemo);
    if (transactionCredit == null) {
        holder.tvAmount.setTextColor(Color.RED);
        holder.tvAmount.setText("(" + transactionDebit + ")");
    } else {
        holder.tvAmount.setTextColor(Color.BLACK);
        holder.tvAmount.setText(transactionCredit);
    }

    holder.ivDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db.deleteTransaction(table, id);
            notifyDataSetChanged();

        }

      });
    return view;

}

public void setTable(String tab) {
    //table = tab;

    //Log.d("adapter", table);
}

public static class ViewHolder {
    public TextView tvDate;
    public TextView tvMemo;
    public TextView tvAmount;
    public ImageView ivDelete;
}

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

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

答案 1 :(得分:0)

在列表适配器中,我实际上最终执行了以下操作:

holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder((Activity) v.getContext());
                alertDialog.setMessage("Are you sure you want to delete?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id){
                                db.deleteTransaction(table, deleteId);
                                cursor = db.getTransactions(table);
                                notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int id){
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = alertDialog.create();
                alert.show();

这似乎没问题!