点击listitem时获取listview item
时遇到问题。
我listview item
(Arrayadapter)获得simple listview
,但我遇到custom 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;
}
}
答案 0 :(得分:3)
您可能从自定义适配器的null
方法返回getItem()
ListviewAdapter
(您应该添加适配器的代码)。此外,无需再次在ListView
上设置侦听器。
像这样修改getItem()
方法:
@Override
public Object getItem(int position) {
return Questions.get(position);
}
答案 1 :(得分:2)
尝试为您的列表执行此操作:
list.setItemsCanFocus(false);
并确保列表中的项目具有属性
android:focusable="false"
android:focusableInTouchMode="false"
答案 2 :(得分:2)
您可以选择错误的适配器,试试这个
String selectedFromList = myAdapter.getItemAtPosition(pos).toString();
答案 3 :(得分: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);
}
});
答案 4 :(得分: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);
}
});