【问题标题】:not able to populate list from json android无法从 json android 填充列表
【发布时间】:2013-04-25 05:40:45
【问题描述】:

我正在尝试从 api 填充列表对象。
这是jsonresult的一种方法。

protected void onPostExecute(String result)
    {

        JSONArray con;
        //String tag_name="tests";
        //String tag_id="ID";
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        try 
        {
            JSONObject jsonObject = new JSONObject(result);
            if("success".equals(jsonObject.getString("result")))
            {   Toast.makeText(getBaseContext(),jsonObject.getString("tests"),Toast.LENGTH_SHORT).show();
                //String nKey=jsonObject.getString("nKey");
            //  switchActivity(nKey);
                //Toast.makeText(getBaseContext(),nKey,Toast.LENGTH_SHORT).show();
                try{

                con = jsonObject.getJSONArray("tests");
                for(int i = 0; i < con.length(); i++){
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject c = con.getJSONObject(i);
                    map.put("EXAM", "" + c.getString("exam"));
                    map.put("ID", "" + c.getString("id"));
                    mylist.add(map);
                }}catch (JSONException e) {
                    e.printStackTrace();
                }
                ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.textview,new String[] { "exam", "id" },new int[] { R.id.exam, R.id.id });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);

            }
            else
            {
                Toast.makeText(getBaseContext(),jsonObject.getString("message"),Toast.LENGTH_LONG).show();
            }
        } 
        catch (Exception e)
        {
            Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage());
        }          
    }

在

处给出错误
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.textview,new String[] { "exam", "id" },new int[] { R.id.exam, R.id.id });

错误:

The constructor SimpleAdapter(examlist.ReadJSONResult, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined

【问题讨论】:

  • SimpleAdapter 的构造函数是什么???和 ListAdapter??

标签: android json android-listview custom-adapter


【解决方案1】:

创建一个自定义的 Adpater 来扩充 ListView。

public class MyListAdapter extends BaseAdapter {

ArrayList<HashMap<String, String>> data;
Activity a;
private static LayoutInflater inflater=null;


public MyListAdapter(Activity act, ArrayList<HashMap<String, String>> UserAndMessage)
{
    data = UserAndMessage;
    inflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    a = act;
}

@Override
public int getCount() {

    return data.size();
}

@Override
public Object getItem(int position) {

    return data.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertview, ViewGroup parent) {

    View vi = convertview;
    if(null == vi)
    {
        vi = inflater.inflate(R.layout.listitem, null);

        TextView ID= (TextView) vi.findViewById(R.id.ID);
        TextView Exam= (TextView) vi.findViewById(R.id.exam);

        HashMap<String,String> item = data.get(position);
        ID.setText(item.get("name"));
        EXAM.setText(item.get("message"));

    }

    return vi;
 }

}

从 onPostExecute() 设置 ListView 的 Adapter 如下:

myList = (ListView) findViewById(R.id.listView1);   
myList.setAdapter(new MyListAdapter(this, UserAndMessage));

【讨论】:

    【解决方案2】:

    这是因为你使用的ListAdapter 没有这样的构造函数。作为Context(第一个参数),您将传递examlist.ReadJSONResult,并且您应该传递Activity 的Context,其中放置了使用此ListAdapter 的View。

    如果您设置ListAdapter 的类不是Activity,那么您应该将Activity 的Context 传递给该类并将其存储为例如成员字段以供进一步使用.

    例如,您的班级名为ReadJSONResult。创建一个以Context为参数的构造函数:

    public ReadJSONResult(Context context) {
        m_context = context;  // There needs to be a field member in ReadJSONResult class called m_context
    }
    

    多亏了这一点,在您创建ReadJSONResult 对象的Activity 中,您将Activity 的Context 传递给构造函数,然后您可以像这样创建您的ListAdapter:

    ListAdapter adapter = new SimpleAdapter(m_context, mylist , R.layout.textview,new String[] { "exam", "id" },new int[] { R.id.exam, R.id.id });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多