我有一个自定义游标适配器CAdapter,其代码如下:
public class CAdapter extends CursorAdapter {
public CAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView titleTV = (TextView) view.findViewById(R.id.listTitle);
titleTV.setText("#"+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))+" "+
cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row_item_layout, parent, false);
return retView;
}
}
row_item_layout.xml文件是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/listTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/heartIV"
android:focusable="false"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="26sp"
android:paddingBottom="3dp"/>
<ImageView
android:id="@+id/heartIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/listTitle"
android:src="@drawable/heart"
android:paddingRight="5dp"
android:paddingLeft="2dp"/>
</RelativeLayout>
活动FavListActivity显示列表。它的布局文件是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xkcdreader.FavListActivity"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@+id/favL"
android:layout_width="match_parent"
android:layout_height="452dp"
android:layout_alignParentBottom="true"
android:layout_below="@+id/headingTV" >
</ListView>
<TextView
android:id="@+id/headingTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:text="Favourites"
android:textSize="25dp" />
</RelativeLayout>
以下是FavListActivity
的一段代码listView = (ListView) findViewById(R.id.favL);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("App", "clicked on item: " + position);
TextView listTV=(TextView) view.findViewById(R.id.titleTV);
Log.d("App",listTV.getText().toString());
}
});
在运行它时,我在Log.d(“App”,listTV.getText()。toString())行获得NullPointerException;该列表没有任何null textview。我无法弄清楚这个问题。请帮忙
答案 0 :(得分:2)
你应该使用getItemAtPosition
:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor mycursor = (Cursor)parent.getItemAtPosition(position);
}
});
并使用myCursor
访问您的数据。
答案 1 :(得分:0)
TextView
中row_item_layou.xml
的{{1}}代码为titleTV
。因此,当您初始化时,最终会获得NUllPointerException
。
更改此
TextView listTV=(TextView) view.findViewById(R.id.titleTV);
到
TextView listTV=(TextView) view.findViewById(R.id.listTitle);
因为你有
<TextView
android:id="@+id/listTitle" // id is listTitle
虽然您的NPE可以如上解决,但最好使用blackbet的答案
Cursor mycursor = (Cursor)parent.getItemAtPosition(position);
然后使用mycursor
获取字符串。
答案 2 :(得分:0)
试试这个..
您的TextView
ID为listTitle
,但您已提及titleTV
更改并尝试使用。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("App", "clicked on item: " + position);
TextView listTV=(TextView) view.findViewById(R.id.listTitle);
Log.d("App",listTV.getText().toString());
}
});