English 中文(简体)
围绕案文撰写
原标题:Wrapping a LinearLayout around a TextView
  • 时间:2011-11-23 00:04:44
  •  标签:
  • java
  • android

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

不从事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。

最佳回答

如果你想要使用更为复杂的布局,那么你就必须将你用于ArrayAdapter的构造改成一种既使用排版,又使用文本电文的索引添加案文。 当您的布局只是一个单独的<代码>TextView时,你就能够放弃另一个版本,因为你只需要提及一件事;但现在,你必须告诉适应者,布局是孤立的,而且新布局内有这种看法。

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

问题回答

如果你对ArrayAdapter的文件进行核对,就会发现,施工者是第二参数,是案文“ResourceId”,因此不使用第一个xml坠毁。

由此可见,案文意见是观点。

如果你想利用自己的布局,你必须写出一个习俗适应者。

希望这将有助于!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签