0

Wondering how I would code a listview with 2 textviews per row of the list view.

my row is going to have the xml layout like this:

simple_list_item_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/stat_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/stat_number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:layout_below="@id/stat_name"/>


</LinearLayout>

The code I have so far is:

public class PlayerActivity extends Activity {

public Elements name;
public Elements num;
public ArrayList<String> statName = new ArrayList<String>();
public ArrayList<String> statNum = new ArrayList<String>();
private ArrayAdapter<String> nameAdapter;
private ArrayAdapter<String> numAdapter;
private ListView lv;
String url = "http://www.futhead.com/14/players/141/andrea-pirlo/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);

    lv = (ListView) findViewById(R.id.listView);

    new Logo().execute();
    new statistics().execute();
    nameAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.stat_name,statName);
    numAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.stat_number,statNum);

}

private class statistics extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... arg) {
        Document document;
        try {
            document = Jsoup.connect(url).get();
            num = document.select("div#stats-base p");
            statNum.clear();
            for (Element element : num) {
                statNum.add(element.ownText());
            }
            name = document.select("div#stats-base span");
            statName.clear();
            for (Element element : name) {
                statName.add(element.ownText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
    @Override
    protected void onPostExecute(String result) {

        lv.setAdapter(nameAdapter);
        lv.setAdapter(numAdapter);
    }
}

I was using a normal ArrayAdapter at first until I realized that it did not support anymore than one textview so I was wondering how I would implement an Array adapter that could support the two textviews.

2 Answers2

1

To achieve this you have to build a custom adapter and inflate your custom row layout. Using ArrayAdapter won't work because

By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

Go to this for how-to-make-a-custom-arrayadapter-for-listview and i would recommend you to google for some better alternatives to implement others Adapters.

M D
  • 47,665
  • 9
  • 93
  • 114
0

You will have to define a custom Adapter(which will extend the Adapter classes for 'super' implementation) for your ListView. You can specify the layout and assign each component of that custom layout to display data depending on your requirement.

For Adapters to extend: http://developer.android.com/reference/android/widget/Adapter.html

Also, refer these:

http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.learn-android-easily.com/2013/06/listview-with-custom-adapter.html
Displaying ArrayList items in ListView contains 2 textviews

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51