从Android dev开始,我对ListView
的更新有疑问。
按照一些教程,我能够做到:
Spinner
有4个项目(客户A,客户B,客户C,客户D); ListView
Checkbox
和TextView
说每个客户的项目; 每个客户都有自己的项目,我们的想法是更新ListView
,以便在Spinner
中选择时显示每个客户项目,例如:
Client A
- >项目1,项目2,项目3,(......),项目7; Client B
- >项目8,项目9,(......),项目16。为此,我使用Base Adapter
引用single_row.xml
来为每个视图设置布局。
有关客户端的信息是在clientes_array
文件中名为strings.xml
的数组上设置的。我还有projectsA
和projectsB
的数组。
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
代码:
`public class AddProj_Activity扩展了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
获取所有projectsA
,Client B
获取所有projectsB
等等。
我现在该怎么办?
提前致谢!
答案 0 :(得分:3)
修改您的适配器添加此方法:
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*/
}
}
答案 1 :(得分:1)
@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;
}
}
每次单击列表视图中的项目时,都会检查单击的位置并重新调整适配器的内容