0

我在最近的帖子中找到了我的答案,但现在我的列表视图开始关注某些事情,所以当我尝试单击要选择的列表项时,什么也没有发生。我进行了一些研究,并了解到当这种情况发生时,某些事情会成为活动的焦点,而我唯一能想到的就是我的 scollview。我尝试将android:focusable="false">我的 xml 文件放在我的滚动视图下,但我仍然得到相同的结果。所以我只是希望有人能给我一些建议

列表活动:

public class List extends ListActivity {    


    ArrayList<HashMap<String, String>> questionList;        

     final String TAG_RESULTS = "results";
     static 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;          

    @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 onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

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

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


        @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://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=4vCW8F3V34GzdMlXOS.yc2WfF5DCnCgqhK0nwCJmEFDgRwEbIgnAoEgJ0zynqOAWtQ&query="+ query +"&search_in=question&sort=relevance&results=25&output=json";
                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     ; 



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

                android.widget.ListView lv = getListView();

                lv.setOnItemClickListener(new OnItemClickListener() {

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

                        String Subject = ((TextView) findViewById(R.id.Subject)).getText().toString();
                        String Content = ((TextView) findViewById(R.id.Content)).getText().toString();
                        String ChosenAnswer = ((TextView) 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);
                    }   

                });     

        }}

    }

列表.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/Subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/NumAnswers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/ChosenAnswer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/Content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:focusable="false">
    </ScrollView>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>
4

1 回答 1

0

尝试将您的listener外部移动到AsyncTask. 使其成为成员变量并在其中初始化onCreate()

public class List extends ListActivity {    

ListView lv;
ArrayList<HashMap<String, String>> questionList;        

 ...       

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

    questionList = new ArrayList<HashMap<String, String>>();
    lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {                    

           String Subject = ((TextView) findViewById(R.id.Subject)).getText().toString();
           String Content = ((TextView) findViewById(R.id.Content)).getText().toString();
           String ChosenAnswer = ((TextView) 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);
          }   

     });     

  new LoadAllData().execute();

AscncTask正在完成,所以你listener可能已经不在了。您仍然可以将您Adapter的设置在那里,只需移动onItemClick()和初始化。

编辑

因为它ListActivity你不需要在它上面设置监听器。只需实现方法

public void onListItemClick(ListView l, View v, int position, long id)
{
     // do your work
}
于 2013-08-11T20:34:54.193 回答