在fragment中顯示一個自定義listView,但是運行後什麼也沒顯示。
public class Tab1 extends Fragment
implements ListView.OnItemClickListener{
private ArrayList<Custom> fetch = new ArrayList<Custom>();
private ContactsAdapter adapter;
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View v = inflater.inflate(R.layout.tab_layout, container, false);
Custom one = new Custom("Hoang Ha", "01672286349");
Custom two = new Custom("Ha Link", "03203590176");
fetch.add(one);
fetch.add(two);
lv =(ListView)v.findViewById(R.id.list);
adapter = new ContactsAdapter(getActivity(), fetch);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
return v;
}
public class Custom{
private String contactName;
private String contactPhone;
public Custom(String st1, String st2){
contactName = st1;
contactPhone = st2;
}
public String getName(){
return contactName;
}
public String getPhone(){
return contactPhone;
}
public void setName(String st1){
contactName = st1;
}
public void setPhone(String st2){
contactPhone = st2;
}
}
@Override
public void onItemClick(AdapterView<?> ad, View v, int position,
long id) {
// TODO Auto-generated method stub
}
private class ContactsAdapter extends BaseAdapter{
private FragmentActivity activity;
private LayoutInflater inflater;
private ArrayList<Custom> data;
public ContactsAdapter (FragmentActivity a, ArrayList<Custom> d){
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
data.size();
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
v = inflater.inflate(R.layout.list_row, null);
TextView contact_name = (TextView)v.findViewById(R.id.contact_name);
TextView phone_number = (TextView)v.findViewById(R.id.phone_number);
//final Custom custom = entries.get(position);
final Custom custom = data.get(position);
contact_name.setText(custom.getName());
phone_number.setText(custom.getPhone());
return v;
}
}
}
這是列表xml代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/light_dark"
android:padding="5dip" >
<TextView
android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@color/white"
android:typeface="sans"
android:textSize="@dimen/font_medium"
android:textStyle="bold"/>
<TextView
android:id="@+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@color/white"
android:typeface="sans"
android:textSize="@dimen/font_small"
/>
</LinearLayout>
@Override
public int getCount() {
// TODO Auto-generated method stub
data.size();
return 0;
}
適配器 return 0 能有東西才怪。。。