Android ListView和Fragment

时间:2017-07-15 22:17:53

标签: android listview android-fragments

具体问题是无法调用自定义arrayadapter的getView方法。 我使用片段来显示列表。产生列表的Activity允许从微调器中选择一个人。然后,我使用所选人员的ID从DB检索数据。我将此数据打包在一个自定义持有者对象中,该对象分配给ArrayList。以下是创建片段事务的活动代码。

FragmentTransaction fragmentTransaction = 
    getSupportFragmentManager().beginTransaction();

NotesListViewFragment notesListViewFragment = new NotesListViewFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(Constants.NOTES_LIST, (ArrayList) rowValues);
notesListViewFragment.setArguments(bundle);
fragmentTransaction.add(notesListViewFragment, "FRAG");
fragmentTransaction.commit();

片段正确获取捆绑数据。 onCreate方法正确执行,onCreateView方法也是如此。以下是onCreateView代码:

View view =  inflater.inflate(R.layout.note_list_fragment_layout, null);
    ListView notesListView = (ListView) 
view.findViewById(android.R.id.list);
    //ListView notesListView = (ListView) view.findViewById(android:list);

notesListView.addHeaderView(inflater.inflate(R.layout.notes_list_headings, 
    container, false));
View footer = ((LayoutInflater) context.getSystemService
            (Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.notes_list_footer, null);
TextView v = (TextView) footer.findViewById(R.id.total_notes);
v.setText(Integer.toString(notesList.size()));
v.setTypeface(font);
footer.setPadding(30, 25, 0, 0);
notesListView.addFooterView(footer);

CustomNotesListAdapter adapter = new CustomNotesListAdapter(context,
            R.layout.notes_list_row_layout, notesList);
            //R.layout.notes_list_row_layout, notesList, false);
adapter.notifyDataSetChanged();
notesListView.setAdapter(adapter);
//setListAdapter(adapter);

return view;

创建适配器后,它包含'notesList'中的数据。 调用适配器构造函数和getCount方法,返回正确的行数。永远不会调用getView方法,并且设备上不会显示任何内容。这是getView方法。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
NoteListHolder noteListHolder = getItem(position);

if (convertView == null) {
    //LayoutInflater inflater = (LayoutInflater) context
                //.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = ((LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(layoutResourceId, null);
    viewHolder = new ViewHolder();
        viewHolder.imageView = (ImageView) 
    convertView.findViewById(R.id.note_image);
        viewHolder.noteText = (TextView) 
  convertView.findViewById(R.id.note_text);
        viewHolder.updatedDate = (TextView) 
convertView.findViewById(R.id.updated_date);
        viewHolder.updatedBy = (TextView) 
convertView.findViewById(R.id.updated_date);
        viewHolder.noteSource = (TextView) 
convertView.findViewById(R.id.note_source);
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.noteText.setText(noteListHolder.getNote());
    viewHolder.noteSource.setText(noteListHolder.getNoteSource());
    viewHolder.updatedDate.setText(noteListHolder.getLastUpdated());
    viewHolder.updatedBy.setText(noteListHolder.getLastUpdatedBy());

    return convertView;

这是ListView xml布局:

<ListView
    android:id="@+id/android:list"
    style="@style/MyMaterialTheme.Base"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:divider="@android:color/black"
    android:dividerHeight="5dp"
    android:drawSelectorOnTop="true" >

我在应用程序中有五六个类似的场景,都正常工作。唯一的区别是使用Fragment来包含List。

希望有人有耐心阅读本文,并感谢任何想法。

1 个答案:

答案 0 :(得分:0)

if(convertView) {
...
}

您想要扩充layoutResourceId

LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(layoutResourceId, null, true);
...
return rowView;