【问题标题】:ListFragment keeps being emptyListFragment 一直为空
【发布时间】:2016-05-25 09:11:48
【问题描述】:

我想用从 API 端点获得的一些数据填充 ListFragment。我首先使用 ListView 对其进行了测试 - 它可以工作,但现在将其更改为 ListFragment。

现在列表不再填充。我测试了 hashmap 是否可能为空,但事实并非如此。

现在我有点不知所措——也许你可以看到我犯的错误:

public class AuftrageFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.auftrag_view, container, false);
        Context context = getContext();
        final SharedPreferences tokenStore = context.getSharedPreferences("TokenStore", Context.MODE_PRIVATE);
        String token = tokenStore.getString("token", null);
        if (token == null) {
            StartFragment start = new StartFragment();
            getFragmentManager().beginTransaction()
                    .replace(R.id.frame_container, start).commit();

        }
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        String[] from = {"auftragsnummer", "schadensbild", "termin"};
        int[] to = new int[]{R.id.auftragsnummer, R.id.schadensbild, R.id.termin};

        final ArrayList<HashMap<String,String>> auftragliste = new ArrayList<HashMap<String, String>>();

        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray ResponseArray = new JSONArray(response);
                    for(int i=0; i < ResponseArray.length(); i++){
                        JSONObject responseObject =  ResponseArray.getJSONObject(i);
                        String termin =  responseObject.getString("termin");
                        String schadensbild = responseObject.getString("schadensbild");
                        String auftragsnummer = responseObject.getString("auftragsnummer");
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("auftragsnummer", auftragsnummer);
                        map.put("schadensbild", schadensbild);
                        map.put("termin", termin);
                        auftragliste.add(map);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        Context context = getContext();
        final SharedPreferences tokenStore = context.getSharedPreferences("TokenStore", Context.MODE_PRIVATE);
        String token = tokenStore.getString("token", null);
        MainListRequest mainListRequest = new MainListRequest(token, responseListener, null);
        RequestQueue queue = Volley.newRequestQueue(this.getActivity());
        queue.add(mainListRequest);

        SimpleAdapter adapter = new SimpleAdapter(getActivity(), auftragliste,R.layout.auftrag_view, from, to);
        setListAdapter(adapter);




    }
}

auftrag_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Aufträge"
    android:id="@+id/uberschrift"
    android:layout_gravity="center_horizontal"
    android:gravity="center" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/android:list"
    android:choiceMode="singleChoice" />
</LinearLayout>

项目 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="fill_parent"
    android:baselineAligned="false">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Auftragsnummer"
            android:id="@+id/auftragsnummer"
            android:elegantTextHeight="true"
            android:gravity="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Schadensbild"
            android:id="@+id/schadensbild"
            android:layout_gravity="center_horizontal"
            android:clickable="true"
            android:gravity="left" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Termin"
            android:id="@+id/termin" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 我在任何地方都看不到 notifyDatasetChanged()。支持数据更改/更新后,您应该在适配器上调用它。

标签: java android android-listfragment


【解决方案1】:

你可以试试这个

(id for your listView).setListAdapter(adapter);

【讨论】:

  • IDE 告诉我视图没有这个方法
  • auftragliste.add(map);这段代码写在 Listener 块中。这意味着在调用 Listener 之前,您的列表是空的。所以添加这两行并在 auftragliste.add(map); 之后尝试notifyDatasetChanged():setListAdapter(适配器);
  • 感谢这个工作:所以在调用 Lisitner 之前 ArrayList 是空的,但是由于适配器不知道它,所以他没有更新视图?
  • 没错..享受:)
【解决方案2】:

你必须调用适配器#notifyDatasetChanged()

【讨论】:

  • 什么时候调用它?它似乎没有效果 - 它也被称为 notifyDataSetChanged()
  • 插入数据后,请从 Main/UI 线程调用!
【解决方案3】:

您期望在 AuftrageFragment.onCreateView 中为 'token' 提供什么价值?

onActivityCreated 在 onCreateView 之后被调用,你能检查一下你的容器中有哪个片段吗?我怀疑它是 StartFragment

【讨论】:

  • token 是 oauth 令牌。容器内的fragment是StartFragment,但也可以是其他的fragment
  • 对不起,我无法明确我的观点。如果 R.id.frame_container 是您的主容器,您将看不到 AuftrageFragment,因为当前可见片段是 StartFragment。检查您的视图层次结构
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多