片段中的listView无法正常工作(不显示)

时间:2015-02-06 17:13:03

标签: listview android-fragments

我在片段中有一个listView(ListFragment不适用,因为视图中还有其他控件)。 它运行正常,但列表没有填充。 日志和调试显示适配器的getView被调用,一切都很好。什么都没有显示。

这是片段

public class FoundFragment extends BaseFragment {

private ArrayList<Record> recordsList;
private ListView recordsListView;
private final String LOG_TAG = "FoundFragment";
private RecordsListAdapter recordsListAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}//end onCreate
//----------------------------------------------------------------------------------//
@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    // The last two arguments ensure LayoutParams are inflated properly.
    View rootView = inflater.inflate(
            R.layout.frag_found, container, false);
    return rootView;
}//end onCreateView
//------------------------------------------------------------------------------------//
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    recordsListView = (ListView) getView().findViewById(R.id.foundRecordsListView);

}
//------------------------------------------------------------------------------------//
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new RecordsLoaderTask().execute(false);
}
//------------------------------------------------------------------------------------//
@Override
public void onResume() {
    super.onResume();
    if(recordsListAdapter != null){
    recordsListAdapter.notifyDataSetChanged();
    }
}
/********************************************************************/
@SuppressWarnings("unchecked")
@Override
public void onDataRetrievalSucceeded(Object dataRetrieved) {
    Log.d("FoundFragment", "arrayRetrived with length of " 
                            + ((ArrayList<Record>) dataRetrieved).size());
    recordsList = (ArrayList<Record>) dataRetrieved;
    recordsListAdapter = new RecordsListAdapter(getActivity(), recordsList);
    recordsListView.setAdapter(recordsListAdapter);
    Log.e(LOG_TAG, "adapterOfList is " + recordsListView.getAdapter() + "\n" + 
    "length is " + recordsListView.getAdapter().getCount());
    //TODO check here 
    if(recordsListAdapter != null){
        recordsListAdapter.notifyDataSetChanged();
        }
}//end onDataRetrievalSucceeded
}//end fragment

适配器:

private class RecordsListAdapter extends BaseAdapter{

    private LayoutInflater mInflater;
    private ArrayList<Record> recsList;
    //--------------------------------------------------------------------//        
    public RecordsListAdapter(Context context, ArrayList<Record> records) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        System.out.println("in constructor of adaapter");
        mInflater = LayoutInflater.from(context);
        recsList = records;
    }
    //--------------------------------------------------------------------//
    @Override
    public int getCount() {
        Log.d(LOG_TAG, "categories adapter get size = " + recsList.size());
        return recsList.size();
    }
    //--------------------------------------------------------------------//
    @Override
    public Object getItem(int position) {
        return recsList.get(position);
    }
    //--------------------------------------------------------------------//
    @Override
    public long getItemId(int position) {
        return position;
    }
    //--------------------------------------------------------------------//
     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.records_list_item, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.recordItemNameTxtView);
            holder.age  = (TextView) convertView.findViewById(R.id.recordItemAgeTxtView);
            holder.img = (CircularImageView) convertView.findViewById(R.id.recordItemImgView);

        convertView.setTag(holder);
        }//end if 
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        Record curRecord = recsList.get(position);
        /*setting data from the item into the fields*/;
        holder.name.setText(curRecord.getName());
        holder.age.setText(curRecord.getAge());
        Log.d(LOG_TAG, "curRecord image link is " + curRecord.getThumbURL());
        //TODO image download
        /*if(curRecord.getThumbURL() != null && !curRecord.getThumbURL().equals("")
                && !curRecord.getThumbURL().equals("null")){
            Log.d(LOG_TAG, "in if b4 download image");
            holder.img.setImageBitmap(
                    ImageDownloader.DownloadImage(curRecord.getThumbURL()));

            progressDialog.cancel();
            System.out.println("after image download");
        }//end if --> imag link is available
        else{
            System.out.println("image link is not available");
        }*/
        Log.w(LOG_TAG, "convertView " + 
        convertView + ", " + ((ViewHolder)convertView.getTag()).name.getText().toString());
        return convertView;
    }//end method getView
    ////////////////////////////////////////////////////////////////////////////////////
     class ViewHolder{
        TextView name;
        TextView age;
        CircularImageView img;
    }


}//end class RecordsListAdapter

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#0000ff" >
     <ListView
    android:id="@+id/foundRecordsListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent"
     >
</ListView>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

最有可能发生的事情是,AdapteronActivityCreated()创建({1}}然后会在onViewCreated()中覆盖。我建议您在Adapter创建一个单点,例如onCreateView(),这样就可以立即将其设置为ListView的适配器。然后启动数据下载任务。完成后,它会创建一个新的List数据,并通过自定义RecordsListAdapter.replaceData()方法将其提交给适配器。在该方法内部,您的适配器应交换其支持数据并调用其自己的notifyDataSetChanged(),以便ListView要求它重新填充视图。

答案 1 :(得分:0)

列出项目高度是问题所在。 调整列表项目布局并设置最小高度解决了它。 感谢Omnia Sayed

答案 2 :(得分:-1)

将inflater线更改为

convertView = inflater.inflate(R.layout.records_list_item, null);