如何动态加载ListView取决于Spinner的值

时间:2012-10-19 11:18:07

标签: android android-layout android-intent

我在视图中有一个微调器,编辑文本,按钮和一个列表视图。我想从数据库加载我的ListView取决于spinner的值。我也在列表视图上添加操作。

我的疑问是我想知道如何加载我的列表视图取决于来自微调器的数据?

public class myClass extends ListActivity implements OnClickListener {
DatabaseFAM db;
Spinner No;
String Selected;
EditText user;
Button btnAdd;
String strselected;
ArrayList<String> list =new ArrayList<String>();
ArrayAdapter<String> adapterLV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myews);
    db = new DatabaseFAM(getApplicationContext());
    initVar();
}


private void initAudit2Var() {
    No = (Spinner) findViewById(R.id.station);
    user = (EditText) findViewById(R.id.er);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);
    adapterLV = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, list);
    setListAdapter(adapterLV);
    final DatabaseFAM db = new DatabaseFAM(getApplicationContext());
    List<String> station = db.getAllstation();
    ArrayAdapter<String> stationAdapter = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_spinner_item,
            station);
    stationAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    No.setAdapter(stationAdapter);
    No.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    Selected = arg0.getItemAtPosition(
                            arg2).toString();   
                    list.clear();
                    list=db.getTag(Selected);
                    adapterLV.notifyDataSetChanged();

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });
}

3 个答案:

答案 0 :(得分:0)

你应该在Spinner的Item选项中设置ListView的适配器....

        mSpinnerLangAttrValue.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            ** Add ListAdapter Here **
        }

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

        }
    });

答案 1 :(得分:0)

  1. 将onItemSelected事件添加到您的微调器。
  2. 根据选项的价值,使用所需数据修改listView
  3. 在listview上使用Datasetchanged事件

答案 2 :(得分:0)

          sp1 = (Spinner)findViewById(R.id.spinner1);
          sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                // Load your list here
                }}