我有按钮,点击它会显示一个列表视图,点击列表视图项目它必须在 logcat 上打印一些东西。我以这样一种方式实现了这个,即列表视图及其侦听器存在于按钮 onclick 侦听器中。
除了没有调用 listview 侦听器外,一切都运行良好(即单击 listview 项时,logcat 上没有打印任何内容)
这是我的听众。
public class MyClickListener implements View.OnClickListener {
LinearLayout parent;
LayoutInflater layoutInflater;
Context context;
Home home;
@Override
public void onClick(View view) {
parent = (LinearLayout) home.findViewById(R.id.main_view);
View childLayout = layoutInflater.inflate(R.layout.mylayout, (ViewGroup) view.findViewById(R.id.list_layout));
parent.addView(childLayout, 0);
LinearLayout layout = (LinearLayout) home.findViewById(R.id.main_view);
ListView list = (ListView) layout.findViewById(R.id.list);
list.setAdapter(new MyListAdapter(context));
lsit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("hello Android");
}
});
}
}
我的布局.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:padding="15dp"
android:divider="@android:color/transparent"
android:dividerHeight="5dp"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"/>
</LinearLayout>
主页.java
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button button= (Button) findViewById(R.id.but);
button.setOnClickListener(new MyClickListener(this,this,layoutInflater));
}
}
主页.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@color/background">
...
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="@+id/main_view">
</LinearLayout>
.....
谁能帮忙
编辑 1
MyListAdapter.java
private class MyListAdapter extends BaseAdapter {
String[] items;
LayoutInflater inflater;
Context context;
public MyListAdapter(Context context) {
this.context = context;
Resources resources = context.getResources();
items = new String[]{resources.getString(R.string.q1), resources.getString(R.string.q2), resources.getString(R.string.q3)};
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = view;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view1 = inflater.inflate(R.layout.list_layout_item, null);
TextView question = (TextView) view1.findViewById(R.id.itemtext);
question.setText(items[i]);
return view1;
}
}
list_layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/itemStyle">
<TextView
android:id="@+id/itemtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:drawableRight="@drawable/disclosure_indicator"/>
</LinearLayout>