我有以下 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>
主类除了绘制布局什么都不做。
我有以下列表项的类和布局:
<?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();
}
});
}
Country 只是一个包含国家列表和一些虚拟值的数组。
现在,如何使 BTListView 类的列表项进入主类的 ListView(带有@+id/listview)?
编辑:现在我的列表有效,但它不再可点击。这是代码:
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();
}
});