2013-07-08 169 views
0

如何將ListView放入片段? 我試着用getview(),但它不工作。請幫幫我。ListView未顯示在片段

我的數組是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

ListView和碎片不喜歡的工作。

1)onCreateView是指定將在片段中顯示的View的方法。所以onActivityCreated是不正確的。

2)請參閱示例hereArrayAdapter如何工作。不過,如果您想爲每個列表項顯示您自己的View,我建議您使用BaseAdapter類。

+0

thnks。好吧..會看到這個例子。 –