android - fragment 不显示 ListView 项目

标签 android mysql android-fragments android-arrayadapter

大家好,我有这个 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/

相关文章:

Android - 我怎么知道一个apk已经用我的 key 签名了

针对特定情况的 Android AsyncTask 与线程

mysql - 从云服务器改为专用服务器会提高MySQL性能吗?

java - 如何使用scrollView在两个 fragment 之间进行通信?

用户点击 "set wallpaper"时Android动态壁纸预览启动activity

java - 适用于 Java 的 Google Http 客户端 : Ignore java. security.cert.CertPathValidatorException:未找到证书路径的信任 anchor

mysql - 执行 Mariadb 更新语句时出错

mysql - Alter Table Engine = InnoDB 可以同时在多个表上运行吗?

android - 在FrameLayout中动态添加多个 fragment

android - 无法通过 DataBinding 或 Android 扩展访问 View