如何在使用onitemselected侦听器单击微调器的项目时显示列表视图?

时间:2014-11-03 16:59:30

标签: android android-listview

我编写了一个显示微调器的程序,但是当我选择微调器的任何项目时,我想在同一个活动中显示一个列表视图,我还编写了一个自定义适配器的代码来显示列表视图中的项目和listviews行有6个textviews,其中所有数据都来自strings.xml文件中定义的String-array,所以需要一些帮助

Result_Date.java有一个微调器和资源,不知道里面要调用什么" onitemselected"方法

public class Result_Date extends Activity {

ImageView iv;
String[] years = { "Select year", "2000", "2001", "2002", "2003", "2004",
        "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012",
        "2013", "2014" };
ArrayAdapter<String> adapter;
ListView lv;
String[] name;
String[] mobile;
String[] gender;
String[] age;
String[] disease;
String[] day;
String[] month;
String[] year;

int[] images = { R.drawable.photo_bg, R.drawable.photo_bg,
        R.drawable.photo_bg, R.drawable.photo_bg, R.drawable.photo_bg,
        R.drawable.photo_bg, R.drawable.photo_bg, R.drawable.photo_bg };

Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_list_year);
    spinner = (Spinner) findViewById(R.id.btnShowYear);
    iv = (ImageView) findViewById(R.id.imgBackReuslt);
    adapter = new ArrayAdapter<String>(getBaseContext(),
            R.layout.spinner_item, R.id.textView1, years) {
        @Override
        //this method is used to hide the default text "Select Year" which repeats twice
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {

            View v = null;

            if (position == 0) {
                TextView tv = new TextView(getContext());
                tv.setHeight(0);
                tv.setVisibility(View.GONE);
                v = tv;
            } else {

                v = super.getDropDownView(position, null, parent);
            }

            parent.setVerticalScrollBarEnabled(false);
            return v;
        }
    };

    adapter.setDropDownViewResource(R.layout.spinner_item);
    spinner.setAdapter(adapter);

    Resources res = getResources();
    name = res.getStringArray(R.array.Name);
    mobile = res.getStringArray(R.array.Mobile);
    gender = res.getStringArray(R.array.Gender);
    age = res.getStringArray(R.array.Age);
    disease = res.getStringArray(R.array.DiagnosisName);
    day = res.getStringArray(R.array.day_array);
    month = res.getStringArray(R.array.month_year);
    year = res.getStringArray(R.array.year_array);

    lv = (ListView) findViewById(R.id.lstShowYear);
    MyAdapter adapter = new MyAdapter(getBaseContext(), name, mobile,
            gender, age, images, day, month, year, disease);
    lv.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @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

        }
    });
}

public void onClick(View v) {
    super.onBackPressed();
    finish();
}

我的CustomAdapter MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {

Context context;
int[] images;
String[] namearray;
String[] mobilearray;
String[] genderarray;
String[] agearray;
String[] diseasearray;
String[] dayarray;
String[] montharray;
String[] yeararray;

public MyAdapter(Context c, String[] name, String[] mobile,
        String[] gender, String[] age, int[] img, String[] disease,
        String[] day, String[] month, String[] year) {
    super(c, R.layout.row_layout, R.id.textView1, name);
    // TODO Auto-generated constructor stub
    this.context = c;
    this.namearray = name;
    this.mobilearray = mobile;
    this.genderarray = gender;
    this.agearray = age;
    this.diseasearray = disease;
    this.dayarray = day;
    this.montharray = month;
    this.yeararray = year;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ListView lv;

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.row_layout, parent, false);
    ImageView image = (ImageView) row.findViewById(R.id.imgView);
    TextView text1 = (TextView) row.findViewById(R.id.txtName);
    TextView text2 = (TextView) row.findViewById(R.id.txtMobile);
    TextView text3 = (TextView) row.findViewById(R.id.txtGender);
    TextView text4 = (TextView) row.findViewById(R.id.txtAge);
    TextView text5 = (TextView) row.findViewById(R.id.txtDesease);
    TextView text6 = (TextView) row.findViewById(R.id.txtDate);
    lv = (ListView) row.findViewById(R.id.listView1);

    image.setImageResource(images[position]);
    text1.setText(namearray[position]);
    text2.setText(mobilearray[position]);
    text3.setText(genderarray[position]);
    text4.setText(agearray[position]);
    text5.setText(diseasearray[position]);
    text6.setText(dayarray[position]);
    text6.setText(montharray[position]);
    text6.setText(yeararray[position]);

    return row;
}

}

我是否需要编写定义Resources obj的代码,直到在onitemselected方法中设置适配器? 欢迎任何建议

1 个答案:

答案 0 :(得分:0)

我会做以下事情:

在我的ListAdapter中创建一个名为例如&#34; refreshList&#34;的方法。可以将您需要的所选微调器项目中的数据作为参数&#34;得到&#34;通过微调器选择的值填充列表所需的数据。例如,如果更改列表的数据是YEAR,则函数可能是这样的:

public void refreshList(String year) {
    //Fill the arrays with the data according to the year
    namearray = getResources().getStringArray(R.array.Name);
    //Do loas all the data you need according to the year here
    //And the refresh the adapter
    this.notifyDataSetChanged();
}

然后在你的onItemSelected调用spinner中调用ListAdapter的函数,如adapter.refreshList(years[position])或类似的东西。它只是一个想法,有很多方法可以做到这一点,重点是你在一个名为notifyDataSetChanged()的适配器中有一个函数,它再次加载列表,其中包含当前处理的数据列表/数组中的数据适配器内部。如果微调器选定的值将更改列表数据,那么您应该使用它来在选择微调器值时刷新列表。希望它有所帮助