2013-08-17 100 views
0

我終於讓我的onListItemClick正常工作,但現在好像我設置爲傳遞到下一個活動的數據沒有經過但活動仍然開始。我的應用程序不會崩潰或任何它只是加載的活動,但我已設置的數據似乎並沒有在那裏。我試圖刪除滾動視圖,但數據似乎仍然沒有通過。未將數據從列表視圖傳遞到活動

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 { 
        //question = json.getJSONArray(TAG_QUESTION); 


        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("TAG_QUESTION_SUBJECT"); 
    String Content = i.getStringExtra("TAG_QUESTION_CONTENT"); 
    String ChosenAnswer = i.getStringExtra("TAG_QUESTION_CHOSENANSWER"); 
    subject.setText(Subject); 
    content.setText(Content); 
    chosenanswer.setText(ChosenAnswer); 

    } 
} 

singlelistitem.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/Title1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title:" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/Subject1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.00" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/Question1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Question:" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/Content1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.01" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ScrollView> 

    <TextView 
     android:id="@+id/BestAnswer1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Best Answer:" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/ChosenAnswer1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.01" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</LinearLayout> 
+0

你的鑰匙不一樣 – Raghunandan

回答

0

你的鑰匙是不一樣的。

你有這樣的

i.putExtra(TAG_QUESTION_SUBJECT, Subject); 

TAG_QUESTION_SUBJECT是 「主題」

final String TAG_QUESTION_SUBJECT = "Subject"; 

的GET部分

String Subject = i.getStringExtra("TAG_QUESTION_SUBJECT"); // keys do not match 
// must be "Subject" 

將其更改爲以下。密鑰必須匹配

String Subject = i.getStringExtra("Subject"); 
String Content = i.getStringExtra("Content"); 
String ChosenAnswer = i.getStringExtra("ChosenAnswer"); 
+0

感謝您的全力幫助我應該親眼看到這個。非常感謝! –

+0

你是否可以通過'TAG_QUESTION_CHOSENANSWER =「ChosenAnswer」;'和String TAG_QUESTION_CONTENT =「Content」;'沒有將這些字符串填充到我的列表視圖中。我問這個的原因是因爲當我點擊這個項目時,我希望顯示在其他活動中的唯一字符串是String TAG_QUESTION_SUBJECT =「Subject」;這是因爲'Subject'是我想要傳遞的唯一字符串已經填充到列表視圖中。那兩個其他字符串包含我只需要知道如何傳遞它們而不必將它們填充到列表視圖中的數據。 –

+0

@BobLast你可以傳遞值而不用在listview中填充。你的散列表的數據只是使用相同的,並使用意圖傳遞值 – Raghunandan

0

您應該使用same keys發送和接收。您的sending activity doesn't match with the keys中的部分密鑰正在接收活動。

發送:

map.put("Question", Subject); 
map.put("NumAnswer", NumAnswers); 
map.put("content", Content); 
map.put("chosenAnswer", ChosenAnswer); 

用於接收:

String Subject = question.getString("Question"); 
String NumAnswers = question.getString("NumAnswer"); 
String ChosenAnswer = question.getString("chosenAnswer"); 
String Content = question.getString("content"); 
0

看起來你已經命名的額外內容,例如,在你的列表視圖活動 「主題」,然後引用它們在單項活動中有所不同:

String Subject = i.getStringExtra("TAG_QUESTION_SUBJECT"); 

可能會考慮到這一點。