Delete an element from my ListView

0

I need to delete an element from my ListView and I'm having trouble with my code

 Host = new String[] { "Host1 ", "Host2", "Host3"};
    Descricao = new String[] { "Estado do Host", "Estado do Host", "Host" };
    Status = new String[]{"DOWN","UNREACHABLE","DOWN"};

    adapter = new ListViewAdpterInicial(getActivity().getBaseContext(),Host,Descricao,Status);
    list.setAdapter(adapter);

listviewAdpter Start

public class ListViewAdpterInicial extends BaseAdapter {

Context context;
String[] Host;
String[] Descricao;
String[] Status;

public ListViewAdpterInicial(Context context, String[] host, String[] descricao, String[] status) {
    this.context = context;
    this.Host = host;
    this.Descricao = descricao;
    this.Status = status;
}

@Override
public int getCount() {
    return Host.length;
}

@Override
public Object getItem(int position) {
    return null;
}

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

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

    TextView txtHost;
    TextView txtDescricao;
    TextView txtStatus;

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.texto_inicial_host, parent, false);


    txtHost = (TextView) itemView.findViewById(R.id.txt_host_inicial);
    txtDescricao = (TextView) itemView.findViewById(R.id.txt_host_descricao);
    txtStatus = (TextView)itemView.findViewById(R.id.txt_host_status);


    txtHost.setText(Host[position]);
    txtDescricao.setText(Descricao[position]);
    txtStatus.setText(Status[position]);

    return itemView;
}}

How would I do to ex: delete Element 2 from this List

    
asked by anonymous 30.07.2014 / 18:47

1 answer

1

To make the application more readable, I suggest that you create an object of type Host.

public class Host {

    private String mNome;
    private String mDescricao;
    private String mStatus;

    private void setNome(String nome) {
        mNome = nome;
    }

    private String getNome() {
        return mNome;
    }

    private void setDescricao(String descricao) {
        mDescricao = descricao;
    }

    private String getDescricao() {
        return mDescricao;
    }

    private void setStatus(String status) {
        mStatus = status;
    }

    private String getStatus() {
        return mStatus;
    }
}

Then you will modify your Adapter to receive a list of objects with type Host.

public class ListViewAdpterInicial extends BaseAdapter {

    Context context;
    List<Host> hostsList;

    public ListViewAdpterInicial(Context context) {
        this.context = context;
    }

    public void setItems(List<Host> hostsAux) {
        hostsList = hostsAux;
    }

    @Override
    public int getCount() {
        return hostsList.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView txtHost;
        TextView txtDescricao;
        TextView txtStatus;

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.texto_inicial_host, parent, false);

        txtHost = (TextView) itemView.findViewById(R.id.txt_host_inicial);
        txtDescricao = (TextView) itemView.findViewById(R.id.txt_host_descricao);
        txtStatus = (TextView)itemView.findViewById(R.id.txt_host_status);

        Host host = hostsList.get(position);

        txtHost.setText(host.getName());
        txtDescricao.setText(host.getDescricao());
        txtStatus.setText(host.getStatus());

        return itemView;
    }
}

Then in your class, you generate a list (just an example) of the type of that object and pass it to the Adapter.

List<Host> hostsList = new ArrayList<Host>();

Host host = new Host();
host.setNome("Host1");
host.setDescricao("Estado do Host");
host.setStatus("DOWN");

hostsList.add(host);

host.setNome("Host2");
host.setDescricao("Estado do Host");
host.setStatus("UNREACHABLE");

hostsList.add(host);

host.setNome("Host3");
host.setDescricao("Host");
host.setStatus("DOWN");

hostsList.add(host);

adapter = new ListViewAdpterInicial(getActivity().getBaseContext());
list.setAdapter(adapter);

adapter.setItems(hostsList);

Finally you should let your Adapter know you have new items and it should update itself.

adapter.notifyDataSetChanged();

When you want to update the Adapter list, simply generate a new one (with only the 2 items you want), do the setItems again and then the notifyDataSetChanged;

    
30.07.2014 / 19:16