您好我是Android的新手,在我的ListView中,我添加了Spinner。
我遇到问题,当我从微调器中选择任何项目时,我滚动我的ListView。 Spinner setSelection数据正在消失。
请帮助我如何解决此问题?
public class MainActivity extends AppCompatActivity {
private ArrayList<Friend> friendArrayList;
String[] countriessArray = { "USA", "USA", "USA", "USA",
"China", "Japan", "France", "USA","USA", "USA", "USA", "USA",
"China", "Japan", "France", "USA"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
friendArrayList = new ArrayList<Friend>();
for (int i = 0; i < countriessArray.length; i++) {
Friend item = new Friend(countriessArray[i]);
friendArrayList.add(item);
}
ListView listView = (ListView) findViewById(R.id.list);
ListViewAdapter adapter = new ListViewAdapter(this, R.layout.item_listview, friendArrayList);
listView.setAdapter(adapter);
}
}
public class ListViewAdapter extends ArrayAdapter<Friend> implements AdapterView.OnItemSelectedListener {
private List<Friend> myFriends;
private Activity activity;
private int selectedPosition = -1;
public ListViewAdapter(Activity context, int resource, List<Friend> objects){
super(context, resource, objects);
this.activity = context;
this.myFriends = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_listview, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setTag(position); // This line is important.
holder.friendName.setText(getItem(position).getName());
if (position == selectedPosition) {
holder.checkBox.setChecked(true);
} else holder.checkBox.setChecked(false);
holder.checkBox.setOnClickListener(onStateChangedListener(holder.checkBox, position));
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
holder.spinner.setAdapter(dataAdapter);
holder.spinner.setOnItemSelectedListener(this);
return convertView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()) {
selectedPosition = position;
} else {
selectedPosition = -1;
}
notifyDataSetChanged();
}
};
}
private static class ViewHolder {
private TextView friendName;
private CheckBox checkBox;
private Spinner spinner;
public ViewHolder(View v) {
checkBox = (CheckBox) v.findViewById(R.id.check);
friendName = (TextView) v.findViewById(R.id.name);
spinner = (Spinner) v.findViewById(R.id.spinnerList);
}
}
}
public class Friend {
private String name;
public Friend(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
答案 0 :(得分:1)
更好的方法是...... 在您的Pojo类朋友中添加选择作为属性。
像: int selection = 0; 添加getter setter。
在OnItemselected中,将其值设置为微调器的选定位置。 在你的getview中,你需要添加条件并从其getter方法中获取值。
将课程更改为以下内容:
rescan_Target
在适配器视图中,
更改此holder.spinner.setOnItemSelectedListener(this); 对此:
public class Friend {
private String name;
private int selection = 0;
public Friend(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSelection(int selection) {
this.selection = selection;
}
public int getSelection() {
return selection;
}
}
在你的getView听众之后, 添加
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
getItem(position).setselection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});;
答案 1 :(得分:0)
这是正常的,因为每次滚动时都会调用getView来获取单元格的视图,并在那里重置微调器项目。
holder.spinner.setAdapter(DataAdapter的); 这行(以及初始化dataAdapter并设置单击侦听器的行)应该放在if(convertView == null)块中(这是初始化单元格视图的位置)。
在onSpinnerClick方法中,您应该跟踪每个单元格的选定项目,在返回convertView之前,您应该设置holder.spinner.setSelectedItem(),以便每个单元格的微调器都显示以前选择的项目