我正在尝试创建一个用于ArrayAdapter
的自定义ListFragment
。现在ListFragment
中没有显示任何内容,只有TextView
表示它是什么片段编号。我在我的适配器的getView()
方法中放了一个断点,但它没有被调用。我搜索了无法调用的原因,有些人似乎认为我的ListView被隐藏了,所以我尝试将layout_height
更改为wrap_content
,但这不起作用。
此外,我一直在关注此开发人员页面中的代码:http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
ListFragment:
public class HomeFragment extends ListFragment implements FragmentTitle
{
private int mNum = 0;
private String mTitle = "Home";
private ListView mListView;
private MyArrayAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
InfoContainer temp = new InfoContainer("test1", "test2");
mAdapter = new MyArrayAdapter(getActivity(), android.R.id.list, temp);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v1 = inflater.inflate(R.layout.fragment_pager_list, container, false);
TextView tv = (TextView) v1.findViewById(R.id.text);
tv.setText("Fragment #" + mNum);
mListView = (ListView) v1.findViewById(android.R.id.list);
return v1;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
mListView.setAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
Log.i("HomeFragment", "Item clicked: " + id);
}
public CharSequence getTitle()
{
return mTitle;
}
}
ArrayAdapter:
public class MyArrayAdapter extends ArrayAdapter<InfoContainer>
{
Context mContext;
InfoContainer data;
public MyArrayAdapter(Context context, int textViewResourceId, InfoContainer temp)
{
super(context, textViewResourceId);
mContext = context;
this.data = temp;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null)
{
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
row = inflater.inflate(R.id.pager, parent, false);
holder.v1 = (TextView) row.findViewById(R.id.listview_name);
holder.v2 = (TextView) row.findViewById(R.id.listview_data);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
InfoContainer info = data;
holder.v1.setText(info.name);
holder.v2.setText(info.text);
return row;
}
public static class ViewHolder
{
TextView v1;
TextView v2;
}
}
fragment_pager_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:background="@drawable/listview_background"
android:textColor="#FFFFFF" />
之前我已经制作了自定义ArrayAdapter,没有任何问题,那么我是否缺少让他们使用ListFragment的东西?
答案 0 :(得分:6)
解决方案是getCount()
为我的ArrayAdapter返回0。所以我只使用mAdapter.add(temp)
然后使用mAdapter.notifyDataSetChanged()
并且它有效。我还必须将行row = inflater.inflate(R.id.pager, parent, false);
更改为row = inflater.inflate(R.layout.listview_row, parent, false);