ListView没有在Fragment上显示

时间:2013-07-08 17:11:16

标签: android-fragments android-arrayadapter

如何将ListView放入片段? 我尝试使用getview()但它无法正常工作。请帮帮我。

我的数组是Mail,属性类型为字符串,代码编译没有错误。

我不明白

我的班级片段1是

public class Fragment1 extends Fragment{
private ListView list;
LayoutInflater inflater1;
ViewGroup container1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState) {
    this.inflater1=inflater;
    this.container1=container;
   return  inflater.inflate(R.layout.list_items, container, false);
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    View view = this.inflater1.inflate(R.layout.list_items,this.container1, false);
    list= (ListView)view.findViewById(R.id.list);

list.setAdapter(new Adapter2 (this));

}

public class Adapter2 extends ArrayAdapter Mail {

private Activity mcontext;
private Correo[] mvalues;
public Adapter2(Fragment context) {
    super(context.getActivity(),R.layout.simple_item,Mail);
    this.mcontext= (Activity) context.getActivity();
    this.mvalues= Mail; // My array list 
}

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = mcontext.getLayoutInflater();
    View item = inflater.inflate(R.layout.simple_item,null);
    TextView lblDe = (TextView)item.findViewById(R.id.textViewlabel);
    lblDe.setText(mvalues[position].getText());
    TextView lblDe2 = (TextView)item.findViewById(R.id.textViewlabel2);
    lblDe2.setText(mvalues[position].getText());
    return(item);
    }
}

1 个答案:

答案 0 :(得分:1)

ListView和Fragments不能那样工作。

1)onCreateView是指定将在片段中显示的View的方法。所以onActivityCreated不是正确的。

2)请参阅示例here ArrayAdapter如何工作。但是,如果您想为每个列表项显示自己的BaseAdapter,我建议您使用View课程。