从自定义列表适配器android更新自定义gridview

时间:2014-10-20 15:00:20

标签: android listview gridview android-custom-view

我有一个包含一组自定义视图的列表视图。每个自定义视图中都有一个gridview。

当用户点击自定义视图中的复选框时,我希望能够更新gridview中每个单元格的背景。

这是我的自定义视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp" />

    <CheckBox
        android:id="@+id/lblHeaderCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:layout_centerVertical="true" />
</RelativeLayout>

<ca.package.CustomGridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clickable="false"
    android:gravity="center"
    android:numColumns="7"
    android:padding="10dp"
    android:stretchMode="columnWidth"
    clickable="true" />

这是我的每个网格单元格的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:clickable="true" >

<TextView
    android:id="@+id/grid_itemTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">
</TextView>

</LinearLayout>

以下是我填写列表的方式

expListView = (ListView) this.findViewById(R.id.event_list);

    prepareListData();  

    listAdapter = new CustomAdapter(this, listDataHeader,
            listDataChild, listChildBoolean);

    expListView.setAdapter(listAdapter);

这是我的listView

的自定义适配器中的getView方法
public View getView(int groupPosition, View convertView, ViewGroup parent) {

    final int gp = groupPosition;

    String headerTitle = (String) getGroup(groupPosition);

    // Boolean bool = _listDataBoolean.get(groupPosition);
    Boolean bool = currentCalendarEvents.get(groupPosition).getAlert();

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    String[] data = new String[14];

    List<String> times = _currentListDataChild.get(_currentListDataHeader
            .get(groupPosition));

    data[0] = "Su";
    data[1] = "M";
    data[2] = "Tu";
    data[3] = "W";
    data[4] = "Th";
    data[5] = "F";
    data[6] = "Sa";

    data[7] = times.get(6); // sun
    data[8] = times.get(0); // mon
    data[9] = times.get(1); // tues
    data[10] = times.get(2); // wed
    data[11] = times.get(3); // thurs
    data[12] = times.get(4); // fri
    data[13] = times.get(5); // sat

    ArrayList<Boolean> boolArray = _listChildBoolean.get(_currentListDataHeader
            .get(groupPosition));

    final GridView gridView = (GridView) convertView.findViewById(R.id.gridView);

    final GridAdapter adapter = new GridAdapter(this._context, data, boolArray);

    gridView.setAdapter(adapter);

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    final CheckBox chk2 = (CheckBox) convertView
            .findViewById(R.id.lblHeaderCheckbox);
    chk2.setChecked(bool);

    chk2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (chk2.isChecked()) {
                currentCalendarEvents.get(gp).setAlert(true);
                currentCalendarEvents.get(gp).setDaysTrue();

                // I WANT TO UPDATE THE LIST HERE

                Log.i(LOG_TAG, "box checked");

            } else {
                currentCalendarEvents.get(gp).setAlert(false);
                currentCalendarEvents.get(gp).setDaysFalse();

                // AND HERE

                Log.i(LOG_TAG, "box not checked");
            }
        }
    });


    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i(LOG_TAG, "gridview clicked");
            currentCalendarEvents.get(gp).setBoolDays(adapter.getHashMap());
        }
    });

    return convertView;
}

这是我的GridAdapter

public class GridAdapter extends BaseAdapter {

private Context context;
private String[] items;
private ArrayList<Boolean> boolArray;
private ArrayList<View> views;
LayoutInflater inflater;

public GridAdapter(Context context, String[] items,
        ArrayList<Boolean> boolArray) {
    this.context = context;
    this.items = items;
    this.boolArray = boolArray;
    this.views = new ArrayList<View>();
    inflater = (LayoutInflater) this.context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.grid_cell, null);

        final TextView tv = (TextView) convertView
                .findViewById(R.id.grid_itemTxt);
        tv.setText(items[position]);

        if (position < 7) {
            convertView.setBackgroundColor(Color.DKGRAY);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
            tv.setTextColor(Color.WHITE);
            tv.setTypeface(null, Typeface.BOLD);
            tv.setLines(2);
        } else {

            Log.e("blerp", "test : " + items[position]);

            if (!items[position].equals("")) {
                if (boolArray.get(position % 7)) {
                    convertView.setBackgroundColor(context.getResources()
                            .getColor(R.color.LightGreen));
                } else {
                    convertView.setBackgroundColor(Color.LTGRAY);
                }

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (boolArray.get(position % 7)) {
                            ((View) tv.getParent())
                                    .setBackgroundColor(Color.LTGRAY);
                            boolArray.set(position % 7, false);
                        } else {
                            ((View) tv.getParent())
                                    .setBackgroundColor(context
                                            .getResources().getColor(
                                                    R.color.LightGreen));
                            boolArray.set(position % 7, true);
                        }
                    }
                });

            } else
                convertView.setBackgroundColor(Color.LTGRAY);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tv.setLines(2);
        }

    }

    return convertView;
}

@Override
public int getCount() {
    return items.length;
}

@Override
public Object getItem(int position) {
    return items[position];
}

@Override
public long getItemId(int position) {
    return position;
}

public HashMap<String,Boolean> getHashMap() {
    HashMap<String, Boolean> hm = new HashMap<String,Boolean>();
    hm.put("sun", boolArray.get(0));
    hm.put("mon", boolArray.get(1));
    hm.put("tues", boolArray.get(2));
    hm.put("wed", boolArray.get(3));
    hm.put("thurs", boolArray.get(4));
    hm.put("fri", boolArray.get(5));
    hm.put("sat", boolArray.get(6));
    return hm;
}


}

非常感谢任何帮助,如果需要,我可以发布更多代码。

1 个答案:

答案 0 :(得分:0)

在适配器类中启动gridview和checkbox(使用id查找视图) 然后通过     if(checkBox.isChecked){
gridview.setBackgroundColor(Color.rgb(X,Y,Z)))}