ListView单击显示/隐藏所有复选框

时间:2016-06-18 05:42:21

标签: android listview

我有一个列表视图,它有一个复选框和两个文本字段,我想在listview上点击功能更改复选框可见性属性,我可以从getView功能内部更改属性,但我想从listview中获取它单击。帮我找到解决方案

public class HelpList extends Fragment {

amfFunctions amf;
MyCustomAdapter dataAdapter = null;
Database_Contact contact = new Database_Contact();
DBHelper mydb = new DBHelper(getActivity());
public static final int PICK_CONTACT = 1;
public String user_phone_number;
public String buddyName;
public String buddyNum;
LayoutInflater vi;
View v ;
Fragment fragment = null;
Button myAddButton,myDelButton;
int selected = 0;
Boolean isInternetPresent = false;
ConnectionDetector cd;
ArrayList<Database_Contact> selectedList = new ArrayList<>();
Database_Contact addcontacts = new Database_Contact();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.activity_helplist, container, false);
    // Inflate the layout for this fragment
    displayListView();
    cd = new ConnectionDetector(getActivity());
    myDelButton = (Button)v. findViewById(R.id.deleteContact);
    myDelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isInternetPresent = cd.isConnectingToInternet();
            if (isInternetPresent) {
                DeleteContact();
            }
            else{
                Toast.makeText(getActivity(),     
 getString(R.string.nointernet), Toast.LENGTH_SHORT).show();
            }
        }
    });
    Constants.i = 0;
    myAddButton = (Button)v. findViewById(R.id.Addanother);
    myAddButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isInternetPresent = cd.isConnectingToInternet();
            Log.e("Myaddbutton text ", (String) myAddButton.getText());
            if (isInternetPresent) {
                if (myAddButton.getText().equals("Close")){
                    Toast.makeText(getActivity(), 
 getString(R.string.click_to_close), Toast.LENGTH_SHORT).show();
                }
                else{
                    AddContact();
                }

            }
            else{
                Toast.makeText(getActivity(), 
 getString(R.string.nointernet), Toast.LENGTH_LONG).show();
            }
        }
    });
    return v;
}

private void displayListView() {
    mydb = new DBHelper(getActivity());
    ArrayList<Database_Contact> contactlist = (ArrayList<Database_Contact>) 
mydb.getAllDatabase_Contacts();

    Collections.sort(contactlist, new Comparator<Database_Contact>() {
        @Override
        public int compare(Database_Contact lhs, Database_Contact rhs) {
            return lhs.getName().compareTo(rhs.getName());
        }
    });

    //create an ArrayAdaptar from the String Array
    dataAdapter = new MyCustomAdapter(getActivity(), 
 R.layout.activity_allcontactlist, contactlist);
    ListView listView = (ListView)v.findViewById(R.id.helplistview);
    // Assign adapter to ListView
    listView.setTextFilterEnabled(true);
    listView.setAdapter(dataAdapter);
    dataAdapter.notifyDataSetChanged();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // When clicked, show a toast with the TextView text
            Database_Contact contact = (Database_Contact) 
 parent.getItemAtPosition(position);
            contact.isSelected();
        }
    });
}

private class MyCustomAdapter extends ArrayAdapter<Database_Contact> {

    private ArrayList<Database_Contact> contactlist;
    public MyCustomAdapter(Context context, int textViewResourceId,
                           ArrayList<Database_Contact> contactlist) {
        super(context, textViewResourceId, contactlist);
        this.contactlist = new ArrayList<Database_Contact>();
        this.contactlist.addAll(contactlist);
    }

    private class ViewHolder {
        TextView code;
        TextView Number;
        CheckBox name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            vi = (LayoutInflater) getActivity().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.activity_allcontactlist, 
null);

            holder = new ViewHolder();

            holder.code = (TextView) 
 convertView.findViewById(R.id.helplist_name);
            holder.Number = (TextView) 
 convertView.findViewById(R.id.helplist_num);
            holder.name = (CheckBox) 
 convertView.findViewById(R.id.checkbox_all);
            convertView.setTag(holder);
            final ViewHolder finalHolder = holder;
            final ViewHolder finalHolder1 = holder;

            holder.code.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (finalHolder1.name.isShown() == false){
                        Constants.i = Constants.i+1;
                        myDelButton.setEnabled(true);
                        finalHolder.name.setVisibility(View.VISIBLE);
                    }
                    else if(finalHolder1.name.isShown() == true) {
                        finalHolder.name.setVisibility(View.GONE);
                        Constants.i = Constants.i-1;
                        if (Constants.i == 0){
                            myDelButton.setEnabled(false);
                        }
                    }
                }
            });
            holder.Number.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (finalHolder1.name.isShown() == false){
                        Constants.i = Constants.i+1;
                        myDelButton.setEnabled(true);
                        finalHolder.name.setVisibility(View.VISIBLE);
                    }
                    else if(finalHolder1.name.isShown() == true) {
                        finalHolder.name.setVisibility(View.GONE);
                        Constants.i = Constants.i-1;
                        if (Constants.i == 0){
                            myDelButton.setEnabled(false);
                        }
                    }
                }
            });

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Database_Contact contact = (Database_Contact) 
 cb.getTag();
                    contact.setSelected(cb.isChecked());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Database_Contact contact = contactlist.get(position);
        holder.code.setText(contact.getName());
        holder.Number.setText(contact.getPhoneNumber());
        holder.name.setText("");
        holder.name.setChecked(contact.isSelected());
        holder.name.setTag(contact);
        return convertView;
    }
}
}

2 个答案:

答案 0 :(得分:0)

由于我找不到任何完美的解决方案,我以不同的方式尝试了它,因为它与我想要的有点不同,即点击我使用了我的displayListView()功能并完成了工作

         ischeckboxVisible = true;
         Runnable run = new Runnable() {
         @Override
         public void run() {
             displayListView();
         }
         };
         getActivity().runOnUiThread(run);

来到我使用的getView:

            if (!ischeckboxVisible)
            {
                holder.name.setVisibility(View.GONE);
            }
            if (ischeckboxVisible)
            {
                holder.name.setVisibility(View.VISIBLE);
            }

所以每次我点击它都会将ischeckboxVisible更改为true或false并初始化displatListview()并且它可以正常工作。

我从这里得到了帮助Android hide and show checkboxes in custome listview on button click

希望这对某些人来说可能派上用场。

答案 1 :(得分:0)

请检查以下代码是否有帮助,

generate_results