如何在按钮上显示列表视图点击同一页面?

时间:2014-11-05 20:11:42

标签: android listview

我写了一个程序,其中我有一个按钮,我想显示点击按钮时的年份列表,我总是在按钮的onclick方法上出现错误(空指针异常),我是还张贴了我希望我的列表看起来的图片,我有完全相同的按钮,请告诉我我哪里出错了

enter image description here

下面是我的代码

public class Result_Date extends Activity {

ImageView iv;
String[] years = {"2000", "2001", "2002", "2003", "2004",
        "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012",
        "2013", "2014" };

CustomAdapter customAdapter;
Button buttonShowYears;
AdapterNew adapter;
ListView listShowYear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_list_year);
    buttonShowYears = (Button) findViewById(R.id.btnShowYear);

    listShowYear = (ListView) findViewById(R.id.listView1);

    buttonShowYears.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            customAdapter = new CustomAdapter(getBaseContext(),
                    R.layout.row, years);
            //also tried with the android.R.layout.simple_list_item_1 but not working
            listShowYear.setAdapter(customAdapter);
        }

    });

我的CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<String> {

Context context;
ArrayList<String> arrayList;
String[] years;

public CustomAdapter(Context context, int resource, String[] years) {
    super(context, resource, years);
    this.context = context;
    this.years = years;
}

public static class ViewHolder {
    TextView txtname;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.row, null);
        holder = new ViewHolder();

        holder.txtname = (TextView) convertView.findViewById(R.id.txtName);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.txtname.setText(years.length);


    return convertView;
}

1 个答案:

答案 0 :(得分:3)

根据您的图片,您似乎想要Spinner

您可以使用状态选择器和9-patch drawable来更改默认微调器的外观。 Here是一个非常好的教程。