sách gpt4 ăn đã đi

android - Listview setOnItemClickListener - 不适用于自定义 ListView ,但适用于简单 ListView

In lại 作者:太空狗 更新时间:2023-10-29 15:55:23 32 4
mua khóa gpt4 giày nike

我在单击列表项时获取 listview 项 时遇到问题。我得到了 simple listview(Arrayadapter) 的 listview item,但我遇到了 custom listview 的问题。

tôi đang sử dụng

listview.setOnItemClickListener(new OnItemClickListener() {

@Ghi đè
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 给我 vô giá trị 值:

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

主类

 được bảo vệ void onCreate(Gói savedInstanceState) {
// TODO Phương thức tự động tạo stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_questions);

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

GoToNextQuestion();
}

private void GoToNextQuestion() {
// TODO Phương thức tự động tạo stub

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

setQuestions();

listview.setOnItemClickListener(new OnItemClickListener() {

@Ghi đè
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 answers = currentQ.getQuestionOptions();
System.out.println("value of the options >>>>>>>>>>>>>:"+answers);


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

//ArrayAdapter< String> adapter = new ArrayAdapter(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);
}


}

biên tập:

 public class ListviewAdapter extends BaseAdapter{

public List Questions;

public Activity context;
public LayoutInflater inflater;


public ListviewAdapter(Activity context,List answers) {
siêu();

this.context = ngữ cảnh;
this.Questions = answers;

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

@Ghi đè
public int getCount() {
// TODO Phương thức tự động tạo stub
return Questions.size();
}

@Ghi đè
public Object getItem(int position) {
// TODO Phương thức tự động tạo stub
trả về giá trị null;
}

@Ghi đè
public long getItemId(int position) {
// TODO Phương thức tự động tạo stub
trả về 0;
}

public static class ViewHolder
{

TextView txtquestion;
}

@Ghi đè
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Phương thức tự động tạo 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() {

@Ghi đè
public void onClick(View v) {
// TODO Phương thức tự động tạo stub

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

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

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

return convertView;
}

}

câu trả lời hay nhất

您可能从自定义适配器 ListviewAdapter của getItem() 方法返回 vô giá trị(您应该为适配器添加代码) .此外,无需再次在 ListView 上设置监听器。

像这样修改您的 getItem() phương pháp:

@Ghi đè  
public Object getItem(int position) {
return Questions.get(position);
}

关于android - Listview setOnItemClickListener - 不适用于自定义 ListView ,但适用于简单 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328804/

32 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress