State of check box in android listview - android

I want to get all the family id selected from the list view. I add the family id in a separate
arraylist when click on the corresponding checkbox. The value in the arraylist losses when i scroll the listview.
Following is my code.
public class FamilyList extends Activity{
ListView fam_list;
DataAdapter myadapt;
ArrayList<ListClass> familyarraylist;
ArrayList<String> familyselectedlist;
Button show;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("zacharia", "inside oncreate");
setContentView(R.layout.activity_familylist);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
fam_list=(ListView) findViewById(R.id.familylist);
myadapt=new DataAdapter(this);
final TextView empty=(TextView) findViewById(R.id.emptyView);
empty.setText("No Data Found");
familyarraylist= myadapt.getfamilylist("All");
FamilyListAdapter familyadapter=new FamilyListAdapter(this, R.layout.family_list_view,familyarraylist);
fam_list.setAdapter(familyadapter);
fam_list.setEmptyView(empty);
familyselectedlist=new ArrayList<String>();
ImageButton button=(ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText edit=(EditText) findViewById(R.id.editText1);
if(edit.getText().length()>0){
if(myadapt==null){
myadapt=new DataAdapter(FamilyList.this);
}
familyarraylist=myadapt.getfamilylist(edit.getText().toString());
fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view, familyarraylist)); }
else{
if(myadapt==null){
myadapt=new DataAdapter(FamilyList.this);
}
familyarraylist=myadapt.getfamilylist("All");
fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view,familyarraylist ));
}
fam_list.setEmptyView(empty);
}
});
show=(Button) findViewById(R.id.button1);
show.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
for(int i=0;i<familyselectedlist.size();i++){
Log.i("zacharia", familyselectedlist.get(i));
}
MainActivity.selected_list=true;
MainActivity.selected_family_id_array=familyselectedlist;
FamilyList.this.finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.activity_list, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.item1){
final SharedPreferences prefs=getSharedPreferences("search_mode", Activity.MODE_WORLD_READABLE);
int pos=prefs.getInt("position", 0);
new AlertDialog.Builder(this).setTitle("Search By")
.setSingleChoiceItems(new String[]{"Name","House Name"}, pos, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Editor edit=prefs.edit();
edit.putInt("position", which);
edit.commit();
dialog.dismiss();
}
}).show();
}
return true;
}
static class ViewHolder {
protected TextView id_text,id_name,id_address;
protected CheckBox id_check;
}
class FamilyListAdapter extends ArrayAdapter<ListClass>{
private ArrayList<ListClass> list_array;
Context context;
boolean checkchange=false;
ListClass obj;
public FamilyListAdapter(Context context, int textViewResourceId,
ArrayList<ListClass> objects) {
super(context, textViewResourceId, objects);
this.context=context;
list_array=objects;
}
#Override
public ListClass getItem(int position) {
// TODO Auto-generated method stub
return list_array.get(position);
}
public int getFamilyId(int position){
return list_array.get(position).getFamilyid();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.family_list_view, null);
final ViewHolder holder = new ViewHolder();
obj=getItem(position);
holder.id_text=(TextView) v.findViewById(R.id.list_familyid);
holder.id_text.setText(""+obj.getFamilyid());
holder.id_name=(TextView) v.findViewById(R.id.list_name);
holder.id_name.setText(""+obj.getHeadname());
holder.id_address=(TextView) v.findViewById(R.id.list_address);
holder.id_address.setText(""+obj.getAddress());
holder.id_check=(CheckBox) v.findViewById(R.id.check_familyview);
Log.i("zacharia", "check result:"+ getFamilyId(position)+" "+familyselectedlist.contains(""+getFamilyId(position)));
holder.id_check.setSelected(false);
if(familyselectedlist.contains(""+getFamilyId(position))){
holder.id_check.setSelected(true);
}
holder.id_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("zacharia", "inside on change");
if(isChecked){
familyselectedlist.add(""+getFamilyId(position));
}
else{
familyselectedlist.remove(""+getFamilyId(position));
}
}
});
v.setTag(holder);
holder.id_check.setTag(getFamilyId(position));
}
else{
v=convertView;
((ViewHolder)v.getTag()).id_check.setTag(getFamilyId(position));
}
obj=getItem(position);
ViewHolder holder = (ViewHolder) v.getTag();
holder.id_text.setText(""+obj.getFamilyid());
holder.id_name.setText(obj.getHeadname());
holder.id_address.setText(obj.getAddress());
if(familyselectedlist.contains(""+getFamilyId(position))){
holder.id_check.setChecked(true);
}
else{
holder.id_check.setChecked(false);
}
return v;
}
}
}

