大家好,我有这个 fragment 创建方法
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
user_id = sp.getString("user_id", "anon");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_friends_list, null, false);
listview = (ListView)view.findViewById(R.id.friends_list);
fadapter = new FriendsAdapter(getActivity(), R.layout.single_friend);
listview.setAdapter(fadapter);
listview.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
fadapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listview.setSelection(fadapter.getCount() - 1);
}
});
我在日志中看到我的数据提供者正在创建列表项 这个类在我的fragment.java中
class GetFriends extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", user_id));
Log.w("params", params.toString());
Log.d("request!", "starting");
// getting product details by making HTTP request
jsonArray = jsonParser.makeHttpRequest(
EVENTS_URL, "POST", params);
Log.w("jsonArray", jsonArray.toString());
if (jsonArray != null) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
Log.w("jsonArray", jsonArray.getJSONObject(i).toString());
//CREATE ITEM FOR LISTVIEW
fadapter.add(new FriendsProvider(
jsonArray.getJSONObject(i).getInt("id"),
jsonArray.getJSONObject(i).getString("username")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
但是我的 fragment 列表没有显示任何内容。
fragment ListView :
<FrameLayout 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"
tools:context="com.myup2.up2.FriendsFragment">
<ListView
android:id="@+id/friends_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff" />
SingleFriend - 列表项
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/singleFriend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:textColor="@android:color/primary_text_light"
android:background="@drawable/own_event_bubble"
android:text="hello worldvfvdfvdfvdfv"
/>
适配器
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.single_friend,parent,false);
}
friends_item = (TextView)convertView.findViewById(R.id.singleFriend);
FriendsProvider provider = getItem(position);
friends_item.setText(provider.username );
return convertView;
}
也许我正在考虑让适配器和提供者变得复杂。 但它可以在应用程序的其他部分运行。
但也许我必须刷新 fragment 的 UI 或类似的东西?
编辑1:
public class FriendsAdapter extends ArrayAdapter<FriendsProvider>{
private List<FriendsProvider> friends_list = new ArrayList<FriendsProvider>();
private TextView friends_item;
Context ctx;
public FriendsAdapter(Context context, int resource) {
super(context, resource);
ctx = context;
}
@Override
public void add(FriendsProvider object) {
friends_list.add(object);
super.add(object);
}
@Override
public int getCount() {
return friends_list.size();
}
@Override
public FriendsProvider getItem(int position) {
return friends_list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.single_friend,parent,false);
}
friends_item = (TextView)convertView.findViewById(R.id.singleFriend);
FriendsProvider provider = getItem(position);
friends_item.setText(provider.username );
return convertView;
}
}
最佳答案
我认为问题在于,当您完成在适配器中添加项目时,您永远不会调用adapter.notifyDataSetChanged()
。
请尝试在您的AsyncTask中添加onPostExecute()
protected void onPostExecute() {
adapter.notifyDataSetChanged();
}
关于android - fragment 不显示 ListView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27213357/