从 JSON 填充 Listview

Posted

技术标签:

【中文标题】从 JSON 填充 Listview【英文标题】:Populate Listview from JSON 【发布时间】:2011-09-10 18:04:53 【问题描述】:

有谁知道动态加载 Json 的任何示例 数据到 ListView,我见过的大多数例子只是使用 某种静态数组。我需要加载 10 行 Json 数据,然后在底部有一个负载更多.. 以获取 接下来的10个等等等等。 请使用 Json 的示例....

【问题讨论】:

我正在处理类似的案例,如果您找到任何解决方案,请在此处发布! 【参考方案1】:

程序员布鲁斯是正确的,没有默认的方法可以做到这一点。但是,有一种非常干净和简单的方法可以完成此任务。这是我用来处理 JSONArrays 的适配器。

class JSONAdapter extends BaseAdapter implements ListAdapter 

    private final Activity activity;
    private final JSONArray jsonArray;
    private JSONAdapter(Activity activity, JSONArray jsonArray) 
        assert activity != null;
        assert jsonArray != null;

        this.jsonArray = jsonArray;
        this.activity = activity;
    


    @Override public int getCount() 

        return jsonArray.length();
    

    @Override public JSONObject getItem(int position) 

        return jsonArray.optJSONObject(position);
    

    @Override public long getItemId(int position) 
        JSONObject jsonObject = getItem(position);

        return jsonObject.optLong("id");
    

    @Override public View getView(int position, View convertView, ViewGroup parent) 
        if (convertView == null)
            convertView = activity.getLayoutInflater().inflate(R.layout.row, null);

        JSONObject jsonObject = getItem(position);  

        return convertView;
    

【讨论】:

你不应该在生产级代码中有assertVenuesAdapter 是错字吗? 尝试复制粘贴,报错,能否举例说明如何使用此代码? 你能发布 R.layout.row 吗?【参考方案2】:

android 没有现成的适配器来用 JSON 数组填充 ListView,就像用数据库记录填充 ListView 一样。

我建议习惯于使用 JSON 数据填充您选择的 Java 数据结构,并习惯于使用 ListViews,使用不同的自定义行从不同的集合中填充它们。

这是一个从 JSON 数组填充List,然后使用List 填充ListView 的简单示例。

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends Activity

  @Override
  public void onCreate(Bundle savedInstanceState)
  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try
    
      String jsonInput = "[\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\",\"ten\"]";
      JSONArray jsonArray = new JSONArray(jsonInput);
      int length = jsonArray.length();
      List<String> listContents = new ArrayList<String>(length);
      for (int i = 0; i < length; i++)
      
        listContents.add(jsonArray.getString(i));
      

      ListView myListView = (ListView) findViewById(R.id.my_list);
      myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents));
    
    catch (Exception e)
    
      // this is just an example
    
  

【讨论】:

【参考方案3】:

dbaugh 有一个很好的答案,但 this 的帖子解释了如何为我们其他不知道从那里去哪里的人使用它。

【讨论】:

【参考方案4】:

试试下面的:

JSON 解析器类:

