如何在简单适配器中添加动态复选框的监听器

时间:2015-08-07 07:03:43

标签: android android-layout android-activity android-adapter simpleadapter

我有动态CheckBoxes,现在我想查看是否点击了CheckBox AdditionalInformation.Java文件这里定义简单适配器:

package com.example.ahmad.cvbuilder21;
    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;


    public class AdditionalInformation extends ActionBarActivity {

        CheckBox cb;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_additional_infromation);

            cb=(CheckBox)findViewById(R.id.additionalchk);
            ABC s = ABC.getSingletonObject();
            String[][] str = s.getString();
            ListView lv=((ListView)findViewById(R.id.additionallist));
            String[] additionalOptions={"Add your Picture","Add Experince / Project","Add Reference","Add Skills / Languages"};
            String[] txtViewNames={"options"};

            List<HashMap<String,String>>list=new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < additionalOptions.length; i++) {

                HashMap<String, String> map= new HashMap<String, String>();
                map.put("options",  additionalOptions[i]);
                list.add(map);
            }
                    SimpleAdapter simpleAdpt = new SimpleAdapter(this, list, R.layout.mylayout2,txtViewNames , new int[] {R.id.additionaltxt1});
            lv.setAdapter(simpleAdpt);

        }


        public void additionalData(View view1)
        {

            if(cb.isChecked())
            {
                cb.findViewById(R.id.additionalchk).setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view)
                            {
                                Toast toast = Toast.makeText(getApplicationContext(), "Checked",
                                        Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
                                toast.show();
                            }
                        }
                );
            }
            else{}



        }

    }

AdditionalInformation.xml文件:其中调用additionalData函数 检查哪个CheckBox点击了。     

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Choose Additional Options:"
       android:id="@+id/additionalBtn"
       />

    <ListView
        android:layout_below="@id/additionalBtn"
        android:layout_centerHorizontal="true"
        android:id="@+id/additionallist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
    <Button

        android:layout_below="@id/additionallist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add to CV"
        android:onClick="additionalData"
        />
</RelativeLayout>

mylayout2.xml文件,我在其中定义了简单适配器的控件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/additionaltxt1"

        />
    <CheckBox
        android:layout_alignParentRight="true"
        android:layout_alignRight="@id/additionaltxt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:id="@+id/additionalchk"
        />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您实际需要做的是创建自定义适配器。在该自定义中,您可以在正确的位置收听复选框事件。

以下是一个例子:

public class ListEmployeesAdapter extends BaseAdapter {
public List<Employees> items;
Context c;

public ListEmployeesAdapter(Context c, List<Employees> items) {
    this.c = c;
    this.items = items;
}

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

@Override
public Object getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.employee_item, null);
    }
    TextView name = (TextView) convertView.findViewById(R.id.name);
    name.setText(items.get(position).name);
    name.setOnCLickListener(new OnClickListener(){
    @Override 
     public void onClick(){
        //listen to the click
      }
    });
    return convertView;
}
}

只需将布局替换为布局并调用复选框而不是TextView,您就可以了。