如何在listview中获取视图的id

时间:2017-03-03 16:05:08

标签: android listview

我已经从数据库中获取了listview,但是我还有其他视图,比如列表视图中的按钮,我没有使用适配器获取,我需要在某些情况下隐藏它。问题是,我不知道如何获得Button的id。我尝试了Button button =(Button)findViewById(R.id.button),但这是返回null。任何帮助将不胜感激。

MainActivity

    private class GetData extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        if (dba.new() == null) {
            dba.insert2("updated");
            update();
        } else if (dba.new().equalsIgnoreCase("update")) {
            String time = dba.datumPondelok();
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEEEEEEE dd. M. yyyy");
            try {
                Date date = dateFormat.parse(time);
                Date newDate = new Date(date.getTime() + 518400000L);

                if (new Date().after(newDate)) {
                    update();
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();

        String[] from = new String[]{dba.MONDAY, dba.DATE_MONDAY, dba.TUESDAY, dba.DATE_TUESDAY, dba.WEDNESDAY, dba.DATE_WEDNESDAY,
                dba.THURSDAY, dba.DATE_THURSDAY, dba.FRIDAY, dba.DATE_FRIDAY, dba.HOD_MONDAY, dba.HOD_TUESDAY, dba.HOD_WEDNESDAY, dba.HOD_THUSDAY, dba.HOD_FRIDAY};
        ListAdapter rateAdapter = new SimpleAdapter(
                MainActivity.this, dba.giveList("1"), R.layout.list, from, to);
        ((SimpleAdapter) rateAdapter).setViewBinder(new MyBinder());
        lv.setAdapter(rateAdapter);
    }
    }

    class MyBinder implements SimpleAdapter.ViewBinder {
    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        if (view.getId() == R.id.ratingMonday || view.getId() == R.id.ratingTuesday || view.getId() == R.id.ratingWednesday
                || view.getId() == R.id.ratingThursday || view.getId() == R.id.ratingFriday) {
            String stringval = (String) data;
            float ratingValue = Float.parseFloat(stringval);
            RatingBar ratingBar = (RatingBar) view;
            ratingBar.setRating(ratingValue);
            return true;
        }
        return false;
    }
    }

activity_main.xml中

    <ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

list.xml

    <TextView
    android:id="@+id/datumMonday"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="60dp"
    android:textStyle="bold"
    android:textSize="10pt"/>

    <TextView
    android:id="@+id/monday"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true" />
    <View style="@style/Divider"/>
    <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/mine"/>
    <RatingBar
    android:id="@+id/ratingMonday"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:numStars="5"
    android:stepSize="1"
    android:rating="0" />

    <Button
    android:text="@string/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:id="@+id/buttonMonday" />
    <View style="@style/Divider"/>   .........

1 个答案:

答案 0 :(得分:0)

要使用简单适配器访问该按钮,您必须在AsyncTask之外声明并初始化适配器,然后添加代码以填充列表中的数据,并在notifyDataSetChanged()方法中调用postExecute()。< / p>

此代码取自here

    SimpleAdapter k=new SimpleAdapter(this,val1,R.layout.mytask,new String[]{"a","c","b"},new int[]{R.id.View1,R.id.View2,R.id.ViewStatus}) {

@Override public View getView (int position, View convertView, ViewGroup parent) { 
View v = super.getView(position, convertView, parent); 
Button b=(Button)v.findViewById(R.id.mytask); 
b.setOnClickListener(new OnClickListener() { 
@Override public void onClick(View arg0) { 
// TODO Auto-generated method stub Toast.makeText(MainActivity.this,"save",Toast.LENGTH_SHORT).show(); 
} 
}); 
return v; 
} 
};