我有一个片段 FragmentTab1 &我想替换 AllContactsFragment 片段,其中包含listview&两个按钮。替换效果很好,但数据未显示在 ListView 中。数据也显示在log cat中。
FragmentTab1里面的替换代码是:
AllContactsFragment allContactsFragment = new AllContactsFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.addToBackStack(null);
transaction.add(R.id.fragmentTabLayout1, allContactsFragment);
transaction.commit();
我在AllContactsFragment里面的listview中填充数据,如:
public class AllContactsFragment extends SherlockFragment implements
OnClickListener {
ListView listViewAllContact;
Button btnAdd, btnCacel;
List<BlockNumber> contactNumberlist;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_all_contacts, container,
false);
// data are comes well & checked in Log cat
contactNumberlist = PhoneUtils.getAllContacts(getActivity());
listViewAllContact = (ListView) rootView
.findViewById(R.id.listViewAllContact);
ContactListAdapter adapter = new ContactListAdapter(getActivity(),
contactNumberlist, m_onSelectedEventCalender);
listViewAllContact.setAdapter(adapter);
if (container == null) {
return null;
}
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
}
}
我也分享我的ContactListAdapter适配器
public class ContactListAdapter extends BaseAdapter {
private List<BlockNumber> allContactsNumbers = null;
public Context context;
public LayoutInflater inflater;
private ViewHolder holder;
private onSelectedEventCalender m_onSelectedEventCalender;
public ContactListAdapter(Context context, List<BlockNumber> allNumberList,
onSelectedEventCalender m_onSelectedEventCalender) {
super();
this.context = context;
this.allContactsNumbers = allNumberList;
this.inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.m_onSelectedEventCalender = m_onSelectedEventCalender;
}
@Override
public int getCount() {
return allContactsNumbers.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_row, null);
convertView.setMinimumHeight(50);
holder.textViewContactName = (TextView) convertView
.findViewById(R.id.textview_contact_name);
holder.textView_Contact_Number = (TextView) convertView
.findViewById(R.id.textview_number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.textViewContactName.setText(allContactsNumbers.get(
position).getName());
holder.textView_Contact_Number.setText(allContactsNumbers.get(
position).getNumber());
holder.textViewContactName.setTag(allContactsNumbers
.get(position));
return convertView;
}
} catch (Exception ex) {
Log.w("Exception", ex.getMessage());
}
return null;
}
public static class ViewHolder {
TextView textViewContactName;
TextView textView_Contact_Number;
// TextView textViewEventEndDate;
}
public interface onSelectedEventCalender {
void onSelectedEventCalender(BlockNumber aBlockNumber, int type);
}
}
AllContactsFragment的相应XML是fragment_all_contacts.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_new" >
<!-- android:background="#80000000" -->
<RelativeLayout
android:id="@+id/relativeLayoutFragmentMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listViewAllContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageViewLine1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:background="@drawable/action_bar"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_weight="1"
android:text="Add" />
<Button
android:id="@+id/btnCanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dip"
android:layout_weight="1"
android:text="Home" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
可能是我错过了什么?
已修改:使上述代码正确。
删除android:layout_below =&#34; @ + id / imageViewLine1&#34;从ListView定义XML中,从适配器&amp;中的getView()中删除return null。检查数据是否可用,设置为listview。
答案 0 :(得分:0)
我会尝试
1)在xml中将listview设置为“线性布局 - 按钮 - 栏”之上“
2)创建ContactListAdapter适配器后调用adapter.notifyDataSetChanged()
3)在适配器中的第一个if - after convertView.setTag(holder); set return convertView;
4)在onViewCreated()方法
中创建并设置适配器或
用imageview替换你的listview一次,看看你是否会看到图像,而不是你知道listview是失败的那个(不要忘记设置背景!)
答案 1 :(得分:0)
窝!我多么愚蠢?我的 contactNumberlist 为空,因为我没有在列表中添加该对象。我的Log cat误导了我。感谢@Ragunandan&amp; @tom贵族因为能够找到错误而负担得起。
我的ContactListAdapter getView()看起来像:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_row, null);
convertView.setMinimumHeight(50);
holder.textViewContactName = (TextView) convertView
.findViewById(R.id.textview_contact_name);
holder.textView_Contact_Number = (TextView) convertView
.findViewById(R.id.textview_number);
holder.imgViewContactImage = (ImageView) convertView
.findViewById(R.id.imgViewContactImage);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
if (allContactsNumbers.size() <= 0) {
holder.textViewContactName.setText("No Data");
} else {
holder = (ViewHolder) convertView.getTag();
String name = allContactsNumbers.get(position).getName();
holder.textViewContactName.setText(name);
String number = allContactsNumbers.get(position).getNumber();
holder.textView_Contact_Number.setText(number);
Uri Uri = allContactsNumbers.get(position).getImage_Uri();
if (Uri != null) {
holder.imgViewContactImage.setImageURI(Uri);
} else {
holder.imgViewContactImage.setImageResource(R.drawable.ic_no_image);
}
Log.d("Contacts in Adapter", "" + name + "" + number);
}
return convertView;
}