0

I'm trying to do something very simple: create a frequently asked questions listview, and when a question is clicked, the answer appears in a textview. I created two string arrays for questions and answers, and each element is listed as an item. There are 5 q's and 5 a's. Right now the questions are being displayed correctly in a listview, but the onclick isn't working. What is wrong??

package freq.asked;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class FreqActivity extends ListActivity {

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //displays all elements of questions array in listview
            setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
                    R.array.questions, R.layout.main));
    }

    public void onItemClick(AdapterView<?> arg0, TextView v, int position, long id) {
        String[] ans = getResources().getStringArray(R.array.answers);
        for (int i=0; i<6; i++) {
                    //should display answer to question in textview
            v.setTag(ans[i]);
        }
    }
}
mdegges
  • 963
  • 3
  • 18
  • 39

2 Answers2

1

I believe you are using the wrong method, try onListItemClick() instead:

protected void onListItemClick(ListView l, View v, int position, long id) {

An example:

public class FreqActivity extends ListActivity {
    String[] ans;
    String[] questions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ans = getResources().getStringArray(R.array.answers);
        questions = getResources().getStringArray(R.array.questions);
        //displays all elements of questions array in listview
        setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
                R.array.questions, android.R.layout.simple_list_item_1));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        ((TextView) v).setText(questions[position] + ": " + ans[position]);
    }
}

Notice that you don't need to get the ans every time the user clicks a row, you only need to do it once in onCreate()


If you only wan to start a new Activity after clicking a "question" use this in onListItemClick():

Intent i = new Intent(this, AnswerActivity.class);
i.putExtra("answer", ans[position]);
startActivity(i);

You can read about how to read this data here: How do I pass data between Activities in Android application?

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • thank you, but that is not quite right. i think what that does is set the question in the listview to the text of the answer. – mdegges Sep 19 '12 at 21:03
  • Well, you only specified "when a question is clicked, the answer appears in a textview". How about this? You can add any format that you want, but I used `": "` as a simple spacer. – Sam Sep 19 '12 at 21:08
  • sorry, i meant that it should open a new page with a textview in it, and display the text of the answer. but i think i can figure that out! – mdegges Sep 19 '12 at 21:12
0

Please use the following replacing the method onItemClick,

protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
}
praveenLal
  • 94
  • 2
  • 12