onItemClickListener需要一组额外的眼睛

时间:2013-08-15 01:56:34

标签: android android-listview

我已经完成了我可能做的所有事情,以找出我的onItemClickListener有什么问题。有一个有帮助的人试图帮助,但他或她说他们绝对没有看到任何错误,他们不明白为什么它不起作用。我做了很多研究,并寻找其他人试图解决问题的代码,但它仍然无效。我真的需要很多眼睛来帮助我解决这个问题。

列出活动:

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();

        }


    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);

                               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.listview,
                        new String[] { TAG_QUESTION_SUBJECT, TAG_QUESTION_NUMANSWERS }, new int[] {
                        R.id.Subject, R.id.NumAnswers });

                setListAdapter(adapter); 
        }

    }

@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);

        }

}

问题是我的onItemClickListener没有像其他问题那样被调用。

listview.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" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:descendantFocusability="afterDescendants" >

    </ListView>

    <TextView
        android:id="@+id/Subject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:focusable="false" 
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/NumAnswers"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView 
        android:id="@+id/ChosenAnswer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView 
        android:id="@+id/Content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:focusable="false" 
        android:focusableInTouchMode="false" />

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

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

ListActivity类附带受保护的方法onListItemClick,使用您的处理程序代码覆盖它,不要直接调用setOnItemClickListenerListActivity负责所有初始化。


使用您的布局时:ListView的ID错误。在XML中,使用id

android:id="@android:id/list"

来自ListActivity reference

  

[...]使用setContentView()设置您自己的视图布局   onCreate()。为此,您自己的视图必须包含ListView对象   标识"@android:id/list"(或android.R.id.list,如果它在代码中)