Just move the following code out of if-condition (if (v==null)):
holder.id_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("zacharia", "inside on change");
if(isChecked){
familyselectedlist.add(""+getFamilyId(position));
}
else{
familyselectedlist.remove(""+getFamilyId(position));
}
}
});
You set listener only if you don't get a view for reusing. But you still have to set listener when you reuse existing view. So, if convertView is not null then it comes already with old onClickListener. This OnClickListener uses wrong position (from old view).
As a result you should set OnClickListener not only if (v == null).

I make some changes to my code and pass an instance of an arraylist (for selected_ids) to the adapter it works.
Following is my code:
public class FamilyList extends Activity{
ListView fam_list;
DataAdapter myadapt;
ArrayList<ListClass> familyarraylist;
ArrayList<String> familyselectedlist;
Button show;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("zacharia", "inside oncreate");
setContentView(R.layout.activity_familylist);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
fam_list=(ListView) findViewById(R.id.familylist);
myadapt=new DataAdapter(this);
final TextView empty=(TextView) findViewById(R.id.emptyView);
empty.setText("No Data Found");
familyarraylist= myadapt.getfamilylist("All");
familyselectedlist=new ArrayList<String>();
FamilyListAdapter familyadapter=new FamilyListAdapter(this, R.layout.family_list_view,familyarraylist,familyselectedlist);
fam_list.setAdapter(familyadapter);
fam_list.setEmptyView(empty);
ImageButton button=(ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText edit=(EditText) findViewById(R.id.editText1);
if(edit.getText().length()>0){
if(myadapt==null){
myadapt=new DataAdapter(FamilyList.this);
}
familyarraylist=myadapt.getfamilylist(edit.getText().toString());
fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view, familyarraylist,familyselectedlist)); }
else{
if(myadapt==null){
myadapt=new DataAdapter(FamilyList.this);
}
familyarraylist=myadapt.getfamilylist("All");
fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view,familyarraylist,familyselectedlist));
}
fam_list.setEmptyView(empty);
}
});
show=(Button) findViewById(R.id.button1);
show.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
for(int i=0;i<familyselectedlist.size();i++){
Log.i("zacharia", familyselectedlist.get(i));
}
MainActivity.selected_list=true;
MainActivity.selected_family_id_array=familyselectedlist;
FamilyList.this.finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.activity_list, menu);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.item1){
final SharedPreferences prefs=getSharedPreferences("search_mode", 0);
int pos=prefs.getInt("position", 0);
new AlertDialog.Builder(this).setTitle("Search By")
.setSingleChoiceItems(new String[]{"Name","House Name"}, pos, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Editor edit=prefs.edit();
edit.putInt("position", which);
edit.commit();
dialog.dismiss();
}
}).show();
}
return true;
}
class FamilyListAdapter extends ArrayAdapter<ListClass>{
private ArrayList<ListClass> list_array;
Context context;
ArrayList<String> selectedlist;
TextView id_text,id_name,id_address;
CheckBox id_check;
boolean checkchange=false;
ListClass obj;
public FamilyListAdapter(Context context, int textViewResourceId,
ArrayList<ListClass> objects,ArrayList<String> selectedlist) {
super(context, textViewResourceId, objects);
this.context=context;
list_array=objects;
this.selectedlist=selectedlist;
}
#Override
public ListClass getItem(int position) {
// TODO Auto-generated method stub
return list_array.get(position);
}
public int getFamilyId(int position){
return list_array.get(position).getFamilyid();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.family_list_view, null);
}
obj=getItem(position);
id_text=(TextView) v.findViewById(R.id.list_familyid);
id_text.setText(""+obj.getFamilyid());
id_name=(TextView) v.findViewById(R.id.list_name);
id_name.setText(""+obj.getHeadname());
id_address=(TextView) v.findViewById(R.id.list_address);
id_address.setText(""+obj.getAddress());
id_check=(CheckBox) v.findViewById(R.id.check_familyview);
id_check.setSelected(false);
if(selectedlist.contains(""+getFamilyId(position))){
id_check.setSelected(true);
}
id_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("zacharia", "inside on change");
if(isChecked){
selectedlist.add(""+getFamilyId(position));
}
else{
selectedlist.remove(""+getFamilyId(position));
}
}
});
id_check.setTag(getFamilyId(position));
if(selectedlist.contains(""+getFamilyId(position))){
id_check.setChecked(true);
}
else{
id_check.setChecked(false);
}
return v;
}
}
}
Now i need to show the selected items when i open this activity. How can i done that? i try to pass it by the familyselectedlist to the adapter. If i uncheck the checkbox and scroll the check box status is remain as checked.

