FragmentActivity中具有自定义适配器的多个列表片段

时间:2014-01-29 01:34:17

标签: android android-arrayadapter android-fragmentactivity android-listfragment viewgroup

我有一项主要活动

public class MainActivity extends FragmentActivity implements ActionBar.TabListener

Eclipse自动生成两个选项卡,并且我创建了两个ListFragments以显示在每个选项卡上(也是由Eclipse自动生成的,我添加了我的代码并且没有删除任何被覆盖的方法)

public class FragA extend ListFragment {
  private List<TypeA> list_A;
  private ArrayAdapter<TypeA> adapter;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  list1=new ArrayList<TypeA>();
  list1= /*gets data from db*/
  adapter = new My_A_Adapter(getActivity(),list_A);
  setListAdapter(adapter);
}


private class My_A_Adapter extends ArrayAdapter<TypeA>{
  private LayoutInflater inflater;
public My_A_Adapter (FragA activity){
super(activity, R.layout.myItemView, list_A)
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
 if(convertView==null){
   convertView = inflater.inflate(R.layout.myItemView_A, parent, false)}
   /* defining the views as setting them by iterating over the list
      getting the data by position*/
  return convertView;
}

和FragB的方法相同,但 FragB列表由android.R.layout.simple_list_item_1 填充,而不是由自定义的myItemView填充

我认为上下文或某些活动属性存在问题(列表中显示的字符串是正确的,但视图不是我的!) 谁能帮我 ?感谢

FragB及其适配器的代码完全相同,只要您看到“A”替换为“B”

1 个答案:

答案 0 :(得分:0)

尝试使用A_Adapter,与B_Adapter相同:

private class My_A_Adapter extends ArrayAdapter<TypeA>{
    private Context context;

    public My_A_Adapter (Context context, int textViewResourceId){
        super(context, textViewResourceId);
        this.context = context;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if(convertView==null){
            convertView = mInflater.inflate(R.layout.myItemView,null)
        }
        /* defining the views as setting them by iterating over the list
        getting the data by position*/
        return convertView;
    }
}