简体   繁体   中英

Wrapping a LinearLayout around a TextView

I have the following code that populates a ListView with a TextView in the XML Layout file. But if I try to wrap a LinearLayout around the TextView it crashes!

Code:

setListAdapter(new ArrayAdapter<MyData>(getApplicationContext(), R.layout.articlelist, items));        
ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        MyData d = items[position];
        Bundle bundle = new Bundle();
        bundle.putString("theArticleText", d.getText());
        Intent newIntent = new Intent(getApplicationContext(), ArticleDetail.class);
        newIntent.putExtras(bundle);
        startActivityForResult(newIntent, 0);
    }
});

Working XML:

<?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" />

Non working XML:

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

Any ideas why this is happening??? Also what I want to do is add an icon next to each item in the list, am I heading in the right direction???

Cheers,

Mike.

If you check the documentation for the ArrayAdapter you'll see that the constructor gets as second parameter a textViewResourceId, that's why it does not crash using the first xml.

By the way keep in mind that a TextView is a view, a LinearLayout is a ViewGroup.

If you want to use your own layout you must write a custom adapter.

Hope it will help!

If you want to use the more complex layout, you will have to change the constructor you use for the ArrayAdapter to a version that takes both the ID of a layout AND the ID of the TextView to insert the text. When your layout is just a single TextView , you can get away with the other version because you only need to reference one thing; but now you have to tell the adapter both the layout to inflate and which view inside the new layout to use.

You will also need to modify your layout so the TextView has a valid ID to reference:

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

Notice the android:id parameter attached to the TextView . Then in your Java code use this constructor:

new ArrayAdapter<MyData>(getApplicationContext(), R.layout.articlelist, R.id.textview, items)

This now properly tells the adapter where to place the string data is obtains from each item.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM