问题描述
- android Fragment 使用adapter时getView 方法不执行,在Activity下可以执行
-
在网上也看了很多相关文章了,还是没有解决,在Activity下使用此代码没有任何问题,数据可以显示,但是用了Fragment adapter的getView方法就一直不执行。
我觉得他们之间唯一的区别的就是context 和view 的不同,请大神指教代码如下
public class BookSearchActivity extends Fragment {
private BookSearchService service; private List<Books> list; private String result; private BookSearchAdapter adapter; private Handler mHandler; private ListView listView; private View v ; private Context context; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { context= getActivity(); v = inflater.inflate(R.layout.booksearch, container, false); return inflater.inflate(R.layout.booksearch, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mHandler = new Handler(); new Thread(new Runnable() { @Override public void run() { service = new BookSearchService(); list = service.queryBookList("", ""); if (list == null) result = ApplConst.FAILURE; else result = ApplConst.SUCCESS; adapter = new BookSearchAdapter(list,context); mHandler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub listView = ((ListView) v.findViewById(R.id.books_list_view)); listView.setAdapter(adapter); listView.setDividerHeight(1); // 不显示分割线 listView.setCacheColorHint(Color.TRANSPARENT); //防止listview背景变黑 listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent newActivity = new Intent(view.getContext(), BookSearchActivity.class); Books book = (Books) listView.getItemAtPosition(position); newActivity.putExtra("BookId", book.getId()); newActivity.putExtra("categoryId", book.getCategoryId()); startActivity(newActivity); } }); } }); } }).start(); }
}
///////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
Adapterpublic class BookSearchAdapter extends BaseAdapter {
private int resourceId; private List<Books> booksList; private Map<Integer, View> rowViews = new HashMap<Integer, View>(); private Context context = null; public BookSearchAdapter(List<Books> objects, Context context) { booksList = objects; this.context = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = rowViews.get(position); if (rowView == null) { SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); LayoutInflater layoutInflater = LayoutInflater.from(context); rowView = layoutInflater.inflate(R.layout.booksearch_items, null); TextView publicationDate = (TextView) rowView .findViewById(R.id.dtPublicationDate); TextView bookName = (TextView) rowView .findViewById(R.id.txtBookName); TextView publish = (TextView) rowView .findViewById(R.id.TxtPublish); TextView writor = (TextView) rowView .findViewById(R.id.txtWritor); Books books = booksList.get(position); if (books.publicationDate != null) publicationDate.setText("出版时间:" + DateUtil.parseDateToString( books.getPublicationDate(), df)); if(books.getBookName()!=null) bookName.setText(books.getBookName()); if(books.getWritor()!=null) writor.setText("编者:"+books.getWritor()); if(books.getBookName()!=null) publish.setText("出版社:"+books.getPublish()); rowViews.put(position, rowView); } return rowView; }
解决方案
你的问题解决了么,我也遇到了同样的问题
时间: 2024-10-07 10:42:14