Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview with multiple strings

Tags:

java

android

I am trying to create a Listview with multiple strings.

Right now I have a function that it will do

       while(i <= 10){
           //GETS DATA FROM WEBPAGE ETC
           a = DATAFROMWEBPAGE1;
           b = DATAFROMWEBPAGE2;
           c = DATAFROMWEBPAGE3
       }

10 times with 10 different sections from the webpage and I wanted to put this in a list view with 3 textviews showing a, b, c. But I'm having a very hard time doing so.

like image 223
arberb Avatar asked Mar 17 '26 14:03

arberb


2 Answers

You can write a custom ListAdapter to do this:

MyAdapter adapter = new MyAdapter (this);
for (int i = 0; i < 10; i++) {
    adapter.addAdapterItem(new AdapterItem("first", "second", "third");
}
view.setListAdapter(adapter);

class AdapterItem:

public class AdapterItem {
  public String first;
  pubilc String second;
  public String third;

  public AdapterItem(String first, String second, String third) {
    this.first = first;
    this.second = second;
    this.third = third;
  }
}

class MyAdapter:

public class MyAdapter extends BaseAdapter {

private List<AdapterItem> items;

public void addAdapterItem(AdapterItem item) {
  items.add(item); 
}

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

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

public View getView(int position, View convertView, ViewGroup parent) 
  View rowView = getLayoutInflater().inflate(R.layout.rowLayout);
  TextView firstTextView = (TextView) rowView.findViewById(R.id.firstView);
  firstTextView.setText(items.get(postion).first);
  // do the same with second and third
  return rowView;
}

Layout:

<LinearLayout android:Layout_width=".." android:layout_height="..">
  <TextView android:id="firstView" />
  <TextView android:id="secondView" />
  <TextView android:id="thirdView" />
</LinearLayout>

The code is not complete, just to give you the idea:

-You create a new ListAdapter that handles your items

  • You can store your items in an extra class (AdapaterItem in this example) inside your adapter
  • Your ListAdapter implementation has to provide common methods like getCount() and getItem()
  • Within the getView(..) method you can return your custom view for each row. If you want to display 3 different text messages you have to define 3 Textviews in your layout and assign the strings in getView(..)
like image 185
micha Avatar answered Mar 19 '26 02:03

micha


First, create a layout that organizes the views you want to appear in a single row. (See here for an example that groups two strings and an image.) Let's say you save this in a layout file called row_layout.xml and it has three TextViews with ids t1, t2, and t3.

Second, decide what data structure you are going to use for your data. We'll suppose it's an array of arrays of String, where each element contains the three strings you want to display in each row.

Third, you need to create a custom list adapter. If you're using the above array data structure, it might look something like this (adapted from here):

public class MySimpleArrayAdapter extends ArrayAdapter<String[]> {
private final Context context;
private final String[][] values;

public MySimpleArrayAdapter(Context context, String[][] values) {
    super(context, R.layout.row_layout, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row_layout, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.t1);
    TextView textView2 = (TextView) rowView.findViewById(R.id.t2);
    TextView textView3 = (TextView) rowView.findViewById(R.id.t3);
    textView1.setText(values[position][0]);
    textView2.setText(values[position][3]);
    textView3.setText(values[position][2]);

    return rowView;
}
}

Finally, in your ListActivity, add this in onCreate:

setListAdapter(new MySimpleArrayAdapter(this, values));

where values is the array of string arrays that you want displayed.

like image 41
Ted Hopp Avatar answered Mar 19 '26 02:03

Ted Hopp