1

我的问题是我无法从 ListView 中的 arrayList 中获取显示的值。列表显示,但我看不到地图中的值。我可以点击的视图,正好是五个——这就是遍历 json 数组后有多少个问题。但是每个视图都是空白的。

片段活动代码:

public class QuestionItemActivity extends FragmentActivity  {

Utils utils = new Utils();
private static int userID;
ArrayList<HashMap<String, Object>> questionsList;
ProgressDialog pDialog;
ListView questionView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_question_item);

    Bundle extras = getIntent().getExtras();
    userID = extras.getInt("id");
    initLayout();
    //new QuestionsLoadTask().execute();

}

private void initLayout() {

    questionView = (ListView) findViewById(R.id.list);
    questionsList = new ArrayList<HashMap<String, Object>>();

    questionView.setAdapter(new SimpleAdapter(QuestionItemActivity.this,
            questionsList, R.layout.quiz_right_fragment, null, null));

    questionView.setDividerHeight(0);

}

@Override
protected void onResume() {
    if (!ConnectivityStatus.isNetworkAvailable(getApplicationContext())) {
        Toast.makeText(QuestionItemActivity.this, R.string.network_failed,
                Toast.LENGTH_SHORT).show();
    } else {
        questionView.setAdapter(null);
        new QuestionsLoadTask().execute();
    }

    super.onResume();
}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(QuestionItemActivity.this);
        pDialog.setMessage("Reading data");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        questionsList.clear();
    }


    @Override
    protected String doInBackground(String... arg0) {
        try {

            ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
            parameters.add(new BasicNameValuePair("request", "getQuestions"));
            parameters.add(new BasicNameValuePair("clubId", Integer
                    .toString(3)));
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(Utils.URL);
            httpPost.setEntity(new UrlEncodedFormEntity(parameters,
                    ("ISO-8859-1")));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // if this is null the web service returned an empty page
            if (httpEntity == null) // response is empty so exit out
                return null;

            String jsonString = EntityUtils.toString(httpEntity);

            // again some simple validation around the returned string
            if (jsonString != null && jsonString.length() != 0) // checking
                                                                // string
                                                                // returned
                                                                // from
                                                                // service
                                                                // to grab
                                                                // id
            {
                JSONArray jsonArray = new JSONArray(jsonString);
                for (int i = 0; i < jsonArray.length(); i++) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                    int id = jsonObject.getInt("clubId");
                    map.put("clubId", id);


                    String question = jsonObject.getString("questionText");
                    map.put("questionText", question);
                    Log.d("WHAT's in JSON: ", jsonArray.toString());

                    questionsList.add(map);
                }


            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            Log.e("ERROR SOMEWHERE!!!! ", e.toString());
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        if (questionsList.size() == 0) {
            Toast.makeText(QuestionItemActivity.this,
                    "No questions in a list", Toast.LENGTH_SHORT).show();
        } else {
            questionView.setAdapter(new QuestionListAdapter(
                    QuestionItemActivity.this, questionsList,
                    R.layout.quiz_right_fragment, new String[] {
                             "question", "id" }, new int[] {
                            R.id.question, R.id.question_list_id}));
        }

        pDialog.dismiss();
    }
}

}

和片段:

public class QuestionsListFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(com.statsports.subjectiveandroid.R.layout.list_view, container, false);
}

有人可以帮我解决这些问题吗?LogCat 也没有指出任何错误。

谢谢你。

4

0 回答 0