我是Android BaseAdapter的新手,当从弹出窗口中获取选中的项目时,我面临一个空指针异常。 我想要做的事情如下 - 我想在弹出窗口中选中已检查列表视图中的选中项目,并在弹出窗口消失后在该活动屏幕上显示这些选定项目。
这是我的代码 - Dropdownlist.java
private void initialize(){
//data source for drop-down list
// final ArrayList<String> itemlist = new ArrayList<String>();
checkSelected = new boolean[itemlist.size()];
//initialize all values of list to 'unselected' initially
for (int i = 0; i < checkSelected.length; i++) {
checkSelected[i] = false;
}
enter code here
final TextView tv = (TextView) findViewById(R.DropDownList.SelectBox);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int len = list.getCount();
SparseBooleanArray checked = list.getCheckedItemPositions();
str=Integer.toString(checked.size());
for (int i = 0; i < len; i++) {
str=Integer.toString(len);
// Item position in adapter
if (checked.get(i)) {
item += itemlist.get(i);
//item += adapter.getItem(i).toString();
selectedItems.add(item);
/*do whatever you want with the checked item */
}
//int position = checked.keyAt(i);
selectedItems.add(adapter.getItem(position).toString());
}
outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),
ResultActivity.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
}
});
//onClickListener to initiate the dropDown list
Button createButton = (Button)findViewById(R.DropDownList.create);
createButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
initiatePopUp(itemlist,tv);
}
});
}
private void initiatePopUp(ArrayList<String> itemlist, TextView tv){
LayoutInflater inflater = (LayoutInflater)DropDownListDemo.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//get the pop-up window i.e. drop-down layout
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.pop_up_window, (ViewGroup)findViewById(R.id.PopUpView));
//get the view to which drop-down layout is to be anchored
RelativeLayout layout1 = (RelativeLayout)findViewById(R.id.relativeLayout1);
pw = new PopupWindow(layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
//Pop-up window background cannot be null if we want the pop-up to listen touch events outside its window
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setTouchable(true);
//let pop-up be informed about touch events outside its window. This should be done before setting the content of pop-up
pw.setOutsideTouchable(true);
pw.setHeight(LayoutParams.WRAP_CONTENT);
//dismiss the pop-up i.e. drop-down when touched anywhere outside the pop-up
pw.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
pw.dismiss();
return true;
}
return false;
}
});
//provide the source layout for drop-down
pw.setContentView(layout);
//anchor the drop-down to bottom-left corner of 'layout1'
pw.showAsDropDown(layout1);
//populate the drop-down list
list = (ListView) layout.findViewById(R.DropDownList.dropDownList);
adapter = new DropDownListAdapter(this, itemlist, tv);
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
这是DropDownListAdapter类扩展Base适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.drop_down_list_row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.DropDownList.SelectOption);
holder.chkbox = (CheckBox) convertView.findViewById(R.DropDownList.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(mListItems.get(position));
final int position1 = position;
//whenever the checkbox is clicked the selected values textview is updated with new selected values
holder.chkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setText(position1);
}
});
if(DropDownListDemo.checkSelected[position])
holder.chkbox.setChecked(true);
else
holder.chkbox.setChecked(false);
return convertView;
}