【问题标题】:Listview with hashmap带有哈希图的列表视图
【发布时间】:2015-12-21 16:01:44
【问题描述】:

我想在 listview 中显示服务器 json 响应。我在 hashmap 中获取所有值我对如何在自定义 listview 中设置该数据感到困惑。请帮助我并显示代码

class viewticket extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdialog = new ProgressDialog(UserLogedIn.this);
        pdialog.setMessage("Loading....");
        pdialog.setIndeterminate(false);
        pdialog.setCancelable(false);
        pdialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        List<NameValuePair> param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("userid", u_id));
        JSONObject jsonArray = jpar.makeHttpRequest(URLMyTicket, "POST", param);
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(URLMyTicket, ServiceHandler.POST, param);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {

               contacts = new JSONArray(jsonStr);
                int a=contacts.length();
                Log.v(TAG,""+a);
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_PROB);
                    String email = c.getString(TAG_DESC);
                    HashMap<String, String> contact = new HashMap<String, String>();
                    contact.put(TAG_ID, id);
                    contact.put(TAG_PROB, name);
                    contact.put(TAG_DESC, email);
                    contactList.add(contact);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();

//What code to write

    }

}

onpost 方法和自定义列表类应该写什么

Ticket_list.java

public class Ticket_list extends ArrayAdapter< String> {
HashMap<String, String> objects;
public Ticket_list(Context context, int resource, HashMap<String,String> objects) {
    super(???? //what );

}
   //What



   }

请帮我写onpost方法代码和Ticket_list的代码

【问题讨论】:

  • 您的自定义适配器在哪里?
  • 首先找到关于列表适配器的基本教程理解它......然后根据你的要求实施......之后你得到错误然后用你的实施编辑这个问题......@987654321 @
  • HashMap to ListView的可能重复
  • 我不知道如何编写代码我是初学者,所以请你根据我的问题给我代码
  • onpost方法里写什么,customlist类里有什么代码

标签: android


【解决方案1】:

我强烈建议您使用 RecyclerView 而不是 ListView 并使用 Retrofit 发送请求并以 json 格式获取响应。请查看这些文档 For RecyclerView

【讨论】:

  • 我不知道如何使用 recycleview 你能用自定义列表视图解决 mt=y 问题吗
  • @abhijaghari 你看文档了吗?它对recyclerView有完整的解释。 RecyclerView 比自定义 listView 更容易学习和使用。请查看我在上面发布的文档。但是,如果您无法理解这个概念,我会发布一些 cusomt listView 的代码。
猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 2014-06-04
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2021-01-07
  • 2015-11-02
  • 1970-01-01
相关资源
最近更新 更多