Related

Android searchView not searching the list until fully scrolled

I have a listview with a custom adapter, and Im trying to use SearchView with a CustomFilter. But the search is not "fully" working.
When I search for something that is on the viewable area of the listview, it is able to search, and all nonviewable area is not being included in the search.
Here is a video on whats going on:
https://youtu.be/2Z9FZMlNmGw
main
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_board_game_list, container, false);
this.listView = (ListView) view.findViewById(R.id.listView);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this.getContext());
databaseAccess.open();
List<String> boardgamesNames = databaseAccess.getNames();
List<String> urls = databaseAccess.getUrls();
adapter = new bgAdapter(getContext(), R.layout.row_layout);
adapterOriginal = new bgAdapter(getContext(), R.layout.row_layout);
databaseAccess.close();
listView.setAdapter(adapter);
int i = 0;
for(String name: boardgamesNames) {
boardgameListRow data = new boardgameListRow(urls.get(i), boardgamesNames.get(i));
i++;
adapter.add(data);
adapterOriginal.add(data);
}
listView.setDivider(null);
listView.setDividerHeight(0);
searchView = (SearchView)view.findViewById(R.id.searchId);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
adapter.getFilter().filter(newText);
}
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
BoardGameListFragment fragment= new BoardGameListFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
//adapter = adapterOriginal;
return true;
}
});
// Inflate the layout for this fragment
return view;
}
}
Here is the Adapter:
https://github.com/Shank09/AndroidTemp/blob/master/bgAdapter.java
I think you need to call notifyDataSetChanged() in onQueryTextChange
I fixed it, I was using the wrong variable in bgAdapter. Please remove this question if possible.
public class Listbyoperator extends Activity {
ListView lstdetail;
Activity act;
EditText search;
ArrayList<DetailModel> detail=new ArrayList<DetailModel>();
DetailaAdapter dadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listbyoperator);
act=this;
lstdetail=(ListView) findViewById(R.id.Listbyoperator_detaillist);
search=(EditText) findViewById(R.id.editsearch);
search.setPadding(10, 0, 0, 0);
search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = search.getText().toString().toLowerCase(Locale.getDefault());
dadapter.filter(text);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
DetailModel d=new DetailModel("1","HARDIP","saahi");
detail.add(d);
DetailModel d1=new DetailModel("2","jalpa","sadfsadf");
detail.add(d1);
dadapter=new DetailaAdapter(act, detail);
lstdetail.setAdapter(dadapter);
dadapter.notifyDataSetChanged();
lstdetail.setEnabled(true);
}
}
/*Detail Model*/
public class DetailModel
{
public String d_id,d_name,d_decription;
public String getD_id() {
return d_id;
}
public void setD_id(String d_id) {
this.d_id = d_id;
}
public String getD_name() {
return d_name;
}
public void setD_name(String d_name) {
this.d_name = d_name;
}
public String getD_decription() {
return d_decription;
}
public void setD_decription(String d_decription) {
this.d_decription = d_decription;
}
public DetailModel(String s1,String s2,String s3)
{
this.d_id=s1;
this.d_name=s2;
this.d_decription=s3;
}
}
/*detail adapter */
public class DetailaAdapter extends BaseAdapter{
Context mContext;
private List<DetailModel> data=null;
private ArrayList<DetailModel> arraylist;
private static LayoutInflater inflater=null;
private static String String=null,valid;
public boolean flag=true;
public DetailaAdapter(Context context,List<DetailModel> data)
{
mContext = context;
this.data = data;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<DetailModel>();
this.arraylist.addAll(data);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.list_detail, null);
final TextView t1,t2,t3,t4,t5;
t1=(TextView)vi.findViewById(R.id.list_detail_text1);
t2=(TextView)vi.findViewById(R.id.list_detail_textview2);
t3=(TextView)vi.findViewById(R.id.list_detail_text2);
DetailModel da =new DetailModel(String, String,String);
da=data.get(position);
final String a1,a2,a3;
a1=da.d_id;
a2=da.d_name;
a3=da.d_decription;
t2.setText(a3);//description
t3.setText(a2);//name
return vi;
}
public void filter(String charText)
{
charText = charText.toLowerCase(Locale.getDefault());
data.clear();
if (charText.length() == 0) {
data.addAll(arraylist);
}
else
{
for (DetailModel wp : arraylist)
{
if (wp.getD_decription().toLowerCase(Locale.getDefault()).contains(charText) || wp.getD_name().toLowerCase(Locale.getDefault()).contains(charText))
{
data.add(wp);
}
}
}
notifyDataSetChanged();
}
}

