我正在尝试创建一个显示来自SQLite数据库的书籍对象的Android应用程序。我正在尝试使用onItemClick为游标适配器创建一个on click侦听器。当用户点击列表的特定行时,我试图让图书的价格出现。但是我无法使用正确的语法来显示它。
这是我的BookListAdapter类
public class BookListFragment extends ListFragment {
private Bookstore mBookstore;
private List<Book> mBooks;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver resolver = this.getActivity().getContentResolver();
Uri uri = Uri.parse("content://com.bookstore.app.model.BookContentProvider/books");
Cursor cursor = resolver.query(uri, null, null, null, null);
String[] from = new String[] {
"title", "author", "year"
};
int [] to = new int[] {
R.id.list_item_book_title_textView,
R.id.list_item_book_author_textView,
R.id.list_item_book_year_textView
};
// CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DATABASE
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this.getActivity(), R.layout.list_item_book, cursor, from, to, 0);
setListAdapter(adapter);
Activity activity = getActivity();
mBookstore = Bookstore.getInstance(activity);
mBooks = mBookstore.getBooks();
ListAdapter adapter1 = new BookListAdapter(activity, mBooks);
setListAdapter(adapter1);
}
}
这是我的BooklistAdapter类
public class BookListAdapter extends ArrayAdapter<Book> implements ListAdapter {
private Context mContext;
private List<Book> mBooks;
public BookListAdapter(Context context, List<Book> books) {
super(context, R.layout.list_item_book, books);
mContext = context;
mBooks = books;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// create a new list item view
Activity activity = (Activity)mContext;
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item_book, null);
}
// customize layout for book
Book bk = mBooks.get(position);
TextView titleView = (TextView)convertView.findViewById(R.id.list_item_book_title_textView);
TextView authorView = (TextView)convertView.findViewById(R.id.list_item_book_author_textView);
TextView PriceView = (TextView)convertView.findViewById(R.id.list_item_book_price_textView);
TextView yearView = (TextView)convertView.findViewById(R.id.list_item_book_year_textView);
titleView.setText(bk.getTitle());
authorView.setText(bk.getAuthor());
yearView.setText("" + bk.getYear());
return convertView;
}
}
我不确定在哪里放一些像
这样的东西 public void onItemClick(AdapterView<?> parent, View v, final int position)
{
Cursor cursor = (Cursor) adapter.getItem(position);
yearView.setText("" + bk.getYear());
}
以显示光标所针对的该行的特定价格。 非常感谢任何帮助。
如果有任何混淆,请发表评论,我会澄清。