Domanda

Sto estendendo Basadapter per creare una riga di Elenco personalizzata. Ho un menu contestuale che si apre ogni volta che un utente tiene sulla riga e richiede se vuole eliminarlo. Tuttavia, come rimuovo la riga? L'hashmap è solo i dati di test.

private MyListAdapter myListAdapter;
private ArrayList<HashMap<String, String>> items;

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

    items = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("date", "10/09/2011");
    map1.put("distance", "309 km");
    map1.put("duration", "1t 45min");
    items.add(map1);

    myListAdapter = new MyListAdapter(this, items);
    setListAdapter(myListAdapter);
    getListView().setOnCreateContextMenuListener(this);
}


private class MyListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<HashMap<String, String>> items;

    public MyListAdapter(Context context, ArrayList<HashMap<String, String>> items) {
        this.context = context;
        this.items = items;
    }

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

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

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

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

        View view = convertView;

        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.row_log, null);
        }

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

        HashMap<String, String> item = items.get(position);
        rowLogOverview.setText(item.get("date"));

        return view;
    }
}
È stato utile?

Soluzione

Non elimini dall'adattatore! Elimini dagli articoli! E l'adattatore è tra i tuoi articoli e la vista. Dalla vista è possibile ottenere la posizione e in base alla posizione è possibile eliminare gli elementi. Quindi l'adattatore ti aggiornerà le viste.

Ciò significa che devi fare qualcosa del genere

 items.remove(position);
adapter.notifyDataSetChanged()

Altri suggerimenti

Per eliminare, dovrai fare 2 cose:

  1. Chiamata .remove() Sul tuo ArrayList (articoli).
  2. Chiamata .notifyDataSetChanged() sull'istanza del tuo MyListAdapter classe (mListAdapter).
  1. Rimuovere l'articolo dagli elementi
  2. chiamata BaseAdapter.notifyDataSetChanged(). Quindi ListView verrà ridisegnato e la riga di destinazione verrà rimossa dalla schermata.

Dovresti aggiungere un ascoltatore sul tuo adattatore per gestire l'evento Elimina.

public YourAdapter(Context context, List<T> rows, View.OnClickListener deleteListener) 
{ ... }

E sul tuo metodo getView () imposta l'ascoltatore

yourBtn.setOnClickListener(this.deleteListener);

È possibile aggiungere un valore sul tag BTN per identificare la riga corrente:

yourBtn.setTag(position);

Infine, sulla tua attività, il tuo ascoltatore spazzerà con la posizione attuale in tag. È quindi possibile utilizzare la risposta precedente per aggiornare l'adattatore e aggiornare la tuaview.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top