0

我想通过String TAG_QUESTION_CONTENT = "Content"; String TAG_QUESTION_SUBJECT = "Subject";String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";但是当我单击我想要的项目时,下一个活动中显示的唯一字符串String TAG_QUESTION_SUBJECT = "Subject";是因为这是填充列表视图的唯一字符串。我不希望在列表视图中填充其他两个字符串,我只想将它们传递给下一个活动,以便它们可以在那里显示。谁能告诉我一种在不填充它们的情况下传递这两个其他字符串的方法。

public class ListView extends ListActivity {    


    ArrayList<HashMap<String, String>> questionList;        

     final String TAG_RESULTS = "results";
     final String TAG_QUESTION_SUBJECT = "Subject";
     final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
     final String TAG_QUESTION = "question";
     final String TAG_QUESTION_CONTENT = "Content";
     final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     final String TAG_ANSWERS = "Answers";
     final String TAG_ANSWER = "Answer";    
     final String TAG_ANSWERS_CONTENT = "content";      
     final String TAG_QUERY = "query";

            JSONArray question = null;          
            android.widget.ListView lv;

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

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


    new LoadAllData().execute();

        }

    @Override   
    protected void onListItemClick(android.widget.ListView l, View v, int pos, long id) {
         super.onListItemClick(l, v, pos, id);  

         String Subject = ((TextView) v.findViewById(R.id.Subject)).getText().toString();
         String Content = ((TextView) v.findViewById(R.id.Content)).getText().toString();
         String ChosenAnswer = ((TextView) v.findViewById(R.id.ChosenAnswer)).getText().toString();


         Intent i = new Intent(ListView.this, SingleListItem.class);
         i.putExtra(TAG_QUESTION_SUBJECT, Subject);
         i.putExtra(TAG_QUESTION_CONTENT, Content);
         i.putExtra(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);
         startActivity(i);

            }     


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


        private Dialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressDialog pDialog; 
            pDialog = new ProgressDialog(ListView.this);
            pDialog.setMessage("Loading Data. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
        }

        protected String doInBackground(String... args) {

            try {
                Intent in = getIntent();
                String searchTerm = in.getStringExtra("TAG_SEARCH");
                String query = URLEncoder.encode(searchTerm, "utf-8");
                String URL = "http://example.com";
                JSONParsser jParser = new JSONParsser();
                JSONObject json = jParser.readJSONFeed(URL);
                try {

                    JSONArray questions = json.getJSONObject("all").getJSONArray("questions");

                    for(int i = 0; i < questions.length(); i++) {
                        JSONObject question = questions.getJSONObject(i);


                    String Subject = question.getString(TAG_QUESTION_SUBJECT);
                    String NumAnswers = question.getString(TAG_QUESTION_NUMANSWERS);
                    String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
                    String Content = question.getString(TAG_QUESTION_CONTENT);

                    //JSONArray Answers = question.getJSONObject(TAG_ANSWERS).getJSONArray(TAG_ANSWER);


                    //JSONObject Answer = Answers.getJSONObject(0);

                    //String Content = Answer.getString(TAG_ANSWERS_CONTENT);

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

                               map.put(TAG_QUESTION_SUBJECT, Subject);
                               map.put(TAG_QUESTION_NUMANSWERS, NumAnswers);
                               map.put(TAG_QUESTION_CONTENT, Content);
                               map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);

                               questionList.add(map);


                    }


                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                return TAG_QUESTION ;           

        }

        @Override
        protected void onPostExecute(String file_URL) {
            ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
                        R.layout.listelements,
                        new String[] { TAG_QUESTION_SUBJECT, TAG_QUESTION_NUMANSWERS }, new int[] {
                        R.id.Subject, R.id.NumAnswers, });

                setListAdapter(adapter); 


        }}}

SingleListItem 活动:

public class SingleListItem extends Activity {

    TextView title;
    TextView question;
    TextView bestanswer;
    TextView subject;
    TextView content;
    TextView chosenanswer;

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

    title = (TextView) findViewById(R.id.Title1);
    question = (TextView) findViewById(R.id.Question1);
    bestanswer = (TextView) findViewById(R.id.BestAnswer1);
    subject = (TextView) findViewById(R.id.Subject1);
    content = (TextView) findViewById(R.id.Content1);
    chosenanswer = (TextView) findViewById(R.id.ChosenAnswer1);

    Intent i = getIntent();
    String Subject = i.getStringExtra("Subject");
    String Content = i.getStringExtra("Content");
    String ChosenAnswer = i.getStringExtra("ChosenAnswer");
    subject.setText(Subject);
    content.setText(Content);
    chosenanswer.setText(ChosenAnswer);

    }
}
4

1 回答 1

0

在这种情况下,您需要使用您的数据模型。下面的代码应该适合你。

@Override   
protected void onListItemClick(android.widget.ListView l, View v, int pos, long id) {
  super.onListItemClick(l, v, pos, id);  

  HashMap<String, String> item = questionList.get(pos);

  Intent i = new Intent(ListView.this, SingleListItem.class);
  i.putExtra(TAG_QUESTION_SUBJECT, item.get(TAG_QUESTION_SUBJECT));
  i.putExtra(TAG_QUESTION_CONTENT, item.get(TAG_QUESTION_CONTENT));
  i.putExtra(TAG_QUESTION_CHOSENANSWER, item.get(TAG_QUESTION_CHOSENANSWER));
  startActivity(i);

}
于 2013-08-18T18:07:43.210 回答