我希望旋转位置与选定的项目文本

时间:2017-03-03 09:57:00

标签: android

这是我的带有arrayadapter类的微调器。当我滚动我的listview我的微调器项值改变所以我在这里使用(Map myMap = new HashMap();)

但是我无法通过listview项目获得选定的微调项目..

public class Lv_001 extends ArrayAdapter<String> {

    Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
    List<String> categories;
    String[] name;
    Context context;
    TextView textView;
    Spinner spinner;
    String data, val;
    String sp = null;
    ArrayList<String> list = new ArrayList<>();

    public Lv_001(Context context, String[] name, List<String> categories) {
        super(context, R.layout.lv_list, categories);
        this.name = name;
        this.categories = categories;
        this.context = context;
    }
    public View getView(final int position, View view, ViewGroup group) {
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.lv_list, null);

        textView = (TextView) view.findViewById(R.id.textView);

        data = categories.get(position);
        textView.setText(data.toString());

        spinner = (Spinner) view.findViewById(R.id.spinner);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, name);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(spinnerArrayAdapter);
        if (myMap.containsKey(position)) {
            spinner.setSelection(myMap.get(position));
        }
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int spinnerPosition, long id) {

                myMap.put(position, spinnerPosition);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个解决方案

获取一个Hashmap temp = new Hashmap&lt;&gt;();

for(i=0;i<list.size();i++){
temp.put(i,0)
}

现在,当用户在该索引处更改微调器时,请更改hashmap值 例如,索引2已被更改

temp.put(position,spinner.getselecteditemposition())

当您使用此临时HashMap

加载视图集选择您的微调器时

最后一件事就是使用notifydatasetchanged()