custom listview with edittext,checkbox and textview

I have a custom listview that contains an edittext,checkbox and two text views.Now i am hving a problem with the edittext values in my code.I am not able to get the correct value from the edittext.And also if i set the value of an edit text at one position in the list,that value is also set to edittexts at random positions in the listview.And i have the common problem of the values changing on scroll.Following is the code:
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assess_list);
prev = getIntent();
final ListView listview = (ListView) findViewById(R.id.studentsListView);
listview.setAdapter(new AssessmentAdapter(this, R.layout.assessment,
StudentNames.student_name, StudentNames.studentRollNo));
}
Adapter:
public class AssessmentAdapter extends ArrayAdapter<String> {
Context context;
ViewHolder holder;
CheckBox present;
EditText marks;
int pos;
public static ArrayList<String> studentNames,studentRollNo,studentsPresent,marksObtainedList;
public AssessmentAdapter(Context context, int textViewResourceId,ArrayList<String> studentNames,ArrayList<String> studentRollNo) {
super(context, textViewResourceId,studentNames);
// TODO Auto-generated constructor stub
this.context=context;
AssessmentAdapter.studentNames=studentNames;
AssessmentAdapter.studentRollNo=studentRollNo;
studentsPresent=new ArrayList<String>();
marksObtainedList=new ArrayList<String>();
Log.d("No. of students",""+studentRollNo.size());
for(int i=0;i<studentNames.size();i++)
{
studentsPresent.add("1");
marksObtainedList.add("0");
}
}
static class ViewHolder {
CheckBox presentCB;
TextView name,Rno;
EditText marksObtained;
Button save;
}
public View getView(final int pos,View convertview,ViewGroup parent)
{
try
{
holder=null;
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertview==null)
{
//rowView=inflater.inflate(org.example.attendance.R.layout.list_radio,parent,false);
convertview=inflater.inflate(R.layout.assessment,null);
holder=new ViewHolder();
holder.presentCB=(CheckBox)convertview.findViewById(org.example.attendance2.R.id.presentCB);
holder.name=(TextView)convertview.findViewById(org.example.attendance2.R.id.studNameTV);
holder.Rno=(TextView)convertview.findViewById(org.example.attendance2.R.id.studRollNoTV);
holder.marksObtained=(EditText)convertview.findViewById(org.example.attendance2.R.id.marksET);
holder.save=(Button)convertview.findViewById(org.example.attendance2.R.id.saveButton);
convertview.setTag(holder);
}
else
{
holder=(ViewHolder) convertview.getTag();
}
holder.name.setText(studentNames.get(pos));
holder.Rno.setText(studentRollNo.get(pos));
holder.save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
holder.marksObtained.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
marksObtainedList.set(pos, arg0.toString());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
holder.presentCB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(studentsPresent.get(pos).equals("1"))
{
studentsPresent.set(pos, "0");
}
else
{
studentsPresent.set(pos, "1");
}
}
});
//if(RadioChecked[pos])
if(studentsPresent.get(pos).equals("1"))
{
holder.presentCB.setChecked(true);
}
// else if(!RadioChecked[pos])
else if(studentsPresent.get(pos).equals("0"))
{
holder.presentCB.setChecked(false);
}
if(holder.marksObtained.getText().toString().equals(""))
{
marksObtainedList.set(pos,"0");
}
else
{
String marks=holder.marksObtained.getText().toString();
marksObtainedList.set(pos,marks);
Log.d(""+pos,marksObtainedList.get(pos));
}
holder.marksObtained.setText(marksObtainedList.get(pos));
}catch(Exception e)
{
}
return convertview;
}
public boolean areAllItemsEnabled()
{
return true;
}
#Override
public boolean isEnabled(int arg0)
{
return true;
}
}
the problem is all about relate to focus . see here , it may help you.
Focusable EditText inside ListView