public class JSONParser 

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() 

    

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) 

        // Making HTTP request
        try 

            // check for request method
            if (method == "POST") 
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

             else if (method == "GET") 
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            

         catch (UnsupportedEncodingException e) 
            e.printStackTrace();
         catch (ClientProtocolException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        

        try 
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
                sb.append(line + "\n");
            
            is.close();
            json = sb.toString();
         catch (Exception e) 
            Log.e("Buffer Error", "Error converting result " + e.toString());
        

        // try parse the string to a JSON object
        try 
            jObj = new JSONObject(json);
         catch (JSONException e) 
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        

        // return JSON String
        return jObj;

    

显示 Listview 的片段类:

public static class EventFragment extends ListFragment 

        ArrayList<HashMap<String, String>> eventsList;
        private String url_all_events = //url goes here;
        private ProgressDialog pDialog;

        JSONParser jParser = new JSONParser();

        // JSON Node names
        private static final String CONNECTION_STATUS = "success";
        private static final String TABLE_EVENT = "Event";
        private static final String pid = "pid";
        private static final String COL_GROUP = "Group";
        private static final String COL_NAME = "Event_Name";
        private static final String COL_DESC = "Event_Desc";
        private static final String COL_DATE = "Event_Date";
        private static final String COL_TIME = "Event_Time";

        JSONArray Events = null;

        public static final String ARG_SECTION_NUMBER = "section_number";

        public EventFragment() 
        

        public void onStart() 
            super.onStart();

            eventsList = new ArrayList<HashMap<String, String>>();
            new LoadAllEvents().execute();

            // selecting single ListView item
            ListView lv = getListView();

            // Lauching the Event details screen on selecting a single event
            lv.setOnItemClickListener(new OnItemClickListener() 

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) 
                    // getting values from selected ListItem
                    String ID = ((TextView) view.findViewById(R.id.pid))
                            .getText().toString();

                    Intent intent = new Intent(view.getContext(),
                            EventDetails.class);
                    intent.putExtra(pid, ID);
                    view.getContext().startActivity(intent);
                
            );
        

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.fragment_events,
                    container, false);

            return rootView;
        

        class LoadAllEvents extends AsyncTask<String, String, String> 

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() 
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Just a moment...");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(true);
                pDialog.show();
            

            protected String doInBackground(String... args) 
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_events,
                        "GET", params);

                try 
                    // Checking for SUCCESS TAG
                    int success = json.getInt(CONNECTION_STATUS);

                    if (success == 1) 
                        // products found
                        // Getting Array of Products
                        Events = json.getJSONArray(TABLE_EVENT);
                        // looping through All Contacts
                        for (int i = 0; i < Events.length(); i++) 
                            JSONObject evt = Events.getJSONObject(i);

                            // Storing each json item in variable
                            id = evt.getString(pid);
                            group = evt.getString(COL_GROUP);
                            name = evt.getString(COL_NAME);
                            desc = evt.getString(COL_DESC);
                            date = evt.getString(COL_DATE);
                            time = evt.getString(COL_TIME);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(pid, id);
                            map.put(COL_GROUP, group);
                            map.put(COL_NAME, name);
                            map.put(COL_DESC, desc);
                            map.put(COL_DATE, date);
                            map.put(COL_TIME, time);

                            // adding HashList to ArrayList
                            eventsList.add(map);
                        
                     else 
                        // Options are not available or server is down.
                        // Dismiss the loading dialog and display an alert
                        // onPostExecute
                        pDialog.dismiss();
                    
                 catch (JSONException e) 
                    e.printStackTrace();
                

                return null;
            

            protected void onPostExecute(String file_url) 
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                getActivity().runOnUiThread(new Runnable() 
                    public void run() 
                        ListAdapter adapter = new SimpleAdapter(getActivity(),
                                eventsList, R.layout.list_item, new String[] 
                                        pid, COL_GROUP, COL_NAME, COL_DATE, COL_TIME ,
                                new int[]  R.id.pid, R.id.group, R.id.name, R.id.header,
                                        R.id.title2 );

                        setListAdapter(adapter);
                    
                );

            

        
    

活动详情类:

