samedi 24 janvier 2015

Android getting unchecked checkbox in checklist after search in edittext

I have checklist with search functionality(used edittext) when i use to search its showing filtered items so i can select what i want, its fine but problem is, before checked items are becoming unchecked when search functionality reloads the list. I want selected items before search after search also should be checked in a list, it should not uncheck how to fix this plz help me.....I am new to android I really tried to solve it from 2 days


here is my code



public class Lab extends Fragment {
Button mViewBtn,msaveBtn;
EditText mReviewdateEdt,mInputSearch;
TextView mMostprecrdTxv,mViewmoreTxv;
View android;
// final Context context = getActivity();
MyCustomAdapter dataAdapter = null;
ListView listView;
DatabaseHandler db;
SQLiteDatabase db1;
Pharmacy mPharmacyFrag;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

android = inflater.inflate(R.layout.lab_frag, container, false);

db=new DatabaseHandler(getActivity());


getIds();
displayListView();
viewAlertDialog();
saveAlertdialog();

return android;
}

private void getIds() {
// TODO Auto-generated method stub
mViewBtn = (Button)android.findViewById(R.id.view);
msaveBtn = (Button)android.findViewById(R.id.save);

mInputSearch = (EditText)android.findViewById(R.id.inputSearch);

listView = (ListView)android.findViewById(R.id.list);

}



ArrayList<States> stateList = new ArrayList<States>();
ArrayList<States> text_sort = new ArrayList<States>();



private void displayListView()
{


stateList = new ArrayList<States>();

States _states = new States("17 KETOSTERIODS","",false);
stateList.add(_states);

_states = new States("17-ALPHA HYDROXY PROGESTERON","",false);
stateList.add(_states);
_states = new States("24 HRS URINE ALBUMIN","",false);
stateList.add(_states);
_states = new States("24HRS URINARYCORTISOL/CREATININE RATIO","",false);
stateList.add(_states);
_states = new States("24HRS URINE PROTEIN","",false);
stateList.add(_states);
_states = new States("2D ECHO","",false);
stateList.add(_states);
_states = new States("2D ECHO&DOPPLER STUDY","",false);
stateList.add(_states);
_states = new States("5-HYDROXY INDOLE ACETIC ACID","",false);
stateList.add(_states);
_states = new States("ABSOLUTE EOSINOPHIL COUNT","",false);
stateList.add(_states);
_states = new States("ABSOLUTE NEUTROPHILIC COUNT","",false);
stateList.add(_states);

dataAdapter = new MyCustomAdapter(getActivity().getBaseContext(),R.layout.state_info, stateList);


listView.setAdapter(dataAdapter);



mInputSearch.addTextChangedListener(new TextWatcher()
{

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)
{





int textlength = mInputSearch.getText().length();
text_sort.clear();


for (int i = 0; i < stateList.size(); i++)
{
System.out.println("firstlooop"+mInputSearch.getText().toString());
System.out.println("names"+stateList.get(i).getCode());
if (stateList.get(i).getCode().toLowerCase().contains(mInputSearch.getText().toString())||stateList.get(i).getCode().toUpperCase().contains(mInputSearch.getText().toString()))

{
States _states = new States(stateList.get(i).getCode(),"",false);
text_sort.add(_states);
System.out.println("Gdafdhujgkgj");

}
}

System.out.println("text_sort"+text_sort.size());
dataAdapter = new MyCustomAdapter(getActivity(),R.layout.state_info, text_sort);
listView.setAdapter(dataAdapter);
dataAdapter.notifyDataSetChanged();
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}

});


private class MyCustomAdapter extends ArrayAdapter



{

private LayoutInflater vi;
private ArrayList<States> stateList;


public MyCustomAdapter(Context context, int textViewResourceId,

ArrayList<States> stateList)
{

super(context, textViewResourceId, stateList);
this.stateList = new ArrayList<States>();
this.stateList.addAll(stateList);

vi = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}

private class ViewHolder
{
TextView code;
CheckBox name;
}


@Override



public View getView(int position, View convertView, ViewGroup parent)

{

ViewHolder holder = null;

Log.v("ConvertView", String.valueOf(position));

if (convertView == null)
{
convertView = vi.inflate(R.layout.state_info, null);

holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);

holder.name =(CheckBox)convertView.findViewById(R.id.checkBox1);

convertView.setTag(holder);

holder.name.setOnClickListener( new View.OnClickListener()

{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;

States _state = (States) cb.getTag();

_state.setSelected(cb.isChecked());



}
});

}
else
{
holder = (ViewHolder) convertView.getTag();
}

States state = stateList.get(position);

holder.code.setText( state.getCode());

holder.name.setText(state.getName());

holder.name.setChecked(state.isSelected());

holder.name.setTag(state);

return convertView;
}

}



private void viewAlertDialog()
{


mViewBtn.setOnClickListener(new View.OnClickListener() {



@Override


public void onClick(View arg0) {


AlertDialog.Builder alertDialogBuilder = new


AlertDialog.Builder(getActivity());



StringBuffer responseText = new StringBuffer();

ArrayList<States> stateList = dataAdapter.stateList;

for(int i=0;i<stateList.size();i++)
{
States state = stateList.get(i);

if(state.isSelected())
{
responseText.append("\n\n" + state.getCode());

}
}


final TextView myView = new TextView(getActivity());



myView.setText("Selected Tests are : \n"+responseText);

myView.setTextSize(14);

alertDialogBuilder.setCustomTitle(myView);

alertDialogBuilder.setCancelable(false);


alertDialogBuilder.setNegativeButton("Close",new DialogInterface.OnClickListener()



{
@Override
public void onClick(DialogInterface arg0, int arg1)
{

}
});



AlertDialog alertDialog = alertDialogBuilder.create();

alertDialog.show();

}


});
}



private void saveAlertdialog()


{



// TODO Auto-generated method stub

msaveBtn.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());


alertDialogBuilder.setMessage("Lab Orders Saved");


alertDialogBuilder.setCancelable(false);


alertDialogBuilder.


setNegativeButton("PHARMACY",newDialogInterface.OnClickListener()


{



@Override

public void onClick(DialogInterface arg0, int arg1) {

int newFragmentNumber = 1;

((Tabswipe) getActivity()).setDisplayedFragment(newFragmentNumber);

}
});


AlertDialog alertDialog = alertDialogBuilder.create();


alertDialog.show();


StringBuffer responseText = new StringBuffer();

ArrayList<States> stateList = dataAdapter.stateList;

for(int i=0;i<stateList.size();i++)
{
States state = stateList.get(i);

if(state.isSelected())
{
responseText.append("\n" + state.getCode());

}
}

db1=db.getWritableDatabase();

ContentValues values=new ContentValues();


values.put("LabTests",String.valueOf(responseText));

db1.insert("Labtests", null, values);
Cursor cursor=db1.rawQuery("select * from Labtests", null);
if(cursor!=null)
{
if(cursor.moveToFirst())
{
do

{

String tests=cursor.getString(cursor.getColumnIndex("LabTests"));

}while(cursor.moveToNext());
}
}
}



});

}


}


public class States {



String code = null;
String name = null;
boolean selected = false;

public States(String code, String name, boolean selected)
{
super();
this.code = code;
this.name = name;
this.selected = selected;
}

public String getCode()
{
return code;
}

public void setCode(String code)
{
this.code = code;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public boolean isSelected()
{
return selected;
}

public void setSelected(boolean selected)
{
this.selected = selected;
}

}




Aucun commentaire:

Enregistrer un commentaire