2013-07-27 72 views
0

我只是想正確地做到這一點。我建議使用此代碼來解決我在幾個小時前問過的問題,但是此代碼有錯誤。安卓與simpleadapter的故障

public class ListView extends ListActivity { 

    private ProgressDialog pDialog; 

    JSONParsser jParser = new JSONParsser(); 
    ArrayList<HashMap<String, String>> questionList; 

    private static String URI = "http://example.com/json";; 

    //JSONObject json = jParser.getJSONFromURI(URI); 

    final String TAG_RESULTS = "ResultsSet"; 
    final String TAG_SUBJECT = "Subject"; 
    final String TAG_NUMANSWERS = "NumAnswers"; 
    final String TAG_QUESTION = "Question"; 
    final String TAG_QUESTION_CONTENT = "Content"; 
    final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer"; 
    final String TAG_QUESTION_ANSWERS = "Answers"; 

      JSONArray ResultsSet = null; 


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

    questionList = new ArrayList<HashMap<String, String>>(); 

    new LoadAllData().execute(); 

    android.widget.ListView lv = getListView(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 


     } 
    }); 

} 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == 100) { 
      Intent intent = getIntent(); 
      finish(); 
      startActivity(intent); 
     } 
    } 

    class LoadAllData extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ListView.this); 
      pDialog.setMessage("Loading Data. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     protected String doInBackground(String... args) { 

      try { 
        JSONObject json = jParser.getJSONFromURI(URI); 
        ResultsSet = json.getJSONArray(TAG_RESULTS); 

        for(int i = 0; i < ResultsSet.length(); i++) { 
         JSONObject r = ResultsSet.getJSONObject(i); 
         String Subject = r.getString(TAG_SUBJECT); 
         String NumAnswers = r.getString(TAG_NUMANSWERS); 

         JSONObject Question = r.getJSONObject(TAG_QUESTION); 
         String Content = Question.getString(TAG_QUESTION_CONTENT); 
         String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER); 
         String Answers = Question.getString(TAG_QUESTION_ANSWERS); 

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

         map.put(TAG_SUBJECT, Subject); 
         map.put(TAG_NUMANSWERS, NumAnswers); 
        } 



      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 


     } 


     protected void onPostExecute(String file_URI) { 

      pDialog.dismiss(); 
      runOnUiThread(new Runnable() { 
      public void run() { 
       ListAdapter adapter = new SimpleAdapter(this, questionList, 
         R.layout.row, 
         new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] { 
         R.id.Subject, R.id.NumAnswers }); 

       setListAdapter(adapter); 

這些線路在這裏

ListAdapter adapter = new SimpleAdapter(this, questionList,R.layout.row, 
new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] { 
          R.id.Subject, R.id.NumAnswers }); 

錯誤稱的構造 simpleAdapte(new Runnable(){}; ArrayList<HashMap<string,String>>,int,String[],int[]) is underfined 有人能告訴我什麼,我需要做什麼來解決這個問題,我有。

回答

0

在你的情況,「這個」在構造函數是指Runnable接口。改用'getBaseContext'。

ListAdapter adapter = new SimpleAdapter(getBaseContext, questionList,R.layout.row, 
new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] { 
         R.id.Subject, R.id.NumAnswers });