简体   繁体   English

ListFragment一直为空

[英]ListFragment keeps being empty

I want to populate a ListFragment with some Data obtained from an API Endpoint. 我想用从API端点获得的一些数据填充ListFragment。 I first tested it with a ListView - which worked but now changed it into a ListFragment. 我首先使用ListView测试了它-可以工作,但现在将其更改为ListFragment。

Now the List isn't populated anymore. 现在列表不再填充。 I tested if the hashmap might be empty but it is not the case. 我测试了哈希图是否为空,但事实并非如此。

Now I am a bit clueless - maybe you can see the mistake I made: 现在我有点笨了-也许您可以看到我犯的错误:

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 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>

The Items xml: 项目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>

您必须调用adapter#notifyDatasetChanged()

你可以试试这个吗

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

what value you are expecting in AuftrageFragment.onCreateView for 'token' ? 您在AuftrageFragment.onCreateView中为“令牌”期望什么值?

onActivityCreated get called after onCreateView, can you check which fragment is in your container ? 在onCreateView之后调用onActivityCreated,可以检查容器中的哪个片段吗? I am suspecting it is StartFragment 我怀疑是StartFragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM