【发布时间】:2015-02-13 09:43:34
【问题描述】:
我想创建一个ListView,其中每个row 都有两个按钮。 Button1 和 Button2。
我已经阅读了一些关于如何创建 CustomAdapter 或扩展 BaseAdapter 的教程,但没有一个对我有用,这就是我在这里提出一个新问题的原因。
我的strings.xml 中有一个静态(不变的)字符串数组:
<string-array name="locations_array">
<item>Item1</item>
<item>Item2</item>
</string-array>
我想在我的 ListView 中使用这个 Array(我设法创建一个带有 onClick 事件的普通 ListView,但现在我想为每一行添加按钮)并向每一行添加两个按钮,每个按钮都有自己的 onClick 事件.
我已经按照this answers here 和网上其他一些教程的步骤进行操作,但在尝试setText 时,我总是得到NullPointerException。
CustomAdapter.java:
public class CustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<>();
private Context ctx;
public CustomAdapter(ArrayList<String> list, Context ctx){
this.list = list;
this.ctx = ctx;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
Log.d("TAG", list.get(position));
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_listview,null);
}
Log.d("TAG", list.get(position));
TextView listItemText = (TextView) view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
Button localData = (Button) view.findViewById(R.id.button1);
Button onlineData = (Button) view.findViewById(R.id.button2);
localData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
onlineData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
}
我在 Fragment 中使用了适配器:
locationsArray = getResources().getStringArray(R.array.locations_array);
ArrayList<String> data = new ArrayList<>(Arrays.asList(locationsArray));
CustomAdapter adapter = new CustomAdapter(data,getActivity());
listView.setAdapter(adapter);
但这不起作用。所有教程都专注于我需要的其他内容。我只需要在每行上放置两个按钮,并使用现有的字符串数组。我不想动态添加任何东西等。只需使用我已经创建的字符串数组。
编辑(添加我忘记的 XML 文件!)
R.layout.layout_listview(EditText是用来过滤List的):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_filter"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Search Location">
</EditText>
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
R.layout.layout_listview_buttons:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Online Data"
android:textColor="#0099CC" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="3dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Local Data"
android:textColor="#0099CC" />
</RelativeLayout>
LogCat 异常:
12-15 11:24:10.852 14626-14626/com.inodroid.myweatherapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.inodroid.myweatherapp, PID: 14626
java.lang.NullPointerException
at com.inodroid.myweatherapp.Data.CustomAdapter.getView(CustomAdapter.java:59)
代码行:
listItemText.setText(list.get(position));
希望有人可以帮助我。
干杯
【问题讨论】:
-
请粘贴您的 R.layout.layout_listview 文件 :-)
-
@Kelevandos 啊,抱歉,不知道我是怎么忘记添加 XML 文件的。我把它们都放了!
-
我认为堆栈跟踪也会很有用
-
我认为您在适配器中膨胀了错误的 xml 文件。将
view = inflater.inflate(R.layout.layout_listview,null);更改为view = inflater.inflate(R.layout.layout_listview_buttons,null); -
就是这样! Oh-My-God..so embarrassing,非常感谢你们!!
标签: java android listview android-listview