有类似的问题,但解决方案都没有帮助。我认为问题在于布局文件或适配器类但无法找到错误。 Fragment加载正常,但列表视图中没有任何内容。
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class FavoritesFragment extends android.app.Fragment {
String namearray[];
String phonearray[];
String namecsv;
String phonecsv;
ListView lvl;
public static FavoritesFragment newInstance()
{
FavoritesFragment fragment = new FavoritesFragment();
return fragment;
}
public FavoritesFragment() {}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
((NavigationActivity) activity).onSectionAttached(4);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.favorites, container, false);
lvl = (ListView) rootView.findViewById(R.id.listViewFavourite);
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null, null);
while(phones.moveToNext())
{
//read contact name
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//read contact phonenumber
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(name!=null)
{
namecsv += name +",";
phonecsv+=phoneNumber +",";
}
}
phones.close();
namearray = namecsv.split(",");
phonearray= phonecsv.split(",");
namearray = removeDuplicate(namearray);
phonearray = removeDuplicate(phonearray);
// ListAdapter theAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1, namearray);
ListAdapter theAdapter = new FavouriteAdapter(getActivity(), namearray, phonearray);
lvl.setAdapter(theAdapter);
Toast.makeText(getActivity(),namecsv, Toast.LENGTH_SHORT).show();
lvl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String number = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getActivity(), number, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
public String[] removeDuplicate(String[] str)
{
List<String> list = Arrays.asList(str);
Set<String> set = new HashSet<String>(list);
String[] res = new String[set.size()];
set.toArray(res);
return res;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewFavourite"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Text View"
/>
</LinearLayout>
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class FavouriteAdapter extends BaseAdapter {
String[] name;
String[] phone;
Context context;
public FavouriteAdapter(Context cntxt, String[] valuename, String[] phonevalue)
{
super();
name = valuename;
phone=phonevalue;
context = cntxt;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View theView = inflator.inflate(R.layout.rowlayoutfavorite, parent, false);
TextView nameTextView = (TextView) theView.findViewById(R.id.textViewContactName);
TextView numberTextView = (TextView) theView.findViewById(R.id.textViewPhoneNumber);
nameTextView.setText(name[position].toString());
numberTextView.setText(phone[position].toString());
return theView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
答案 0 :(得分:1)
问题是由getCount()
的实施引起的。负责在屏幕上显示列表的某个部分调用此方法。当你返回零时,列表似乎是空的,就是这样。
更改方法:
@Override
public int getCount()
{
return name.length;
}