本文介绍了自定义ArrayAdapter在ListFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义 ArrayAdapter 使用我的 ListFragment 。现在什么都没有,除了在的TextView 说这是什么片段数显示在 ListFragment 。我已经把一个断点 getView()方法,我适配器,它是不会被调用。我搜索了,不会被调用,有些人似乎认为我的ListView被隐藏的原因,所以我试图改变 layout_height WRAP_CONTENT ,但没有奏效。

I'm trying to create a custom ArrayAdapter to use for my ListFragment. Right now nothing is being displayed in the ListFragment except for the TextView saying what Fragment number it is. I have put a breakpoint in getView() method of my adapter and it is not being called. I searched for reasons that wouldn't be called and some people seem to think that my ListView is hidden, so I've tried changing the layout_height to wrap_content, but that didn't work.

另外,我一直在关注这个开发者页面的code:http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

Also, I've been following the code from this developer's page: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

ListFragment:

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:

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:

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" />

我以前有没有问题做定制ArrayAdapters,所以有什么我失踪约让他们有ListFragment工作?

I've made custom ArrayAdapters before with no problem, so is there something I'm missing about getting them to work with a ListFragment?

推荐答案

解决的办法是, getCount将()返航0我的ArrayAdapter。所以我只是用 mAdapter.add(临时)然后 mAdapter.notifyDataSetChanged()和它的工作。我也不得不改变线路行= inflater.inflate(R.id.pager,父母,假); 行= inflater.inflate( R.layout.listview_row,父母,假);

The solution is that getCount() was returning 0 for my ArrayAdapter. So I just used mAdapter.add(temp) and then mAdapter.notifyDataSetChanged() and it worked. I also had to change the line row = inflater.inflate(R.id.pager, parent, false); to row = inflater.inflate(R.layout.listview_row, parent, false);

这篇关于自定义ArrayAdapter在ListFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:29