Listview setOnItemClickListener - Not working for custom listview but working into simple listview
Asked Answered
C

5

2

I have problem with the get the listview item when click listitem. I Got listview item for the simple listview(Arrayadapter), but I have face problem for the custom listview.

I am using

listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {

  //String  selectedFromList = (String) listview.getItemAtPosition(pos);
   String  selectedFromList = (String) adapter.getItem(pos);

    System.out.println("selected value >> >> >> >> >> >>"+selectedFromList);

    SelectedAnswer.setAnswer(selectedFromList);

              }

          });

Into Logcat give me the null value :

I/System.out( 1238): selected value >> >> >> >> >> >>null

main.class

 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_questions);

        listview = (ListView) findViewById(R.id.questions_list);

        GoToNextQuestion();
  }

private void GoToNextQuestion() {
    // TODO Auto-generated method stub

     currentGame = ((Quizapplication)getApplication()).getCurrentGame();
    currentQ = currentGame.getNextQuestion();

       setQuestions();

    listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {

  //String  selectedFromList = (String) listview.getItemAtPosition(pos);
   String  selectedFromList = (String) adapter.getItem(pos);

    System.out.println("selected value >> >> >> >> >> >>"+selectedFromList);

    SelectedAnswer.setAnswer(selectedFromList);

              }

          });


}

 private void setQuestions() {


    // set the available options
    List<String> answers = currentQ.getQuestionOptions();
    System.out.println("value of the options >>>>>>>>>>>>>:"+answers); 


     //ArrayAdapter< String> myadapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice ,answers);

    //ArrayAdapter< String> adapter = new ArrayAdapter<String>(this, R.layout.quiz_questions_listitem, answers);        

             // working for the simple adapter

      adapter = new ListviewAdapter(this,answers);

     listview.setAdapter(adapter);
     //listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}


 }

EDIT:

 public class ListviewAdapter extends BaseAdapter{

public List<String> Questions;  

public Activity context;  
public LayoutInflater inflater;


public ListviewAdapter(Activity context,List<String> answers) {  
    super();  

    this.context = context;  
    this.Questions = answers;  

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}  

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return Questions.size();  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
    return null;  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}  

public static class ViewHolder  
{  

    TextView txtquestion;  
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  

    ViewHolder holder;  
    if(convertView==null)  
    {  
        holder = new ViewHolder();  
        convertView = inflater.inflate(R.layout.quiz_questions_listitem, null);  


        holder.txtquestion = (TextView) convertView.findViewById(R.id.textView_option);  

        convertView.setTag(holder);  

      /*  convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>"+v.toString());
                notifyDataSetChanged();

                 SelectedAnswer.setAnswer(v.toString());
            }
        });*/
    }  
    else  
        holder=(ViewHolder)convertView.getTag();  

    holder.txtquestion.setText(Questions.get(position));  

    return convertView;  
}   

}
Cesium answered 8/9, 2012 at 7:11 Comment(0)
S
3

You probably return null from the getItem() method of your custom adapter, ListviewAdapter(you should add the code for the adapter). Also, there is no need to set the listener on the ListView again.

Modify your getItem() method like this:

@Override  
public Object getItem(int position) {     
    return Questions.get(position);  
}  
Suppurative answered 8/9, 2012 at 7:18 Comment(3)
Thanks,Here I displayed the content into listview success, but problem with the get the selected listview item.Cesium
@RahulPatel Please add to your question the code of your adapter ListviewAdapter.Suppurative
pls check the ListviewAdapterCesium
P
2

Try to do this for your list :

list.setItemsCanFocus(false);

and make sure that items inside your list had attributes

android:focusable="false"
android:focusableInTouchMode="false"
Polysaccharide answered 8/9, 2012 at 7:17 Comment(0)
I
2

you may select wrong adapter, try this

String  selectedFromList =  myAdapter.getItemAtPosition(pos).toString();
Irradiation answered 8/9, 2012 at 7:34 Comment(1)
Thanks, but I had problem in my Adapter class. It's solved now.Cesium
G
1

Use this

   public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {


   String  selectedFromList = myAdapter.getItemAtPosition(pos);

  System.out.println("selected value >> >> >> >> >> >>"+selectedFromList);

  SelectedAnswer.setAnswer(selectedFromList);

          }

      });
Galoot answered 8/9, 2012 at 7:16 Comment(2)
thanks, but getItemAtPosition is not option for the myadapter. it,s for the listviewCesium
as per String selectedFromList = (String) listview.getItemAtPosition(pos);Cesium
T
0

try to Do this.

public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {

  //String  selectedFromList = (String) listview.getItemAtPosition(pos);

   ListviewAdapter adapter=(ListviewAdapter) listview.getAdapter();
   String  selectedFromList = (String) adapter.getItem(pos);

    System.out.println("selected value >> >> >> >> >> >>"+selectedFromList);

    SelectedAnswer.setAnswer(selectedFromList);

              }

          });
Toolis answered 8/9, 2012 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.