public class EventDetails extends Activity 

    String pid;
    TextView EvtName, EvtDesc, EvtDate, EvtTime, Header;
    ImageView logo;

    private String url_event_details = "http://centrubook.eu.pn/eventDetails.php";
    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();

    // JSON Node names
    private static final String CONNECTION_STATUS = "success";
    private static final String TABLE_EVENT = "Event";
    private static final String COL_PID = "pid";
    private static final String COL_NAME = "Event_Name";
    private static final String COL_DESC = "Event_Desc";
    private static final String COL_DATE = "Event_Date";
    private static final String COL_TIME = "Event_Time";

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId()) 
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        
        return super.onOptionsItemSelected(item);
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventdetails);

        // ActionBar actionBar = getActionBar();
        // actionBar.setDisplayHomeAsUpEnabled(true);

        EvtName = (TextView) findViewById(R.id.BombaName);
        EvtDesc = (TextView) findViewById(R.id.BombaDesc);
        EvtDate = (TextView) findViewById(R.id.BombaDate);
        EvtTime = (TextView) findViewById(R.id.BombaTime);
        logo = (ImageView) findViewById(R.id.BombaLogo);

        Intent i = getIntent();
        pid = i.getStringExtra(COL_PID);
        new getEventDetails().execute();
    

    class getEventDetails extends AsyncTask<String, String, String> 

        String name, desc, date, time;

        @Override
        protected void onPreExecute() 
            super.onPreExecute();
            pDialog = new ProgressDialog(EventDetails.this);
            pDialog.setMessage("Just a moment...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        

        @Override
        protected String doInBackground(String... args) 

            // Check for success tag
            int success;
            try 
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pid", pid));

                // getting product details by making HTTP request
                // Note that product details url will use GET request
                JSONObject json = jParser.makeHttpRequest(url_event_details,
                        "GET", params);

                // json success tag
                success = json.getInt(CONNECTION_STATUS);
                if (success == 1) 
                    // successfully received product details
                    JSONArray eventsObj = json.getJSONArray(TABLE_EVENT);

                    // get first product object from JSON Array
                    JSONObject details = eventsObj.getJSONObject(0);

                    name = details.getString(COL_NAME);
                    desc = details.getString(COL_DESC);
                    date = details.getString(COL_DATE);
                    time = details.getString(COL_TIME);

                 else 
                    // product with pid not found
                
             catch (JSONException e) 
                e.printStackTrace();
            
            return null;
        

        protected void onPostExecute(final String file_url) 
            pDialog.dismiss();

            runOnUiThread(new Runnable() 
                public void run() 

                    EvtName.setText(name);
                    EvtDesc.setText(desc);
                    EvtDate.setText(date);
                    EvtTime.setText(time);
                    logo.setImageResource(R.drawable.events);

                
            );
        
    

希望这会有所帮助;)

【讨论】:

嗨@user788971,如果它对你有帮助,你能接受这个答案吗? 谢谢,HttpClient 自您的帖子以来已被弃用,更新的课程可以在这里找到:danielnugent.blogspot.com.by/2015/06/… 显然现在你会使用 Volley 来处理这个请求【参考方案5】:

我做了一个这样的简单 JSONAdapter:

public abstract class JSONAdapter extends BaseAdapter 

private JSONArray array;

@Override
public int getCount() 
    return array == null ? 0 : array.length();


@Override
public Object getItem(final int position) 
    if (array == null | array.length() < position) 
        return null;
    
    try 
        return array.get(position);
     catch (final JSONException e) 
        e.printStackTrace();
        return null;
    


@Override
public long getItemId(final int position) 
    return position;


public JSONObject getObject(final int position) 
    return (JSONObject) getItem(position);


public void setData(final JSONArray data) 
    array = data;
    notifyDataSetChanged();


然后当我需要它的特定实例时,我必须根据我的具体需要覆盖适配器中的 getView 方法:

@Override
public View getView(final int position, final View convertView,
        final ViewGroup parent) 
    final View view;
    final ViewHolder holder;
    if (convertView != null) 
        view = convertView;
        holder = (ViewHolder) view.getTag();
     else 
        view = context.getLayoutInflater().inflate(
                R.layout.<mylayout>, parent, false);
        holder = new ViewHolder();
        holder.name = (TextView) view.findViewById(R.id.name);
                    [...]
        view.setTag(holder);
    
    final JSONObject jsonObj = getObject(position);
    holder.name.setText(jsonObj.optString("name"));
    [...]
    return view;

并像这样设置数据:

final JSONObject json = new JSONObject(jsonString);
adapter.setData(json.getJSONArray("projects"));

【讨论】:

以上是关于从 JSON 填充 Listview的主要内容,如果未能解决你的问题,请参考以下文章

ExtJs:从 Json 响应中填充 ComboBox

从 json 填充单元格时表为空

从 JSON 数据填充 UITableView

从 json 填充选择器,SwiftUI

从 ajax json 填充数据表

从 JSON 填充 Listview