Refresh ListView in ImageButton.onClick inside get

2019-07-27 11:40发布

问题:

I have an activity that is generated by a custom BaseAdapter and look like this:

This is the getView off the custom BaseAdapter:

    @Override
public View getView(int position, View view, ViewGroup parent) {
    final Visita visita = getItem(position);

    view = mInflater.inflate(R.layout.visita_item, null); //The ListView Item

    ImageButton btnUp, btnDown;
    btnUp = (ImageButton)view.findViewById(R.id.visita_btn_move_up);
    btnDown = (ImageButton)view.findViewById(R.id.visita_btn_move_down);

    btnUp = (ImageButton) view.findViewById(R.id.visita_btn_move_up);
    if (position != 0) {// First item can't have the button up
        btnUp.setBackgroundResource(R.drawable.ic_arrow_up);
    }

    btnDown = (ImageButton) view.findViewById(R.id.visita_btn_move_down);
    if (position != visitas.size() - 1) {// Last item can't have the button down
        btnDown.setBackgroundResource(R.drawable.ic_arrow_down);
    }

    final DatabaseAdapter adapter = new DatabaseAdapter(context);

    btnUp.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d(TAG, "Moving UP");

            ContentValues values = new ContentValues();
            valores.put(DatabaseHelper.VISITA_COLS[5], visita.order - 1);
            String where = "ordem=?";
            String[] whereArgs = {String.valueOf(visita.order)};
            Log.d(TAG, "ID: " + adapter.atualizar(valores, where, whereArgs, DatabaseHelper.TB_VISITA));

                  // I should refresh the ListView now
        }
    });

    btnDown.setOnClickListener(new OnClickListener( ) {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d(TAG, "Movind DOWN");

             //Here i should need the same, update the order in database and after refresh the ListView
        }
    });

    return view;
}

Basically, the onClick off btnUp and btnDown are self explanatory. The order need to be saved in database too because it will be sent after. The update in dastabase works fine, my problem is: How can I refresh the ListView inside his own BaseAdapter?

I checked this question but without success.

回答1:

I've used BroadcastReceiver to refresh the ListView like this