2013-04-25 74 views
2

我想從API填充列表對象。
這是一個方法jsonresult。無法填充json的列表android

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 
+0

什麼是SimpleAdapter的構造函數?和ListAdapter? – 2013-04-25 05:46:38

回答

2

這是因爲當你使用它有沒有這樣的構造函數ListAdapter。作爲Context(第一個參數),您將通過examlist.ReadJSONResult,並且您應該通過ContextActivity,其中使用此的View被放置。

如果您正在設置ListAdapter的類不是一個Activity,那麼你應該通過ActivityContext這個類並將其存儲例如作爲進一步用部件領域。

例如,您的班級名爲ReadJSONResult。創建一個構造函數,需要Context作爲參數:在您創建ReadJSONResult對象

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

感謝的是,在Activity,你通過ActivityContext到構造函數,然後您可以創建ListAdapter這樣的:

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

Create a Custom Adpater for inflating the 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; 
} 

} 

from onPostExecute() set ListView's Adapter as below:

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