How to customize the row of a ListVIew according to the value in the ArrayAdapter

2

Assuming that I have an object called Item, and that it has text, id, and validation properties, in this case, text is a string, id is an integer, and validation is a Boolean value.

class Item
{
    private int id;
    private String texto;
    private boolean validacao;
    //get and set
}

And let this object serve as a parameter for an ArrayAdapter

ArrayAdapter<Item> itemAdapter = new ArrayAdapter<Item(this,R.layout.layoutItem,view)
//no momento os parametros do construtor do ArrayAdapter não são levados em conta 

But I need to be able to customize some elements of each row in the ListView according to the items.

Each line will have:

1- an image, which will be chosen according to the validation value of the Item of the current line.

2- An EditText, which will receive and manipulate the text property value of the Item of the current line.

4- an image with the drawing of a recycle bin that when clicked deletes the current item.

5- The item id should be changed according to the position in the array.

How can I customize the rows of the view in this way ?, should I create a class that extends the ArrayAdapter class?, have some examples ?, or an alternative, or standard, best for this need?

    
asked by anonymous 25.09.2014 / 15:03

1 answer

3

I'll show you an example using with Custom Adapter using TextView and a Button, resulting in the image below and you want to insert 50 elements for example:

MainActitvy class that is based on a ListView:

public class MainActivity extends ListActivity {

    private NumeroAdapter adapter;
    private final int numero = 50;

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

        adapter = new NumeroAdapter(this, numero);

        setListAdapter(adapter);

    }
}

NumeroAdapter class that uses the custom BaseAdapter responsible for displaying on the screen:

public class NumeroAdapter extends BaseAdapter implements View.OnClickListener {

    private List<String> array = new ArrayList<String>();
    private LayoutInflater inflater;
    private Context context;


    public NumeroAdapter(Context context, int tamanho) {
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int i = 0; i < tamanho; i++) {
            array.add(String.valueOf(i + 1));
        }
    }

    @Override
    public int getCount() {

        return array.size();
    }

    @Override
    public Object getItem(int position) {

        return array.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

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

        View view = convertView;
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.activity_main, null);
            holder = new ViewHolder();
            holder.texto = (TextView) view.findViewById(R.id.texto);
            holder.info = (Button) view.findViewById(R.id.info);
            holder.info.setOnClickListener(this);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.texto.setText(array.get(position));
        holder.info.setTag(array.get(position));
        return view;

    }

    @Override
    public void onClick(View v) {

        Toast.makeText(context, (String) v.getTag(), Toast.LENGTH_SHORT).show();

    }

}

ViewHolder interface responsible for managing the views that are shown on the screen:

public class ViewHolder {
    public TextView texto;
    public Button info;
}

Layout used by adapter:

<TextView
    android:id="@+id/texto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/info"
    android:layout_marginLeft="10dp"
    android:layout_toLeftOf="@+id/info" />

<Button
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="@string/info" />

This example serves as support for you to be able to solve your problem.

    
25.09.2014 / 17:39