user974047
user974047

Reputation: 2285

multiple arrays in a listview

For my listview, I want to be able to display multiple textviews within it, with each textview displaying the items of a specific array associated with it. Right now is what I have:

public class GreatestHits extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Array1));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
          String text = (String) ((TextView)view).getText();
}

    });

}
 static final String[] Array1 = new String[] {"Item1", "Item2"};
 static final String[] Array2 = new String[] {"Item1", "Item2"};
static final String[] Array3 = new String[] {"Item1", "Item2"};  

and the xml for R.layout.list_item:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id= "@+id/textView1" 
>

</TextView>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" 
android:id= "@+id/textView2" 
>
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" 
android:id= "@+id/textView3" 
>
</TextView>

How would I put array 2 and array 3 into the code so it can be displayed in the textviews of listview lv?

Upvotes: 1

Views: 2352

Answers (1)

aleph_null
aleph_null

Reputation: 5786

ArrayAdapter can only display data from one array in one textview. You will have to provide this functionality yourself by extending BaseAdapter yourself. I can't compile android code at the moment, so the following code is untested... but it should at least give you an indication of what you can do.

public class TripleArrayAdapter extends BaseAdapter {
  private String[] array1, array2, array3;
  private LayoutInflater inflater;

  public TripleArrayAdapter(Context context, String[] a1, String[] a2, String[]a3) {
    array1 = a1;
    array2 = a2;
    array3 = a3;
    inflater = 
        (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

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

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

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

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

    if(currentView==null) {
      currentView = inflater.inflate(R.layout.list_item, parent, false);
    }

    TextView tView = (TextView)currentView.findViewById(R.id.textView1);
    tView.setText(array1[position]);

    tView = (TextView)currentView.findViewById(R.id.textView2);
    tView.setText(array2[position]);

    tView = (TextView)currentView.findViewById(R.id.textView3);
    tView.setText(array3[position]);

    return currentView;
  }
}

Then, to use this adapter as your ListActivity's adapter, you would just say

setListAdapter(new TripleArrayAdapter(this, Array1, Array2, Array3));

Upvotes: 4

Related Questions