如何使“微调器onItemSelected()”从“字符串.xml”中的数组更新“列表视图”项



从Android开发开始,我有一个关于ListView更新的问题。按照一些教程,我能够制作:

  • Spinner 4 个项目(客户 A、客户 B、客户 C、客户 D);
  • ListView CheckboxTextView说每个客户的项目;

每个客户都有自己的项目,这个想法是在Spinner中选择每个客户项目时更新ListView以显示每个客户项目,例如:

  1. Client A -> 项目 1、项目 2、项目 3、(...)、项目 7;
  2. Client B ->项目 8、项目 9、(...)、项目 16。

为此,我使用引用single_row.xmlBase Adapter来设置每个视图的布局。

有关客户端的信息在strings.xml文件中名为 clientes_array 的数组上设置。在那里,我还有用于projectsAprojectsB的数组.

strings.xml

<string-array name="clientes_array"> <item>Cliente A</item> <item>Cliente B</item> <item>Cliente C</item> <item>Cliente D</item> </string-array> <string-array name="projectoA"> <item>Projecto #1</item> <item>Projecto #2</item> <item>Projecto #3</item> <item>Projecto #4</item> <item>Projecto #5</item> <item>Projecto #6</item> <item>Projecto #7</item> </string-array> <string-array name="projectoB"> <item>Projecto #8</item> <item>Projecto #9</item> <item>Projecto #10</item> <item>Projecto #11</item> <item>Projecto #12</item> <item>Projecto #13</item> <item>Projecto #14</item> <item>Projecto #15</item> <item>Projecto #16</item> </string-array>

这是我Activity代码:

'公共类AddProj_Activity扩展 活动实现 AdapterView.OnItemSelectedListener {

Spinner spinner1;
Button botaoAdd;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addproj_layout);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    botaoAdd = (Button) findViewById(R.id.addProj);
    listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(new Adaptador(this));
    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,
            R.array.clientes_array, R.layout.spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter2);
    spinner1.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub
}
class SingleRow {
    String projecto;
    public SingleRow(String projecto) {
        // TODO Auto-generated constructor stub
        this.projecto = projecto;
    }
}
class Adaptador extends BaseAdapter {
    ArrayList<SingleRow> listSR;
    Context context;
    public Adaptador(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
        listSR = new ArrayList<SingleRow>();
        Resources res = c.getResources();
        String[] projecto = res.getStringArray(R.array.projectoA);
        for (int i = 0; i < projecto.length; i++) {
            listSR.add(new SingleRow(projecto[i]));
        }
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listSR.size();
    }
    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return listSR.get(i);
    }
    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {// call to each row
        // TODO Auto-generated method stub
        // Inflater -> goes toxml, reads properties and creates the object with those properties!
        // new object everytime: layout inflater || same object everytime:
        // findViewById
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_row, viewGroup, false);// ref to RelativeLayout
        TextView tV = (TextView) row.findViewById(R.id.textView1);
        CheckBox cBx = (CheckBox) row.findViewById(R.id.checkbox);
        SingleRow temp = listSR.get(i);
        tV.setText(temp.projecto);
        return row;
    }
}

}'

如何让特定客户端获取项目数组?

Client A得到所有的projectsAClient B得到所有的projectsB,等等??

我现在应该怎么做?

提前感谢!

添加此方法修改适配器:

public void swapData(int resId) {
   Resources res = c.getResources();
   String[] projecto = res.getStringArray(resId);
   listSR.clear();
   for (int i = 0; i < projecto.length; i++) {
      listSR.add(new SingleRow(projecto[i]));
   notifyDatasetChanged(); 
}

然后在您的 onItemSelectedListener 中:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
    long id) {
    Adaptor adapter = (Adaptor)listView.getAdapter();
    switch (position) {
       case 0: {
          adapter.swapData(R.array.projectoA);
          break;
       }
       /* and so on*/
    }
}
   @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position,
                long id) {
            adapter2.clear();
            switch (position) {
            case 0:
                // fill adapter2 with client A
                break;
            case 1:
                // fill adapter 2 with clientB 
                break;
            }
        }

每次单击列表视图中的项目时,都会检查单击的位置并修改适配器的内容

相关内容

  • 没有找到相关文章

最新更新