I am a new to android BaseAdapter and I am facing a null pointer exception when get the checked item from popup window. what I want to do given below- I want to select the checked item from checked listview on popup window and show these selected items on that activity screen when the popup window has gone.
here is my code- 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);
}
Here is DropDownListAdapter class extends Base adapter
@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;
}