我想在我的自定义适配器中显示/隐藏复选框,但我找不到任何解决方案进行3天的搜索。
在我的自定义适配器中,当用户单击“显示”按钮然后使其可见时,所有复选框都将隐藏为默认值,同时再次单击将其隐藏。 这是我的代码。
感谢您的帮助
主要活动
public class PhonebookList extends ListActivity{
private ArrayList<PhoneListView> lists;
private ArrayList<String> phoneName = new ArrayList<String>();
private PhoneListAdapter adapter;
private ListView lv;
private RelativeLayout rl;
private ImageView im1;
private ImageView im2;
private boolean checkUncheck = true;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_phonelist); // Get main display layout
lv = (ListView) findViewById(android.R.id.list);
String uniqueName = "";
int index = 0;
lists = new ArrayList<PhoneListView>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext())
{
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if(!name.equals(uniqueName))
{
uniqueName = name;
phoneName.add(name);
lists.add(new PhoneListView(name, index, checkUncheck));
index++;
}
else
{
continue;
}
}
int x = lists.size();
PhoneListView[] toArray = new PhoneListView[x];
toArray = lists.toArray(toArray);
adapter = new PhoneListAdapter(this,
R.layout.custom_phone_listview_rows, toArray); // Layout whose hold rows
lv.setAdapter(adapter);
lv.setClickable(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Intent i = new Intent(getApplicationContext(),
PhoneBookDetail.class);
i.putExtra("id", phoneName.get(position));
startActivity(i);
}
});
}
@Override
protected void onResume()
{
final ImageView showButton = (ImageView) findViewById(R.id.show_hide_button);
showButton.setBackgroundResource(R.drawable.icon_choise);
showButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(checkUncheck)
{
showButton.setBackgroundResource(R.drawable.icon_choise);
checkUncheck = false;
rl.setVisibility(View.GONE);
im1.setVisibility(View.GONE);
im2.setVisibility(View.GONE);
}
else
{
showButton.setBackgroundResource(R.drawable.icon_completion);
checkUncheck = true;
rl.setVisibility(View.VISIBLE);
im1.setVisibility(View.VISIBLE);
im2.setVisibility(View.VISIBLE);
}
}
});
rl = (RelativeLayout) findViewById(R.id.show_hide_bar);
im1 = (ImageView) findViewById(R.id.show_hide_backup);
im2 = (ImageView) findViewById(R.id.show_hide_checkall);
rl.setVisibility(View.GONE);
im1.setVisibility(View.GONE);
im2.setVisibility(View.GONE);
super.onResume();
}
public void backupForChecked(View v){
if(adapter.checkedItem.size() == 0)
Toast.makeText(getApplicationContext(), "Select atleast one item", Toast.LENGTH_SHORT).show();
else
{
// Send selected values as Array
int[] myArray = new int[adapter.checkedItem.size()];
Bundle b=new Bundle();
for(int i = 0; i < adapter.checkedItem.size(); i++)
{
myArray[i] = Integer.parseInt(adapter.checkedItem.get(i));
}
b.putIntArray("contactID", myArray);
Intent i = new Intent(getApplicationContext(), SaveAllWithoutAsk.class);
i.putExtra("contactID", myArray);
startActivity(i);
}
}
public void checkUncheckAll(View v)
{
}
public void modoru(View v)
{
finish();
}
}
自定义ListView
package custom_list;
public class PhoneListView{
private String name;
private int id;
private boolean itemToggled = false;
public PhoneListView()
{
super();
}
public PhoneListView(String name, int id, boolean itemToggled)
{
this.setName(name);
this.setId(id);
this.setItemToggled(itemToggled);
}
public void setName(String name)
{
this.name = name;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public int getId()
{
return id;
}
public boolean getItemToggled()
{
return itemToggled;
}
public void setItemToggled(boolean itemToggled)
{
this.itemToggled = itemToggled;
}
}
我的自定义适配器
public class PhoneListAdapter extends ArrayAdapter<PhoneListView>{
private Context context;
private int resID;
private PhoneListView[] data = null;
public ArrayList<String> checkedItem = new ArrayList<String>();
public PhoneListAdapter(Context context, int resource,
PhoneListView[] objects)
{
super(context, resource, objects);
this.context = context;
this.resID = resource;
this.data = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
CustomHolder holder = null;
if (row == null)
{
LayoutInflater inflate = ((Activity) context).getLayoutInflater();
row = inflate.inflate(resID, null);
holder = new CustomHolder();
holder.cb = (CheckBox) row.findViewById(R.id.phonelist_checkbox);
holder.tw = (TextView) row
.findViewById(R.id.phonelist_contact_name);
row.setTag(holder);
}
else
holder = (CustomHolder) row.getTag();
final PhoneListView plv = data[position];
holder.tw.setText(plv.getName().toString());
holder.id = plv.getId();
holder.checked = plv.getItemToggled();
if (holder.checked == false)
holder.cb.setVisibility(View.GONE);
else
{
holder.cb.setVisibility(View.VISIBLE);
holder.cb.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (((CompoundButton) v).isChecked())
{
checkedItem.add("" + plv.getId());
}
else
{
checkedItem.remove("" + plv.getId());
}
}
});
}
return row;
}
public class CustomHolder
{
CheckBox cb;
TextView tw;
int id;
boolean checked;
}
}
屏幕截图在这里
答案 0 :(得分:0)
您应该通过调用按钮的notifyDatasetChanged()
中的OnClickListener
功能来通知适配器数据集已更改。
如果row为null,您还应该删除检查。通常,重复使用视图是一种好习惯,但由于您的视图发生了更改,并且您需要在更改设置后进行更新,因此需要重新创建项目。
答案 1 :(得分:0)
感谢您的回复。 我已经解决了我的问题#1我将在这里写出解决方案,可以帮助别人的时间和头发。
我创建了新的公共方法,并在点击显示/隐藏按钮时调用它。
现在我仍在研究如何处理检查/取消选中所有内容,我仍需要提示和帮助
答案 2 :(得分:0)
我认为您可以为复选框的数量创建一个循环,然后遍历循环,即
1)您可以使用一个名为checkall()的方法,其中所有元素都将循环并检查其状态,如果选中则将保持检查状态,如果未选中则将被检查。
2)另一个uncheckall()循环方法,取消选中所有复选框。
3)另外,看看你是否可以为你拥有的复选框创建一个复选框组。
最好的问候
下面的代码是来自android示例代码的一些代码的修改版本,看它是否有帮助。
public void onCheckboxClicked(查看视图){
// Here you can create an array of the check boxes available in the view .
// create a loop which will check which one of the check boxes is checked and which one is not
for(...... ....)
{ boolean checked =((CheckBox)view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
// Put some meat on the sandwich
else
// Remove the meat
break;
case R.id.checkbox_cheese:
if (checked)
// Cheese me
else
// I'm lactose intolerant
break;
// TODO: Veggie sandwich
}
}
}
答案 3 :(得分:-1)
您可以将您使用的布局xml文件显示出来吗? 窗口小部件具有分配给它们的属性,根据需要显示/隐藏它们。 检查您的复选框小部件是否有这样的权利。
问候 普拉萨德