2

listview itemリスト項目をクリックしたときの取得に問題があります。(Arrayadapter)を手に入れlistview itemましたが、顔に問題があります。simple listviewcustom listview

私は使っている

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

              }

          });

Logcatにnull値を教えてください:

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


 }

編集:

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

}
4

5 に答える 5

3

カスタム アダプターnullのメソッドから戻る可能性があります(アダプターのコードを追加する必要があります)。また、再度リスナーを設定する必要はありません。getItem()ListviewAdapterListView

getItem()メソッドを次のように変更します。

@Override  
public Object getItem(int position) {     
    return Questions.get(position);  
}  
于 2012-09-08T07:18:03.870 に答える
2

間違ったアダプターを選択する可能性があります。これを試してください

String  selectedFromList =  myAdapter.getItemAtPosition(pos).toString();
于 2012-09-08T07:34:12.010 に答える
2

リストに対してこれを試してください:

list.setItemsCanFocus(false);

リスト内のアイテムに属性があることを確認してください

android:focusable="false"
android:focusableInTouchMode="false"
于 2012-09-08T07:17:19.150 に答える
1

これを使って

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

          }

      });
于 2012-09-08T07:16:44.290 に答える
0

これをやってみてください。

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

              }

          });
于 2012-09-08T07:26:33.670 に答える