我以RelativeLayout
为ScrollView
作为根元素。在RecyclerView
我有spinner
和TextView
。现在我从JSON
获取数据,例如RollNumber
和Marks
。现在在TextView
,我需要显示RollNumber
,并且应在Spinner
Marks
中显示。我已经获得了ArrayList<Progress>
中的数据,对于这种情况,代码应该是什么样的;
RelativeLayout
for for循环? OnCreate
for (int i = 0; i < progress.size(); i++) {
progress_addmore_spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
loadspinner
}
private void loadSpinners()
{
if (progress != null || progress.size() > 0) {
ArrayList<String> progresscard = new ArrayList<>();
for (int i = 0; i < progress.size(); i++) {
progresscard.add(progress.get(i).toString());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
progresscard);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
progress_addmore_spinner.setAdapter(dataAdapter);
if (progress.get(0).getMarks() != null && !progress.get(0).getMarks().isEmpty()) {
product_addmore_spinner.setSelection(progresscard.indexOf(toppingtypelist.get(0).getMarks()));
}
}
}
答案 0 :(得分:0)
您可以设置包含适配器的微调器,它是recyclerview适配器的onBindViewHolder中的默认值。同时将位置设置为适配器中微调器的标记,如:vh.view.setTag(position);
现在在活动或片段中实现OnItemSelectedListener并将其传递到recyclerview适配器。由于您已将微调器的位置设置为标记,因此可以轻松地在ArrayList中提取progress对象的索引。
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int index = (int) parent.getTag();
parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};