Manually enter a ListView

Is there a way to manually populate a ListView that has more than one column using multiple arrays? I need to create a layout section, and I want to use a ListView, but I need to populate a few View elements with data. Since it does not come from the database, I do not want to use a cursor, I just want to use several parallel arrays. Is it possible?

+4
source share
3 answers

Depending on what I interpreted your problem correctly, this should be possible with an adapter:

You can extend BaseAdaptor by overriding getView () and return some ViewGroup containing widgets representing data from different lists.

Subclassing the ViewGroup to create a complex view that is easier to process:

public class DoubleText extends LinearLayout { TextView t1, t2; public DoubleText(Context c) { super(c); t1 = new TextView(c); t2 = new TextView(c); this.addView(t1, 0); this.addView(t2, 1); } public void setText(String s1, String s2) { t1.setText(s1); t2.setText(s2); } } 

The subclassification of the BaseAdapter, however, not all the necessary data is shown, there is more, but it should be easier to find out, as this is where there are big differences compared to other examples regarding the use of adapters:

 public class DoubleAdaptor extends BaseAdaptor { List<String> lista, listb; // <- these are your paralell arrays, they and the Context c; // context need to be initialized by some method @Override public View getView(int position, View convertView, ViewGroup parent) { DoubleText recycled = (DoubleText) convertView; if (recycled == null) { recycled = new Double(context); recycled.setText(lista.get(position), listb.get(position)); return recycled; } } 

In addition, you do not have to have the same view type for all rows, i.e. override getViewTypeCount () to give the android an upper limit on the number of different kinds of types to use and getItemViewType (int position) to tell android what kind of view to iterate over for that particular row.

Edit: I would recommend checking the Android developer docs on the Adapter (Interface) and BaseAdapter (abstract class) and ListAdapter (interface) interface, also I forgot to tell you that ListView has setAdapter (), which may be of interest.

+1
source

It can be done; the question is how ;-) If you really want to use multiple parallel arrays, then you may have no choice but to subclass BaseAdapter . If you use (or can use) an array of objects, you can make a slightly simpler subclass of ArrayAdapter . In the latter case, I think the only method you need to override is getView .

0
source

Have you tried to use this as your main list adapter? → https://github.com/commonsguy/cwac-thumbnail

CommonsGuy has nice examples about this and is easily customizable to create a ListView with dynamic elements.

0
source

All Articles