android Fragment 使用adapter时getView 方法不执行,在Activity下可以执行

Polly蜀黍 发布于 2014/03/06 12:07
阅读 10K+
收藏 0

在网上也看了很多相关文章了,还是没有解决,在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 ProgressDialog dialog;
	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);
								}
							});
//							if(result.equals(ApplConst.FAILURE))
//								Toast.makeText(MainActivity.this, "数据加载失败,服务器异常",
//										Toast.LENGTH_LONG).show();
//							if(result.equals(ApplConst.NET_EXCEPTION))
//							Toast.makeText(MainActivity.this, "数据加载失败,请检查网络",
//									Toast.LENGTH_LONG).show();
						}
						
					});
					//dialog.dismiss();
				}
			}).start();
  
	    }  
}




///////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
Adapter


public 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🇲🇲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;
	}
	
	@Override
	public int getCount() {
		return booksList.size();
	}

	@Override
	public Object getItem(int position) {
		return booksList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}




加载中
0
TracyZhang
TracyZhang
请用ListFragment
0
Polly蜀黍
Polly蜀黍
谢谢,问题解决了。使用 ListFragment     setListAdapter(adapter)后进入getView了
0
小木兵兵
小木兵兵

listFragment  返回的根布局是什么样的啊?

返回顶部
顶部