我有自定义列表视图和自定义适配器。布局如下:
现在,我有一个默认隐藏的复选框(在customlistlayout中定义)。我希望它在用户按下删除按钮时显示。
我尝试在按钮onclicklistener中设置setVisibility(View.VISIBLE);
,但应用程序崩溃了。
我怎样才能做到这一点。感谢您的帮助
Log Cat`12-23 22:27:54.236: E/AndroidRuntime(18681): FATAL EXCEPTION: main
12-23 22:27:54.236: E/AndroidRuntime(18681): Process: com.testing.callreminder, PID: 18681
12-23 22:27:54.236: E/AndroidRuntime(18681): java.lang.NullPointerException
12-23 22:27:54.236: E/AndroidRuntime(18681): at com.testing.callreminder.MainActivity.onClick(MainActivity.java:130)
12-23 22:27:54.236: E/AndroidRuntime(18681): at android.view.View.performClick(View.java:4633)
12-23 22:27:54.236: E/AndroidRuntime(18681): at android.view.View$PerformClick.run(View.java:19330)
12-23 22:27:54.236: E/AndroidRuntime(18681): at android.os.Handler.handleCallback(Handler.java:733)
12-23 22:27:54.236: E/AndroidRuntime(18681): at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 22:27:54.236: E/AndroidRuntime(18681): at android.app.ActivityThread.main(ActivityThread.java:5356)
12-23 22:27:54.236: E/AndroidRuntime(18681): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 22:27:54.236: E/AndroidRuntime(18681): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 22:27:54.236: E/AndroidRuntime(18681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-23 22:27:54.236: E/AndroidRuntime(18681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
布局的OnClickListener实现:
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btn_add :
Intent next = new Intent("com.testing.callreminder.ADDITEM");
startActivity(next);
break;
case R.id.btn_delete:
chk.setVisibility(View.VISIBLE);
break;
default:
chk.setVisibility(View.GONE);
break;
}
}
CustomAdapter
public class MenuListAdapter extends BaseAdapter {
boolean i;
Context c;
ArrayList<MenuListItems> menuList;
public MenuListAdapter(Context context, ArrayList<MenuListItems> list){
c=context;
menuList=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return menuList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void setCheckBoxVisibility(boolean visible) {
i = visible;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MenuListItems menuListItems = menuList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customlist, parent, false);
}
if(i){
CheckBox ck = (CheckBox) convertView.findViewById(R.id.delBox);
ck.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
TextView id = (TextView) convertView.findViewById(R.id.tvhide);
id.setText(menuListItems.getSlno());
TextView title = (TextView) convertView.findViewById(R.id.tvtitle);
title.setText(menuListItems.getTitle());
TextView phone = (TextView) convertView.findViewById(R.id.tvpnumber);
phone.setText(menuListItems.getPhone());
return convertView;
}
}
答案 0 :(得分:0)
在适配器上创建一个方法,例如&#34; setCheckBoxVisibility(boolean visible);&#34;在此方法中,您将布尔值保存到私有字段中并调用notifyDataSetChanged();在getView方法中,您将检查此字段并设置可见性。
public class MenuListAdapter extends BaseAdapter {
boolean i;
boolean showCheckbox;
Context c;
ArrayList<MenuListItems> menuList;
public MenuListAdapter(Context context, ArrayList<MenuListItems> list){
c=context;
menuList=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return menuList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void setCheckBoxVisibility(boolean visible) {
i = visible;
notifyDataSetChanged(); //notify dataset changed here
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MenuListItems menuListItems = menuList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customlist, parent, false);
}
if(i){
CheckBox ck = (CheckBox) convertView.findViewById(R.id.delBox);
if(i)
ck.setVisibility(View.VISIBLE);
else
ck.setVisibility(View.GONE);
}
TextView id = (TextView) convertView.findViewById(R.id.tvhide);
id.setText(menuListItems.getSlno());
TextView title = (TextView) convertView.findViewById(R.id.tvtitle);
title.setText(menuListItems.getTitle());
TextView phone = (TextView) convertView.findViewById(R.id.tvpnumber);
phone.setText(menuListItems.getPhone());
return convertView;
}
}