[英]Displaying list items in ListView
I've got the following main.xml: 我有以下main.xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:id="@+id/listview"
android:layout_width="315px"
android:layout_height="379px"
android:layout_x="2px"
android:layout_y="50px"
>
</ListView>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select device:"
android:textSize="17sp"
android:layout_x="-2px"
android:layout_y="1px"
>
</TextView>
<Button
android:id="@+id/refresh"
android:layout_width="109px"
android:layout_height="wrap_content"
android:text="Refresh"
android:layout_x="209px"
android:layout_y="7px"
>
</Button>
</AbsoluteLayout>
The main class does nothing but drawing the layout. 主类只绘制布局。
I've got the following class and layout for the listitems: 我为列表项提供了以下类和布局:
<?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" >
</TextView>
package com.android.bluetoothp2p;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class BTListView extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
Countries is just an array with a list of countries, some dummy values. 国家只是一个包含国家列表和一些虚拟值的数组。
Now, how do I make the list items of the BTListView class go in the ListView (with @+id/listview) of the main class? 现在,如何使BTListView类的列表项进入主类的ListView(带有@ + id / listview)中?
EDIT: Now my list works, but it isn't clickable anymore. 编辑:现在我的列表有效,但它不再可单击。 This is the code: 这是代码:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "lolwat", Toast.LENGTH_SHORT).show();
}
});
You have to give the ID @android:id/list to your ListView in your layout - see http://developer.android.com/reference/android/app/ListActivity.html for details. 您必须将ID @android:id / list赋予布局中的ListView-有关详细信息,请参见http://developer.android.com/reference/android/app/ListActivity.html 。 This ID is used by the ListActivity class to find the ListView instance. ListActivity类使用此ID查找ListView实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.