ListFragment一直是空的

时间:2016-05-25 09:11:48

标签: java android android-listfragment

我想使用从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>

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你可以试试吗

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>DB143DBF0F316EB8</RequestId>
    <HostId>5hWJ0AHM466QcT+BK4UaEFpqXFNaJFEuAPlN/ZZPBhL+NDYBoGaySRkXQ3BRdyfy9PBDuSb0oHA=</HostId>
</Error>

答案 2 :(得分:0)

您在AuftrageFragment.onCreateView中为“&#39;令牌”期待的价值是什么? ?

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

相关问题