SimpleAdapter

时间:2016-09-01 12:19:20

标签: android listview linkedhashmap simpleadapter

我在'map'上的simpleAdapter中出错,无法解决。我通过json文件创建了LinkedHashMap。 见下面的截图。我认为这是正确的。此屏幕截图仅用于查看数组的内容。

Screenshot of LinkedHashMap

请参阅下面的代码:

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class BlogFragment extends Fragment  {

View myView;
ArrayList<String> lists = new ArrayList<>();
ListView lv = (ListView) myView.findViewById(R.id.listView);

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.blog_layout, container, false);

    new AsyncTaskParseJson().execute();

     return myView;
}

public class AsyncTaskParseJson extends AsyncTask<LinkedHashMap, LinkedHashMap, LinkedHashMap> {

    String url = "myUrl"; // Link to json file
    JSONArray dataJsonArr = null;
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(getActivity(), "", "Loading blog ...");
    }

    @Override
    protected LinkedHashMap<Integer, ArrayList<String>> doInBackground(LinkedHashMap...  params) {

        LinkedHashMap<Integer, ArrayList<String>> map = new LinkedHashMap<>();

        try {

            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(url);
            dataJsonArr = json.getJSONArray("blog");

            for (int i = dataJsonArr.length()-1; i >= 0 ; i--) {

                JSONObject c = dataJsonArr.getJSONObject(i);
                String afb = c.getString("afbeelding");
                String tt = c.getString("titel");
                String inh = c.getString("inhoud");

                lists = new ArrayList<>();

                lists.add(tt);
                lists.add(afb);
                lists.add(inh);

                map.put(i,lists);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return map;
    }

    @Override
    protected void onPostExecute(LinkedHashMap map) {
        dialog.dismiss();

        String[] from = new String[]{"6", "5", "4", "3", "2", "1"};
        int[] to = new int[]{R.id.textView, R.id.textView2, R.id.textView3};

        //Map<String, ArrayList<String>> map1 = new LinkedHashMap<>(map);

        SimpleAdapter simpleAdapter;
        simpleAdapter = new SimpleAdapter(getActivity(), map, R.layout.blog_layout, from, to); // <<< **I get an error on 'map'**
        lv.setAdapter(simpleAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(), Detail.class);
                i.putExtra("string", Long.toString(id));
                startActivity(i);
            }
        });
    }}}

我已经尝试将LinkedHashMap转换为Map,但它没有解决问题。

有人可以帮帮我吗? 谢谢!

0 个答案:

没有答案