我正在创建一个应用程序,其中包含一个功能,其中包含一个问题列表,每个问题都有多个答案选项。为此,我使用listview,每个项目包含一个TextView,用于显示包含答案列表的微调器的问题。我试图在每个listview项目中检索当前选定的微调器值。这通常很容易,除了我为每个listview项目都有一个微调器,我需要的微调器的数量是"未知"直到问题的数量加载。
我的问题是如何获取每个当前选定的微调器项的值。目前我只能获得第一个微调项目的值。下面是我使用自定义适配器的类的代码:
questionsList = new ArrayList<>();
masterAnswersList = new ArrayList<>();
//For all questions with the tech answer's group id, get questions
for(int k = 0; k < allQuestions.size(); k++) {
final int z = k;
//Each question for the tech
System.out.println("Question " + z + " " + allQuestions.get(z).getQuestionTitle());
questionsList.add(new CompleteQuestions.ListViewItem() {{
QUESTION = allQuestions.get(z).getQuestionTitle();
}});
int questionId = allQuestions.get(z).getQuestionId();
//Get all possible answers for this question based on the question and the group id
allAnswers = db.getAllGroupAnswers(questionId, groupID, database);
answersList = new ArrayList<>();
//For all answers for this question, add to arraylist
for(int j = 0; j < allAnswers.size(); j++) {
final int p = j;
answersList.add(new CompleteQuestions.ListViewItem() {{
ANSWER = allAnswers.get(p).getAnswerTitle().trim();
}});
}
masterAnswersList.add(answersList);
}
adapter = new CompQuestionsAdapter(this, questionsList, masterAnswersList);
completeQuestionsListView.setAdapter(adapter);
//For the number of items in the listview
for (int i = 0; i < completeQuestionsListView.getCount(); i++) {
//Update ticket questions table
String techChoice = CompQuestionsAdapter.spinner.getSelectedItem().toString();
System.out.println("techChoice: " + techChoice);
//Get id of answer tech chose
int techAnswerId = db.getAnswerId(techChoice, database);
int questionId = allQuestions.get(i).getQuestionId();
int groupId = db.getGroupId(techChoice, questionId, database);
System.out.println("Group id: " + groupId);
Ticket questionTicket = new Ticket(questionId, techAnswerId, ticketId, companyId, groupId, true);
db.addTicketQuestionResponse(questionTicket, context, database);
}
自定义适配器本身的一部分:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/**
* The database used to pull all ticket information
*/
final DatabaseHandler db = new DatabaseHandler(context);
final SQLiteDatabase database = db.getReadableDatabase();
ListViewItem item = questions.get(position);
//ListViewItem item2 = answers.get(position).get(position);
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.comp_questions_view, null);
TextView question = (TextView)vi.findViewById(R.id.QUESTION);
question.setText("" + item.QUESTION);
spinner = (Spinner)vi.findViewById(R.id.ANSWERS);
List<String> strAnswers = new ArrayList<>();
List<CompleteQuestions.ListViewItem> currentAnswers = answers.get(position);
for (int i = 0; i < currentAnswers.size(); i++) {
strAnswers.add(currentAnswers.get(i).ANSWER);
}
//Place all the cleared by codes in a string array used to populate the spinner
String[] strArrayAnswers = new String[strAnswers.size()];
for(int i = 0; i < strAnswers.size(); i++) {
if(strAnswers.get(i) != null) {
strArrayAnswers[i] = strAnswers.get(i);
}
}
adapter = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item, strArrayAnswers);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
List<String> selections = new ArrayList<>();
selections.add(spinner.getSelectedItem().toString());
System.out.println("Currently selected item: " + spinner.getSelectedItem());
return vi;
}
对此的任何帮助将不胜感激。
答案 0 :(得分:2)
首先,您需要定义一个接口,以便您的适配器类可以用来告诉父类从位置中的微调器中选择了哪个项目,以及什么是 selectedItem 强>
OnSpinnerItemSelected接口
public interface OnSpinnerItemSelected {
void onItemSelected(int position, String selectedItem);
}
然后,您需要在适配器构造函数中传递一个OnSpinnerItemSelected对象。
private OnSpinnerItemSelected onSpinnerItemSelected;
public CompQuestionsAdapter(......, OnSpinnerItemSelected onSpinnerItemSelected) {
this.onSpinnerItemSelected = onSpinnerItemSelected;
}
在使用custem适配器的类中添加:
adapter = new CompQuestionsAdapter(this, questionsList, masterAnswersList, new OnSpinnerItemSelected() {
@Override
public void onItemSelected(int position, String selectedItem) {
}
});
在你的适配器中调用onItemSelected方法(在getView方法中)
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
onSpinnerItemSelected.onItemSelected(position, adapterView.getAdapter().getItem(i));
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
如果不清楚,请告诉我。