如何从ListView中的视图访问Adapter对象

时间:2017-08-09 19:43:56

标签: android android-arrayadapter

我很长一段时间回到Android,我有点陷入困境 该应用程序由一系列布局组成,每个布局都通过适配器由模型支持 每个布局都有几个视图,其中一个是Spinner 我想在更改Spinner的值时修改适配器中的数据,但我不知道如何在适配器中引用该对象。

public class MainActivity extends AppCompatActivity {
    ArrayList<Troop> values = new ArrayList<Troop>();
    MySimpleArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        values.add(new Troop());

        final ListView listView =(ListView) findViewById(R.id.listview);

        adapter = new MySimpleArrayAdapter(
                this,
                values);
        listView.setAdapter(adapter);
    }

    public class Troop {
        private String weapon;
        private int bs;
        private int total_modifier;

        public Troop() {

        }

        public int getBs() {
            return bs;
        }

        public void setBs(int bs) {
            this.bs = bs;
        }
    }

    public class MySimpleArrayAdapter extends ArrayAdapter {
        private final Context context;
        private ArrayList<Troop> troopers;
        private String[] possible_bs = new String[] {"10", "11", "12", "13", "15", "16"};

        public MySimpleArrayAdapter(Context context, ArrayList<Troop> values) {
            super(context, -1, values);
            this.context = context;
            this.troopers = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

                Spinner bs_spinner = rowView.findViewById(R.id.bs_spinner);
                ArrayAdapter<String> bs_adapter = new ArrayAdapter<String>(
                        context,
                        android.R.layout.simple_spinner_dropdown_item,
                        possible_bs);
                bs_spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
                bs_spinner.setAdapter(bs_adapter);
            return rowView;
        }

    }

    public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        // I want to call getBs on the Trooper correspoding to this view here
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }

    }
}

2 个答案:

答案 0 :(得分:0)

我要解决的问题很多。首先,让我们谈谈你的ArrayAdapter子类。

正如您对问题的评论中所述,您应将其更改为extends ArrayAdapter<Troop>。这将导致getItem()返回Troop而不是Object

您无需存储自己的contexttroopers字段。基本ArrayAdapter实现在[{1}}字段中存储了一个Context,您可以通过mContext访问它。此外,它将支持数据存储在getContext()字段中,您可以通过mObjects访问它。

最后,如果适配器中的所有项目都将共享相同的getItem()值,则应将其设为常量。

接下来让我们谈谈possible_bs

您需要一种方法来了解您的AdapterView.OnItemSelectedListener所居住的位置(来自MySimpleArrayAdapter)(而不是内的位置 {{1}用户已选择)。我建议将此位置传递给构造函数并将其保存为私有final字段。

要从Spinner内部获取适配器,您可以通过调用Spinner访问支持Spinner参数的Adapter,然后将其转换为所需类型

总之,你离开了这个:

AdapterView

答案 1 :(得分:0)

您可以移入匿名课程。

如果您想要当前的ListView元素以及内部适配器位置,您希望获得两个位置元素。

这可能是更清洁的方法,但这是我的第一个想法

public class MySimpleArrayAdapter extends ArrayAdapter<Troop>  {
    private final Context context;
    private ArrayList<Troop> troopers;
    private String[] possible_bs = new String[] {"10", "11", "12", "13", "15", "16"};

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
            Spinner bs_spinner = rowView.findViewById(R.id.bs_spinner);

            final ArrayAdapter<String> bs_adapter = new ArrayAdapter<String>(
                    context,
                    android.R.layout.simple_spinner_dropdown_item,
                    possible_bs);
            bs_spinner.setAdapter(bs_adapter);

            bs_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    // You can access MySimpleArrayAdapter.this or bs_adapter
                    Troop t = troopers.get(position); // or getItem(position);
                    String bs = possible_bs[pos];
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                }

            });

        return rowView;
    }