How to set radiobutton unchecked in adapter class

I have a customdialog box ,in this i have a listview and a spinner item.The listview contains textview and a radio button in each row.I can select only one row at a time with the help of radio buttons.This is happening.
Problem is coming when i select spinner items and then i select radio button ,the previous radio button is still selected.Now what i want when i select spinner items ,the previously selected radiobuttons should be unchecked.
Note:my spinner is in Main class and radio button is in CustomAdapterClass which i am calling in my Main class
Here is my code: Adapter Class:
public class APTRequestCustomAdapter extends BaseAdapter{
Context context;
ArrayList<APTRequestCustomdetails> APTRequestCustomitems;
private static String display_aptstatus1="";
public static String adapterbookingid="";
private boolean userSelected = false;
public static RadioButton mCurrentlyCheckedRB;
String patientid1="";
public static String from2="";
public static String to2="";
public static String testing2="";
public static String cancelbookid="";
public static String radiostring="";
public static String troubleradiobutton="";
private int selectedItemIndex=-1;
public APTRequestCustomAdapter(Context context,
ArrayList<APTRequestCustomdetails> aPTRequestCustomitems) {
super();
this.context = context;
this.APTRequestCustomitems = aPTRequestCustomitems;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return APTRequestCustomitems.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return APTRequestCustomitems.get(arg0);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v=convertView;
final int pos=position;
if(v==null){
v = LayoutInflater.from(context).inflate(R.layout.backupcustomdailoguniversalappointment,null);
}
TextView patientname=(TextView)v.findViewById(R.id.txtpatientname);
TextView tag=(TextView)v.findViewById(R.id.txttype);
TextView phone=(TextView)v.findViewById(R.id.txtphone);
TextView age=(TextView)v.findViewById(R.id.txtage);
TextView Apptstatus=(TextView) v.findViewById(R.id.txtapptstatus);
TextView bookid=(TextView) v.findViewById(R.id.txtbookingid);
ImageView remove=(ImageView)v.findViewById(R.id.txtremove);
RadioButton radio=(RadioButton)v.findViewById(R.id.txtRadiobutton);
patientname.setPaintFlags(patientname.getPaintFlags() |Paint.UNDERLINE_TEXT_FLAG);
patientname.setText(APTRequestCustomitems.get(position).getPatient_Name());
tag.setText(APTRequestCustomitems.get(position).getTag());
phone.setText(APTRequestCustomitems.get(position).getPhone());
age.setText(APTRequestCustomitems.get(position).getAge());
Apptstatus.setText(APTRequestCustomitems.get(position).getAppointmentstatus());
bookid.setText(APTRequestCustomitems.get(position).getBookingId());
Apptstatus.setVisibility(View.INVISIBLE);
bookid.setVisibility(View.INVISIBLE);
//APT_CustomRequestResponse.radiostring="";
// adapterbookingid=APTRequestCustomitems.get(pos).getBookingId().toString();// wrong code
patientname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Hello buddy");
Intent io=new Intent(context, PatientSummaryActivity.class);
DoctorGlobal.patid=APTRequestCustomitems.get(pos).getPatient_ID();
DoctorGlobal.patname=APTRequestCustomitems.get(pos).getPatient_Name();
System.out.println("PATID"+DoctorGlobal.patid);
context.startActivity(io);
}
});
if (position==getCount()-1 && userSelected==false) {
// radio.setChecked(true);
mCurrentlyCheckedRB = radio;
} else {
radio.setChecked(false);
}
if(APTRequestCustomitems.get(position).getAppointmentstatus().equals("1")){
remove.setVisibility(View.INVISIBLE);
radio.setVisibility(View.INVISIBLE);
v.setBackgroundColor(Color.parseColor("#1569C7"));
}else
{
v.setBackgroundColor(Color.parseColor("#FFFFFF"));
remove.setVisibility(View.VISIBLE);
radio.setVisibility(View.VISIBLE);
radio.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
APTRequestCustomAdapter.radiostring="checkedtrue";
if (mCurrentlyCheckedRB !=null) {
if (mCurrentlyCheckedRB ==null)
mCurrentlyCheckedRB = (RadioButton) v;
mCurrentlyCheckedRB.setChecked(true);
// Toast.makeText(context, ""+pos, Toast.LENGTH_LONG).show();
adapterbookingid=APTRequestCustomitems.get(pos).getBookingId().toString();
}
if (mCurrentlyCheckedRB == v)
return;
mCurrentlyCheckedRB.setChecked(false);
((RadioButton) v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton) v;
}
});
}
return v;
}
}
And this is the code where i am calling the adapter class:
This method is in Async Class on PostExecute()method
public void showCustomDialog() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(context);
List<String> list=new ArrayList<String>();
list.add("Normal");
list.add("Low");
list.add("High");
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.customdialoguniversalappointment);
ListView listcustomuniversalappt=(ListView) dialog.findViewById(R.id.listcustomuniversalappt);
LinearLayout layoutsubject=(LinearLayout) dialog.findViewById(R.id.layoutsubject);
LinearLayout layoutappt=(LinearLayout) dialog.findViewById(R.id.layoutappt);
spinnerappt=(Spinner)dialog.findViewById(R.id.permissionspinner);
ImageView cancel=(ImageView)dialog.findViewById(R.id.imgcancel);
Button cancelappt=(Button)dialog.findViewById(R.id.btncancelappt);
Button confirmappt=(Button)dialog.findViewById(R.id.btnconfirmappt);
EditText subject=(EditText) dialog.findViewById(R.id.edtsubject);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerappt.setAdapter(dataAdapter);
spinnerappt.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index = arg0.getSelectedItemPosition();
selected_item=arg0.getItemAtPosition(arg2).toString();
APTRequestCustomAdapter.mCurrentlyCheckedRB.setChecked(false);//this line of code is not executing,the radio button remains checked in the view
if(APTRequestCustomAdapter.mCurrentlyCheckedRB.isChecked()){// this lines of codes are not executing even though the radio button is checked ,i don't know why
APTRequestCustomAdapter.mCurrentlyCheckedRB.setChecked(false);// And this line is also not executing
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
APTRequestCustomAdapter adap=new APTRequestCustomAdapter(context,run_custom_universal_apptdata());
listcustomuniversalappt.setAdapter(adap);
/*//runapptstatus_method();
listcustomuniversalappt.setChoiceMode(1);
/
dialog.show();
}
Try to use your Custom Adapter like,
public class MyRadioAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<Variation> mVariations;
private int mSelectedVariation;
public MyRadioAdapter(Context context, ArrayList<Variation> variations, int selectedVariation)
{
mContext = context;
mVariations = variations;
mSelectedVariation = selectedVariation;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view==null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_radio_adapter_item, null);
}
final Variation variation = mVariations.get(position);
TextView name = (TextView) view.findViewById(R.id.name);
RadioButton radio = (RadioButton) view.findViewById(R.id.radio);
name.setText(variation.getName());
if(position==mSelectedVariation) radio.setChecked(true);
else radio.setChecked(false);
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
mSelectedVariation = position;
MyRadioAdapter.this.notifyDataSetChanged();
}
});
return view;
}
here Radio buttons are usually grouped by Radio Group.When one RadioButton within a group is selected, all others are automatically deselected.

Select all and clear all selection checkbox

I'm implementing functions of notifications like default messages application in android. Now I'm doing the multiple message deletion by adding checkbox in which I'm using one common checkbox to select all messages in the list. But I can not check the listview checkbox which is in getview of CustomAdpter.
class customListAdpter extends BaseAdapter {
private Context ctx;
CheckBox checkBox;
TextView sender, message;
customListAdpter(Context context) {
this.ctx = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return new NotifiCation().senderlist.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int pos, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.checkboxlist, null);
checkBox = (CheckBox) v.findViewById(R.id.btn_chck);
sender = (TextView) v.findViewById(R.id.text_senderno);
message = (TextView) v.findViewById(R.id.text_msg);
}
sender.setText("" + new NotifiCation().senderlist.toArray()[pos]);
message.setText("" + new NotifiCation().msglist.toArray()[pos]);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
}
}
});
return v;
}
}
And this is my main activity. Here I have the main checkbox.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.delete_notifications);
db = new DB(NotificationsDelete.this);
notifications = (ListView) findViewById(R.id.list_with_ckbox);
selectAll = (CheckBox) findViewById(R.id.btn_checkall);
done = (Button) findViewById(R.id.done_notification_delete);
notifications.setAdapter(new customListAdpter(con));
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton chkbox, boolean arg1) {
if (chkbox.isChecked() == true) {
for (int i = 0; i < new NotifiCation().senderlist.size(); i++) {
//notifications.setItemChecked(i, true);
customListAdpter adpter=new customListAdpter(con);
adpter.checkBox.setChecked(true);
}
} else {
// new customListAdpter(con).checkBox.setChecked(true);
}
}
});
done.setOnClickListener(this);
}
Try calling notifyDataSetChanged() after you handle onCheckedChanged() of selectAll checkbox field.

Listvew with textview and checkbox

I want to have a listview with textview + check box. I was able to create the listview. I can capture listview item select. However when I try to capture check box select, unselect I get a null pointer exception. How to write the setOnCheckedChangeListener() for the check box..
public class LocationActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.locationmain);
ListView listview = (ListView) findViewById(R.id.listView1);
String selectQuery = "SELECT * FROM " + DatabaseHandler.TABLE_LOCATIONLABLES;
SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
final List<String> locLables = new ArrayList<String>();
if(cursor != null){
if (cursor.moveToFirst()) {
do {
locLables.add(cursor.getString(1));
} while (cursor.moveToNext());
}
}
//String[] locLables = new String[] {"Home","University","Office"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.locationmain_entry,R.id.textView12, locLables);
//cb gives a null pointer exception
CheckBox cb = (CheckBox) listview.findViewById(R.id.checkBox12);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("selected");
}else if(!isChecked){
System.out.println("not selected");
}
}
});
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();
}
});
}
You can maintain seperate adpater class so that you can achive this easily....
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
public int getCount()
{
// TODO Auto-generated method stub
return listview_arr.length;
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return listview_arr[position];
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return listview_array.length;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.search_list_item, null);
TextView tv=(TextView)row.findViewById(R.id.title);
CheckBox cb=(CheckBox)row.findViewById(R.id.cb01);
tv.setText(listview_arr[position]);
cb.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(cb.ischecked)
{
//ur code
}
else //ur code
}
});
return row;
}
}
Do not use listview.findViewById(), just use findViewById() like you did for your list.
Unless the checkbox is part of each of the list items, in which case you would have to access the checkbox from within the getView() of your ListAdapter.

Categories

Resources