2

我正在尝试从 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
4

2 回答 2

2

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

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

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

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

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

ListAdapter adapter = new SimpleAdapter(m_context, mylist , R.layout.textview,new String[] { "exam", "id" },new int[] { R.id.exam, R.id.id });
于 2013-04-25T05:45:11.977 回答
2

创建一个自定义 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 的适配器如下:

myList = (ListView) findViewById(R.id.listView1);   
myList.setAdapter(new MyListAdapter(this, UserAndMessage));
于 2013-04-25T05:59:16.337 回答