适配器的addItem没有在功能
问题描述:
public class TabBooklist extends Fragment {
/* <DB> */
//variable use in DB->
int nCount=1;
SQLiteDatabase db;
BookListDBHelper helper;
ListView listview ;
BookListShowAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_booklist, container, false);
/* (XML) <Book list> */
adapter = new BookListShowAdapter(getContext()) ;
listview = (ListView) rootView.findViewById (R.id.lv_book_list);
listview.setAdapter(adapter);
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name1", "author1", 110, 1200) ; //work
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name21", "author21", 210, 1003) ; //work
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name31", "author31", 101, 1020) ; //work
//refresh
Button btn_ref = (Button) rootView.findViewById(R.id.btn_refresh);
btn_ref.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //click to add
select();
}
});
return rootView;
}
//select
public void select() {
nCount = 1;
db = helper.getReadableDatabase();
Cursor c = db.query("booklist", null, null, null, null, null, null);
while (c.moveToNext()) {
int int_idBook = c.getInt(c.getColumnIndex("id_book"));
int int_idTree = c.getInt(c.getColumnIndex("id_tree"));
String str_title = c.getString(c.getColumnIndex("title"));
String str_author = c.getString(c.getColumnIndex("author"));
String str_imageLink = c.getString(c.getColumnIndex("image_link"));
int n_pageTotal = c.getInt(c.getColumnIndex("page_total"));
//add
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
str_title, str_author, 0, n_pageTotal); //not work
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name31", "author31", 101, 1020) ; //not work
nCount ++;
}
}
}
我想要当如果单击刷新按钮然后添加项目到列表视图工作。adapter.addItem
in onCreateView function
is work。
但是在select function
,adapter.addItem是不工作。
(并且没有错误。)
数据在数据库中。 (我用日志检查过)
为什么adapter.addItem
在select function
是不能工作什么是解决方案?适配器的addItem没有在功能
答
之所以
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name1", "author1", 110, 1200) ; //work
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name21", "author21", 210, 1003) ; //work
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.ic_empty),
"name31", "author31", 101, 1020) ; //work
作品里面onCreateView()是因为onCreateView()运行你的观点被渲染之前,允许ListView控件呈现的项目。
然而,在你选择()方法要添加后ListView控件已经呈现的项目。
因为我不知道你BookListShowAdapter.addItem()方法是什么样子,我只能假设,你不叫notifyDataSetChanged()。如果您在BookListShowAdapter.addItem()方法末尾调用此方法,则适配器会告诉任何反映您的数据以自我刷新(包括呈现)的视图。
如果你把一个虚拟物品放进去,什么都不做? – Mercato