Jamie Osborn
Jamie Osborn

Reputation: 3

Getting text from custom ListView

As the title says really.. I have a list view with 3 text view's in it, see below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textColor="#000"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textSize="12sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textStyle="italic"
        android:typeface="sans" />

</LinearLayout>

This is how I call it.

public void displayResultList(ArrayList<HashMap<String, String>> results2) {
    // TODO Auto-generated method stub

    lv = (ListView) findViewById(android.R.id.list);
    SimpleAdapter adapter = new SimpleAdapter(this, results2,
            R.layout.custom_row_view,
            new String[] { "Ear", "Breed", "Sex" }, new int[] { R.id.text1,
                    R.id.text2, R.id.text3 });

    lv.setAdapter(adapter);

}

and this is how I try to retrieve the text, but this doesn't work..

protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        String item = (String) ((TextView) v).getText();

        Toast toast3 = Toast.makeText(this, "items = " + item,
                Toast.LENGTH_SHORT);
        toast3.show();

        // Intent mainIntent3 = new Intent(this,CowDetailsActivity.class);
        // mainIntent3.putExtra("cow", item.substring(10, 16));
        // startActivity(mainIntent3);

    }

Can anybody tell me how I can get the first line of text?

Thanks in advance

Upvotes: 0

Views: 251

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

Can anybody tell me how I can get the first line of text?

((HashMap<String, String>)l.getItemAtPosition(position) will give you your HashMap. I don't know what "the first line of text" is.

Or, if you still have results2 held somewhere, results2.get(position) will also return to you your HashMap.

Upvotes: 3

Related Questions