How do I delete a specific item from a ListView?

6

I would like to ask a question: I have a ListView , where your adapter is clarified in the same activity and the contents of adapter (the strings ) are in another class, in another package. How do I delete a specific item from ListView , in the same code, without user interaction? (for example, an application update where an item will no longer appear).

In the case I am studying, the user will click on an item of listView , and a text inside the activity will be filled with the string that is in otherClass.lista and depending on the clicked position , a different text will appear.

EDITED

The following click action on the ListView:

ListView listView = (ListView) findViewById(R.id.lista);
listView.setAdapter(new listAdapter(this));

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {

                nome.setText(otherClass.string.get(position));

            }
});

Here's the Adapter I'm using:

public class listAdapter extends BaseAdapter implements ListAdapter {

    public Context context;
    public ArrayList<String> lug;
    public Typeface fonte;

    public listAdapter(Context context) {



        lug = otherClass.string;


        this.context = context;
    }

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

    @Override
    public Object getItem(int position) {
        return lug[position];
    }

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

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

            TextView title = new TextView(context);     
            title.setText(lug.get(position));
            title.setTextSize(18);

        return title;
    }

}

Now follows the Array in the class that is in another package

public class otherClass {

public static ArrayList<String> string = new ArrayList<String>();{
    string.add("primeiro item");
    string.add("Segundo item");
}
} 

The current issue is that the ArrayList items above are not being printed ... and I would like to know (when I can print the items) how to delete the items.

The intention, as the images follow, would be that in otherClass have the items that will appear in listView and clicking on some item, the respective information of the item will be clicked (these declared in the same class so as not to be confused all separated). More items will be added to each application update, so it needs to be a dynamic and organized mode. Ideas? (since Strings does not seem to be a good way)

    
asked by anonymous 21.05.2014 / 17:17

2 answers

3

Come on @GH_ see if that helps.

Create a class called Other, this class will persist the data you want for a given object, let's assume it's a person, car, or anything. In your case will persist two Strings: first item and second item.

/**
 * Classe que irá persistir dados de um objeto para ser usado em uma lista de Strings.
 */
public class Outros {
    private String primeiroItem;
    private String segundoItem;

    public Outros( String primeiroItem, String segundoItem ) {
        this.primeiroItem = primeiroItem;
        this.segundoItem = segundoItem;
    }

    public String getPrimeiroItem() {
        return primeiroItem;
    }

    public void setPrimeiroItem( String primeiroItem ) {
        this.primeiroItem = primeiroItem;
    }

    public String getSegundoItem() {
        return segundoItem;
    }

    public void setSegundoItem( String segundoItem ) {
        this.segundoItem = segundoItem;
    }
}

Your adapter can stay as follows:

public class MeuCustomAdapter extends BaseAdapter {
    /** Lista de objetos outros, contendo duas Strings. */
    private List<Outros> listOutros;
    /** Contexto usado para acessar recursos de string */
    private Context contexto;

    /**
     * Metodo construtor que recebe o contexto e uma lista de objetos instrumentos para popular o ListView personalizado.
     *
     * @param contexto Contexto uasdo para acessar o recurso de layout.
     * @param listOutros  Lista de objetos da representacao de cada item de lista (linha do ListView).
     */
    public MeuCustomAdapter( Context contexto, List<Outros> listOutros ) {
        this.contexto = contexto;
        this.listOutros = listOutros;
    }

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

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

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

    /**
     * Chamado toda vez que e necessario mostrar um item da ListView.
     *
     * @param position    A posicao do item de ListView.
     * @param convertView O elemento View que representa o item de ListView.
     * @param parent      Elemento pai do elemento view.
     */
    @Override
    public View getView( int position, View convertView, ViewGroup parent ) {
        TextView textViewTitle = new TextView( contexto );
        // Aqui é definido como texto do TextView o primeiro item do objeto Outros.
        textViewTitle.setText( listOutros.get( position ).getPrimeiroItem() );
        textViewTitle.setTextSize(18);

        // Para exibir o item você tem que retornar uma VIEW e não uma String.
        return textViewTitle;
    }
}

In Your Activity you do this:

    ListView listView = findViewById( R.id.meuListView );

    Outros outros1 = new Outros( "Outros 1", "Item 1" );
    Outros outros2 = new Outros( "Outros 2", "Item 2" );
    Outros outros3 = new Outros( "Outros 3", "Item 3" );

    List<Outros> listOutros = new ArrayList<Outros>();
    listOutros.add( outros1 );
    listOutros.add( outros2 );
    listOutros.add( outros3 );

    MeuCustomAdapter meuCustomAdapter = new MeuCustomAdapter( getAplicationContext(), listOutros );
    listView.setAdapter( meuCustomAdapter );

I do not know if it will help, you have to give more study in Lists, adapters, default ViewHolder ...

EDITED : And to delete a specific ListView item just do so:

To remove an item from an ArrayList you have the following options:

  • seuArrayList.clear() - Removes all elements from the array leaving it empty;
  • seuArrayList.remove(index) - Removes the object at the specified position in the list;
  • seuArrayList.remove(Object) - Removes the instance of the specified object if it is contained.
  • 23.05.2014 / 05:24
    2

    You can use adapter.remove() , and then call method notifyDataSetChanged() to have the changes reflected in the Listview .

    See:

     
    adapter.remove(adapter.getItem(index)); // Índice do item a ser deletado
    adapter.notifyDataSetChanged();
    
        
    21.05.2014 / 17:36