Set RadioButton Checked in custom adapter - android

I have radio button in list-view which is working fine with my custom adapter.
The problem is I want to set radio button checked if answer already exists in my model.
Following is adapter class:
public class options_data_adapter extends ArrayAdapter<ChildListDataModel> {
private Activity context;
private List<ChildListDataModel> data;
private int resId;
MySharedPreferences mpref;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;
boolean[] itemChecked;
public options_data_adapter(Context context, int resource, List<ChildListDataModel> objects) {
super(context, resource, objects);
this.context = (Activity) context;
this.resId = resource;
this.data = objects;
mpref = new MySharedPreferences(context);
itemChecked = new boolean[objects.size()];
}
#Override
public View getView(final int position, View v, ViewGroup parent) {
final Holder h;// = null;
if (v == null) {
h = new Holder();
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(resId, parent, false);
h.check = (CheckBox) v.findViewById(R.id.c1);
h.radio=(RadioButton)v.findViewById(R.id.r1);
h.spinner=(Spinner)v.findViewById(R.id.s1);
h.btn=(Button)v.findViewById(R.id.btn_spiner);
v.setTag(h);
} else {
h = (Holder) v.getTag();
}
Log.d("soh_answer", data.get(position).answere);
if(mpref.GetquestionType().equals("Radio")){
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.VISIBLE);
h.spinner.setVisibility(View.GONE);
h.btn.setVisibility(View.GONE);
h.radio.setText(data.get(position).answere);
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
h.radio.setTag(position);
h.radio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit # position : " + pos);
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
((VasNBIActivity) context).removeChecked(data.get(mSelectedPosition).id);
mSelectedRB.setBackgroundResource(R.drawable.button_ackgroun1);
/*data.get(mSelectedPosition).checked="0";*/
}
mSelectedPosition = position;
((VasNBIActivity) context).addChecked(data.get(mSelectedPosition).id);
h.radio.setBackgroundResource(R.drawable.button_background);
/* data.get(mSelectedPosition).checked="1";*/
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
}else if(mpref.GetquestionType().equals("Checkbox")){
h.check.setVisibility(View.VISIBLE);
h.radio.setVisibility(View.GONE);
h.check.setText(data.get(position).answere);
h.btn.setVisibility(View.GONE);
//=============================================================================
if(data.get(position).checked.equals("1")) {
itemChecked[position] = true;
}else{
itemChecked[position] = false;
}
if (itemChecked[position]) {
h.check.setChecked(true);
((VasNBIActivity) context).addChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_background);
}else {
h.check.setChecked(false);
}
h.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (h.check.isChecked()&& data.get(position).checked.equals("0")) {
itemChecked[position] = true;
((VasNBIActivity) context).addChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_background);
data.get(position).checked = "1";
}
else {
itemChecked[position] = false;
((VasNBIActivity) context).removeChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_ackgroun1);
data.get(position).checked="0";
}
}
});
//for spinner type questions
}else {
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.GONE);
h.spinner.setVisibility(View.VISIBLE);
h.btn.setVisibility(View.VISIBLE);
h.btn.setText(data.get(position).answere);
h.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
data.get(position).spinner_value = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
return v;
}
private class Holder {
CheckBox check;
RadioButton radio;
Spinner spinner;
Button btn;
}
}
And here is the code for radio button:
if(mpref.GetquestionType().equals("Radio")){
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.VISIBLE);
h.spinner.setVisibility(View.GONE);
h.btn.setVisibility(View.GONE);
h.radio.setText(data.get(position).answere);
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
h.radio.setTag(position);
h.radio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit # position : " + pos);
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
((VasNBIActivity) context).removeChecked(data.get(mSelectedPosition).id);
mSelectedRB.setBackgroundResource(R.drawable.button_ackgroun1);
/*data.get(mSelectedPosition).checked="0";*/
}
mSelectedPosition = position;
((VasNBIActivity) context).addChecked(data.get(mSelectedPosition).id);
h.radio.setBackgroundResource(R.drawable.button_background);
/* data.get(mSelectedPosition).checked="1";*/
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
I am trying to check button if its checked value=1 in list model.
addChecked and remove checked functions:
public void addChecked(String val) {
checkedValue.add(val);
}
public void removeChecked(String val) {
checkedValue.remove(val);
}
How can I achieve this??

You check button if its checked value=1 in this code.
// #1
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
But, you change the state again this code
// #2
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
So, remove your code(#2) and try again

Related

How to disable previous items in a ListView

I am trying to develop an activity where there is a custom listView made out of CustomAdapter.
The list consists of a TextView and an EditText. The EditText when clicked, it auto fetches the system time.
What I want is when a particular EditText is filled, I want all the previous(above) list items in the sequence to be disabled.
So far, I have tried using isEnabled() and areAllItemsEnabled() functions returning respective boolean values using position, but however didn’t work.
Please help me achieve the above.
Thanks.
This is my CustomAdapter Class
public class SelectStnListByRoute extends BaseAdapter implements View.OnClickListener {
Context context;
ArrayList<StnNames> stnList;
LayoutInflater layoutInflater = null;
ViewHolder viewHolder;
private int mLastClicked;
public SelectStnListByRoute(Context context, ArrayList<StnNames> stnList) {
super();
this.context = context;
this.stnList = stnList;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return stnList.size();
}
#Override
public Object getItem(int position) {
return stnList.get(position);
}
#Override
public long getItemId(int position) {
return stnList.indexOf(getItem(position));
}
public int getViewTypeCount() {
return 1;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
if(position==position){
return false;
}
return false;
}
#Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
int type = getItemViewType(position);
StnNames stnDetails = stnList.get(position);
viewHolder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.footplate_custome_layout, null);
viewHolder.txtStnNAme = (TextView) convertView.findViewById(R.id.txtStnCode);
viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
viewHolder.txtStnDep = (TextView) convertView.findViewById(R.id.txtDepTime);
convertView.setTag(viewHolder);
viewHolder.txtStnArr.setTag(stnDetails);
viewHolder.txtStnDep.setTag(stnDetails);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.txtStnArr.setTag(stnDetails);
viewHolder.txtStnDep.setTag(stnDetails);
}
viewHolder.txtStnNAme.setText(stnDetails.getStnCode());
viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("Position: " , String.valueOf(position)); //Here I am getting the position of the row item clicked, where should I put the Onclick false for disabling all of the above fields using the position?
}
});
viewHolder.txtStnDep.setOnClickListener(this);
viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
if (stnDetails.getArrivalTime() != null) {
viewHolder.txtStnArr.setText(stnDetails.getArrivalTime());
} else {
viewHolder.txtStnArr.setText("");
}
if (stnDetails.getDeptTime() != null) {
viewHolder.txtStnDep.setText(stnDetails.getDeptTime());
} else {
viewHolder.txtStnDep.setText("");
}
return convertView;
}
class ViewHolder {
TextView txtStnNAme, txtStnArr, txtStnDep;
int ref;
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case txtArrivalTime:
TextView textViewArrVal = (TextView) view.findViewById(R.id.txtArrivalTime);
textViewArrVal.setClickable(false);
StnNames listItemsArrr = (StnNames) textViewArrVal.getTag();
if (listItemsArrr.getArrivalTime() != getCurrentTime()) {
listItemsArrr.setArrivalTime(getCurrentTime());
if (listItemsArrr.getArrivalTime() != null) {
int position = textViewArrVal.getSelectionStart();
textViewArrVal.setText(listItemsArrr.getArrivalTime());
} else {
textViewArrVal.setText("");
}
}
break;
case txtDepTime:
TextView textViewDepVal = (TextView) view.findViewById(R.id.txtDepTime);
StnNames listItemsDepp = (StnNames) textViewDepVal.getTag();
if (listItemsDepp.getDeptTime() != getCurrentTime()) {
listItemsDepp.setDeptTime(getCurrentTime());
if (listItemsDepp.getDeptTime() != null) {
textViewDepVal.setText(listItemsDepp.getDeptTime());
} else {
textViewDepVal.setText("");
}
}
break;
default:
break;
}
}
public String getCurrentTime(){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String arrDate = mdformat.format(calendar.getTime());
return arrDate;
}
}
You can do this as below mentioned -:
You need to store the position of clicked button was. So initialize a variable in your class
int mButtonSelected = -1;
EDIT 1.
Then make a change to your isEnabled method
#Override
public boolean isEnabled(int position) {
if(position<mButtonSelected){
return false;
}
return true;
}
That will work it if any other button was clicked. but you have to do that in your onClick
mButtonSelected = position;
notifyDataSetChanged();
Let me it worked or not
EDIT
see below changes in your code-:
public class SelectStnListByRoute extends BaseAdapter {
Context context;
ArrayList<StnNames> stnList;
LayoutInflater layoutInflater = null;
ViewHolder viewHolder;
private int mLastClicked;
private SQLiteDB sqLiteDB;
int mArrivalSelected = -1;
int mDepartSelected = -1;
public SelectStnListByRoute(Context context, ArrayList<StnNames> stnList) {
super();
this.context = context;
this.stnList = stnList;
layoutInflater = LayoutInflater.from(context);
sqLiteDB = new SQLiteDB(context);
}
#Override
public int getCount() {
return stnList.size();
}
#Override
public Object getItem(int position) {
return stnList.get(position);
}
#Override
public long getItemId(int position) {
return stnList.indexOf(getItem(position));
}
public int getViewTypeCount() {
return 1;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
if (position <= mArrivalSelected) {
return false;
}
return true;
}
public boolean isEnabledd(int position) {
if (position <= mDepartSelected) {
return false;
}
return true;
}
#Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
int type = getItemViewType(position);
StnNames stnDetails = stnList.get(position);
viewHolder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.footplate_custome_layout, null);
viewHolder.txtStnNAme = (TextView) convertView.findViewById(R.id.txtStnCode);
viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
viewHolder.txtStnDep = (TextView) convertView.findViewById(R.id.txtDepTime);
convertView.setTag(viewHolder);
viewHolder.txtStnArr.setTag(stnDetails);
viewHolder.txtStnDep.setTag(stnDetails);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.txtStnArr.setTag(stnDetails);
viewHolder.txtStnDep.setTag(stnDetails);
}
viewHolder.txtStnNAme.setText(stnDetails.getStnCode());
if (!isEnabled(position)) {
if (position <= mArrivalSelected) {
viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#ffa500"));
viewHolder.txtStnArr.setEnabled(false);
if (position < mArrivalSelected) {
viewHolder.txtStnDep.setEnabled(false);
viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#ffa500"));
}
}
} else {
viewHolder.txtStnArr.setEnabled(true);
viewHolder.txtStnDep.setEnabled(true);
viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#b4b4b4"));
viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#b4b4b4"));
}
viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("Position: ", String.valueOf(position));
mArrivalSelected = position;
arrivalClick(view);
notifyDataSetChanged();
}
});
if (!isEnabledd(position)) {
if (position <= mDepartSelected) {
viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#ffa500"));
viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#ffa500"));
viewHolder.txtStnArr.setEnabled(false);
viewHolder.txtStnDep.setEnabled(false);
} else {
viewHolder.txtStnArr.setEnabled(true);
viewHolder.txtStnDep.setEnabled(true);
viewHolder.txtStnArr.setBackgroundColor(Color.parseColor("#b4b4b4"));
viewHolder.txtStnDep.setBackgroundColor(Color.parseColor("#b4b4b4"));
}
}
viewHolder.txtStnDep.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("Position: ", String.valueOf(position));
mDepartSelected = position;
departureClick(view);
notifyDataSetChanged();
}
});
viewHolder.txtStnArr = (TextView) convertView.findViewById(R.id.txtArrivalTime);
if (stnDetails.getArrivalTime() != null) {
viewHolder.txtStnArr.setText(stnDetails.getArrivalTime());
} else {
viewHolder.txtStnArr.setText("");
}
if (stnDetails.getDeptTime() != null) {
viewHolder.txtStnDep.setText(stnDetails.getDeptTime());
} else {
viewHolder.txtStnDep.setText("");
}
return convertView;
}
class ViewHolder {
TextView txtStnNAme, txtStnArr, txtStnDep;
StnNames pos;
int ref;
}
public void arrivalClick(View view) {
TextView textViewArrVal = (TextView) view.findViewById(R.id.txtArrivalTime);
StnNames listItemsArrr = (StnNames) textViewArrVal.getTag();
if (listItemsArrr.getArrivalTime() != getCurrentTime()) {
listItemsArrr.setArrivalTime(getCurrentTime());
int stnId = listItemsArrr.getStnId();
String arrClick = "arrival";
String upSideKm = listItemsArrr.getStnUpsideKm();
String downsideKm = listItemsArrr.getStnDownSideKm();
String arrTime = getCurrentTime();
/* sqLiteDB.open();
*//* long abc = sqLiteDB.insertJourneySchedule(stnId,arrTime,"",userId,journeyId,latitute,longitute,journyDate,arrClick);*//*
*//* long abcd = sqLiteDB.updateJourneySchedule(stnId,arrTime,"",userId,journeyId,latitute,longitute,journyDate,arrClick,downsideKm,upSideKm);
Log.e("arrclick",String.valueOf(abcd));*//*
sqLiteDB.close();*/
if (listItemsArrr.getArrivalTime() != null) {
int position = textViewArrVal.getSelectionStart();
textViewArrVal.setText(listItemsArrr.getArrivalTime());
} else {
textViewArrVal.setText("");
}
}
}
public void departureClick(View view) {
TextView textViewDepVal = (TextView) view.findViewById(R.id.txtDepTime);
StnNames listItemsDepp = (StnNames) textViewDepVal.getTag();
if (listItemsDepp.getDeptTime() != getCurrentTime()) {
listItemsDepp.setDeptTime(getCurrentTime());
String depTime = getCurrentTime();
int stnId = listItemsDepp.getStnId();
String depClick = "departure";
String upSideKm = listItemsDepp.getStnUpsideKm();
String downsideKm = listItemsDepp.getStnDownSideKm();
sqLiteDB.open();
/*long abc = sqLiteDB.insertJourneySchedule(stnId,"",depTime,userId,journeyId,latitute,longitute,journyDate,depClick);*/
/*long abcd = sqLiteDB.updateJourneySchedule(stnId,"",depTime,userId,journeyId,latitute,longitute,journyDate,depClick,downsideKm,upSideKm);
Log.e("depclick",String.valueOf(abcd));*/
sqLiteDB.close();
if (listItemsDepp.getDeptTime() != null) {
textViewDepVal.setText(listItemsDepp.getDeptTime());
} else {
textViewDepVal.setText("");
}
}
}
public String getCurrentTime() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String arrDate = mdformat.format(calendar.getTime());
return arrDate;
}
}
Get the position of the row which is clicked and then set onclick false for positions less than clicked position
as follows:
viewHolder.txtStnArr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("Position: " , String.valueOf(position));
for (int i = 0; i < position; i++) {
viewHolder.txtStnArr.setEnable(false);
}
notifyDataSetChanged();
}
});

Refresh ListView pressing option DialogFragment

I have a listview that clicking on an element shows a DialogFragmen with several options, options that take the user will be shown in the item of listview TextView and I get the following error in execution:
Activity app.gepv.Inventario has leaked IntentReceiver com.immersion.android.haptics.HapticFeedbackManager$HapticFeedbackBroadcastReceiver#426e94b8 that was originally registered here. Are you missing a call to unregisterReceiver()?
Enter the code that I think is important, if you need anything else edit the question :)
This is mi DialogFragment:
final String[] items= equiDisp.toArray(new String[equiDisp.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Asigne equipo/equipos:")
.setOnKeyListener(new Dialog.OnKeyListener(){
public boolean onKey(DialogInterface arg0, int keyCode,KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
//dialog.dismiss();
actualizarDisplay();
}
return true;
}
})
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item, boolean isChecked) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
if(isChecked)
{
marcado.add(items[item]);
Log.i("Dialogos", "Marcado: " + items[item]);
obras.get(pulsado).equiA.add(Integer.parseInt(items[item]));
for( int k=0; k< obras.get(pulsado).equiA.size(); k++)
{
Log.i("Dialogos", "Equipos: " + obras.get(pulsado).equiA.get(k) );
}
}
}
});
So far everything is running well because I check with Log.i
This is the function ActualizarDisplay():
public void actualizarDisplay()
{
adapter = new ObrasAdapter(this, obras);
lvObras = (ListView) findViewById(R.id.lvItems);
lvObras.setAdapter(adapter);
lvObras.setOnItemClickListener(this);
}
And this is my custom dataApdapter for the listview:
public class ObrasAdapter extends ArrayAdapter<Obra> {
private Context context;
private ArrayList<Obra> datos;
public ObrasAdapter(Context context, ArrayList<Obra> datos) {
super(context, R.layout.listview_item, datos);
this.context = context;
this.datos = datos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ObrasHolder holder;
if (item == null) {
item = LayoutInflater.from(context).inflate(R.layout.listview_item,
null);
holder = new ObrasHolder();
holder.foto = (ImageView) item.findViewById(R.id.imgAnimal);
holder.num = (TextView) item.findViewById(R.id.numC);
holder.iden = (TextView) item.findViewById(R.id.idenC);
holder.ubi = (TextView) item.findViewById(R.id.ubiC);
holder.hombres = (TextView) item.findViewById(R.id.homC);
holder.material = (TextView) item.findViewById(R.id.matC);
holder.eq1 = (TextView) item.findViewById(R.id.eq1);
holder.eq2 = (TextView) item.findViewById(R.id.eq2);
holder.eq3 = (TextView) item.findViewById(R.id.eq3);
holder.eq4 = (TextView) item.findViewById(R.id.eq4);
holder.fondo = (RelativeLayout) item.findViewById(R.id.fondobra);
item.setTag(holder);
}
holder = (ObrasHolder) item.getTag();
holder.foto.setImageResource(datos.get(position).getDrawableImageID());
if(datos.get(position).getPrioridad()==1)
{
holder.num.setTextColor(Color.RED);
holder.iden.setTextColor(Color.RED);
holder.ubi.setTextColor(Color.RED);
holder.hombres.setTextColor(Color.RED);
holder.material.setTextColor(Color.RED);
holder.eq1.setTextColor(Color.RED);
holder.eq2.setTextColor(Color.RED);
holder.eq3.setTextColor(Color.RED);
holder.eq4.setTextColor(Color.RED);
}
if(datos.get(position).getPrioridad()==2)
{
holder.num.setTextColor(Color.parseColor("#FF8000"));
holder.iden.setTextColor(Color.parseColor("#FF8000"));
holder.ubi.setTextColor(Color.parseColor("#FF8000"));
holder.hombres.setTextColor(Color.parseColor("#FF8000"));
holder.material.setTextColor(Color.parseColor("#FF8000"));
holder.eq1.setTextColor(Color.parseColor("#FF8000"));
holder.eq2.setTextColor(Color.parseColor("#FF8000"));
holder.eq3.setTextColor(Color.parseColor("#FF8000"));
holder.eq4.setTextColor(Color.parseColor("#FF8000"));
}
if(datos.get(position).getPrioridad()==3)
{
holder.num.setTextColor(Color.GREEN);
holder.iden.setTextColor(Color.GREEN);
holder.ubi.setTextColor(Color.GREEN);
holder.hombres.setTextColor(Color.GREEN);
holder.material.setTextColor(Color.GREEN);
holder.eq1.setTextColor(Color.GREEN);
holder.eq2.setTextColor(Color.GREEN);
holder.eq3.setTextColor(Color.GREEN);
holder.eq4.setTextColor(Color.GREEN);
}
holder.num.setText(datos.get(position).getNum());
holder.iden.setText(datos.get(position).getIden());
holder.ubi.setText(datos.get(position).getUb());
holder.hombres.setText(datos.get(position).getHom());
holder.material.setText(datos.get(position).getMat());
if(datos.get(position).getEstado()==1)
{
holder.fondo.setBackgroundColor(Color.GREEN);
holder.num.setTextColor(Color.WHITE);
holder.iden.setTextColor(Color.WHITE);
holder.ubi.setTextColor(Color.WHITE);
holder.hombres.setTextColor(Color.WHITE);
holder.material.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
}
if(! datos.get(position).equiA.isEmpty())
{
for(int i=0; i<datos.get(position).equiA.size();i++)
{
if(i == 0)
{
holder.eq1.setText(String.valueOf(datos.get(position).equiA.get(i)));
}
if(i == 1)
holder.eq2.setText(String.valueOf(datos.get(position).equiA.get(i)));
if(i == 2)
holder.eq3.setText(String.valueOf(datos.get(position).equiA.get(i)));
if(i == 3)
holder.eq4.setText(String.valueOf(datos.get(position).equiA.get(i)));
}
}
else
{
holder.eq1.setVisibility(View.INVISIBLE);
holder.eq2.setVisibility(View.INVISIBLE);
holder.eq3.setVisibility(View.INVISIBLE);
holder.eq4.setVisibility(View.INVISIBLE);
}
return item;
}
}
Can anyone help me?
I think i must to do something as:
#Override
protected void onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}

Editext not taking user typed value

In my app there is a Edit text and below there is a list with check box.When user selects a particular checkbox,that value is being displayed in the edittext.Now if user writes some values in the edittext and then selects the checkbox,then the value which user had typed is overridden by the selected value.I want both the values to be displayed in the edittext
Code
public class EmailListAdapter extends BaseAdapter {
private Context context;
private ArrayList<EmailModel> data;
DbHandler dbHandler;
int[] emails;
static ArrayList<String> emailSeperated;
private String strEmails, strTemp;
public EmailListAdapter(Context context, ArrayList<EmailModel> data) {
this.context = context;
this.data = data;
emails = new int[data.size()];
emailSeperated = new ArrayList<String>();
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
dbHandler = new DbHandler(context);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.email_custom_list, viewGroup, false);
holder.tvContact = (TextView) view.findViewById(R.id.tv_email_name);
holder.checkBox = (CheckBox) view.findViewById(R.id.cb_email_checkbox);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// if (emails[i] == 0) {
// holder.checkBox.setChecked(false);
// } else {
// holder.checkBox.setChecked(true);
// }
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton == holder.checkBox) {
if (b) {
emails[i] = 1;
//dbHandler.updateContactList(data.get(i).getUserID(), 1);
//
} else {
emails[i] = 0;
}
}
}
}
);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (emails[i] == 1) {
emailSeperated.add(data.get(i).getEmail());
Log.e("Email values", emailSeperated.toString());
Log.e("Position", "" + i);
} else if (emails[i] == 0) {
emailSeperated.remove(data.get(i).getEmail());
Log.e("Email values", emailSeperated.toString());
Log.e("Position", "" + i);
}
if (!TextUtils.isEmpty(ShareWithinpocketDocs.etEmailLists.getText().toString())) {
ShareWithinpocketDocs.etEmailLists.setText(ShareWithinpocketDocs.etEmailLists.getText().toString() + "," + emailSeperated.toString().subSequence(1, emailSeperated.toString().length() - 1));
} else {
strTemp = emailSeperated.toString().substring(1, emailSeperated.toString().length() - 1);
Log.e("Email Seperated values", strTemp);
ShareWithinpocketDocs.etEmailLists.setText(strTemp);
}
}
});
if (emails[i] == 0) {
holder.checkBox.setChecked(false);
// emailSeperated.remove(data.get(i).getEmail());
// Log.e("Email values", emailSeperated.toString());
// ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
} else {
holder.checkBox.setChecked(true);
// emailSeperated.add(data.get(i).getEmail());
// Log.e("Email values", emailSeperated.toString());
}
holder.tvContact.setText(data.get(i).getEmail());
return view;
}
private class ViewHolder {
TextView tvContact;
CheckBox checkBox;
}
}
This will display what is currently in the EditText and the new text separated by a comma. Change
holder.tvContact.setText(data.get(i).getEmail());
to
String s = holder.tvContact.gettext().toString();
holder.setText(s + ", " + data.get(i).getEmail());

Single Adapter for multiple ListView Android

I want to display Two Listview in my Projects.I am passing using a single adapter to set the contents of two listviews.If I pass array of same length for both listviews to adapter,then it works fine,But If I pass Array of Different Length for Different listview in the adapter,My Application Force closes with the Error:-
>java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
>09-24 11:55:10.359: E/AndroidRuntime(4822): at com.dropdownlistdemo.DropDownListAdapter.getView(DropDownListAdapter.java:98)
>09-24 11:55:10.359: E/AndroidRuntime(4822): at android.widget.AbsListView.obtainView(AbsListView.java:2189)
>09-24 11:55:10.359: E/AndroidRuntime(4822): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
09-24 11:55:10.359: E/AndroidRuntime(4822): at android.widget.ListView.onMeasure(ListView.java:1155)
09-24 11:55:10.359: E/AndroidRuntime(4822): at android.view.View.measure(View.java:12775)
09-24 11:55:10.359: E/AndroidRuntime(4822): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
My code for the adapter is:-
public class DropDownListAdapter extends BaseAdapter {
private ArrayList<String> mListItems;
private LayoutInflater mInflater;
private static int selectedCount = 0;
private static String firstSelected = "";
private ViewHolder holder;
private static String selected = ""; //shortened selected values representation
String car_type;
public static String getSelected() {
return selected;
}
public void setSelected(String selected) {
DropDownListAdapter.selected = selected;
}
public DropDownListAdapter(Context context, ArrayList<String> items) {
mListItems = new ArrayList<String>();
mListItems.addAll(items);
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mListItems.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(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.car_type)
{
if(DropDownListDemo.checkSelected_cartype[position])
holder.chkbox.setChecked(true);
else
holder.chkbox.setChecked(false);
}
else
{
if(DropDownListDemo.checkSelected_transmissontype[position])
holder.chkbox.setChecked(true);
else
holder.chkbox.setChecked(false);
}
return convertView;
}
/*
* Function which updates the selected values display and information(checkSelected[])
*/
private void setText(int position1){
if(DropDownListDemo.car_type)
{
if (!DropDownListDemo.checkSelected_cartype[position1]) {
DropDownListDemo.checkSelected_cartype[position1] = true;
selectedCount++;
} else {
DropDownListDemo.checkSelected_cartype[position1] = false;
selectedCount--;
}
if (selectedCount == 0) {
//mSelectedItems.setText(R.string.select_string);
} else if (selectedCount == 1) {
for (int i = 0; i <DropDownListDemo.checkSelected_cartype.length; i++) {
if (DropDownListDemo.checkSelected_cartype[i] == true) {
firstSelected = mListItems.get(i);
break;
}
}
//mSelectedItems.setText(firstSelected);
setSelected(firstSelected);
} else if (selectedCount > 1) {
for (int i = 0; i < DropDownListDemo.checkSelected_cartype.length; i++) {
if (DropDownListDemo.checkSelected_cartype[i] == true) {
firstSelected = mListItems.get(i);
break;
}
}
}
}
else
{
if (!DropDownListDemo.checkSelected_transmissontype[position1]) {
DropDownListDemo.checkSelected_transmissontype[position1] = true;
selectedCount++;
} else {
DropDownListDemo.checkSelected_transmissontype[position1] = false;
selectedCount--;
}
if (selectedCount == 0) {
//mSelectedItems.setText(R.string.select_string);
} else if (selectedCount == 1) {
for (int i = 0; i <DropDownListDemo.checkSelected_transmissontype.length; i++) {
if (DropDownListDemo.checkSelected_transmissontype[i] == true) {
firstSelected = mListItems.get(i);
break;
}
}
//mSelectedItems.setText(firstSelected);
setSelected(firstSelected);
} else if (selectedCount > 1) {
for (int i = 0; i < DropDownListDemo.checkSelected_transmissontype.length; i++) {
if (DropDownListDemo.checkSelected_transmissontype[i] == true) {
firstSelected = mListItems.get(i);
break;
}
}
}
//mSelectedItems.setText(firstSelected + " & "+ (selectedCount - 1) + " more");
setSelected(firstSelected + " & "+ (selectedCount - 1) + " more");
}
}
void getselected_checkboxes()
{
if(car_type != null && !car_type.equals(""))
{
car_type="";
}
for (int i = 0; i < DropDownListDemo.checkSelected_cartype.length; i++) {
if (DropDownListDemo.checkSelected_cartype[i] == true){
Log.w("checked items"," "+mListItems.get(i));
if(car_type != null && !car_type.equals(""))
{
car_type=car_type+","+mListItems.get(i);
}
else
{
car_type=mListItems.get(i);
}
}
}
car_type = car_type.replaceAll(" ", "%20");
Log.w("car_type",""+car_type);
}
private class ViewHolder {
TextView tv;
CheckBox chkbox;
}
}
And I setting the adapter in Listview as:-
ArrayList<String> items_transmission = new ArrayList<String>();
items_transmission.add("Automatic");
items_transmission.add("Manual");
adapter1 = new DropDownListAdapter(DropDownListDemo.this, items_transmission);
transmisson_type.setAdapter(adapter1);
ArrayList<String> items = new ArrayList<String>();
items.add("Cars");
items.add("Passenger Van");
items.add("SUV");
adapter = new DropDownListAdapter(DropDownListDemo.this, items);
list.setAdapter(adapter);
Please help me...?
I think the problem is with this part.
if(DropDownListDemo.car_type)
{
if(DropDownListDemo.checkSelected_cartype[position])
holder.chkbox.setChecked(true);
else
holder.chkbox.setChecked(false);
}
else
{
if(DropDownListDemo.checkSelected_transmissontype[position])
holder.chkbox.setChecked(true);
else
holder.chkbox.setChecked(false);
}
When you call that code for 2 listviews in a row, the value of DropDownListDemo.car_type probably doesn't change so it goes into the same part (second) for both listview. Is that right?
Try to return mListItems.get(arg0) in getItem(arg0) method
public DropDownListAdapter(Context context, ArrayList<String> items) {
mListItems = items;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mListItems.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mListItems.get(arg0);
}

checkbox auto checked item after remove item

after remove item selected check box auto check next item.
i try to override getcount method but no result
CountryAdapter.java
CountryAdapter extends ArrayAdapter<MyCountry>{
Context context; int layoutResourceId; ArrayList<MyCountry> countries; ContextualActionMode activity;
public CountryAdapter(Context context, int layoutResourceId,
ArrayList<MyCountry> countries) {
}
#Override
public int getCount() {
return countries.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyCountry country = countries.get(position);
ViewHolder viewHolder = null;
if(convertView == null)
{
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, null);
viewHolder.nameEn = (TextView) convertView.findViewById(R.id.tvNameEn);
viewHolder.nameVi = (TextView) convertView.findViewById(R.id.tvNameVi);
viewHolder.flag = (ImageView) convertView.findViewById(R.id.ivFlag);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.nameEn.setText(countries.get(position).getNameEn());
viewHolder.nameVi.setText(countries.get(position).getNameVi());
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
count = 0;
for (MyCountry country : countries) {
if(country.isCheck()) count++;
}
if(isChecked)
{
if(activity.actionMode == null || count == 0)
activity.actionMode = activity.startActionMode(activity.callback);
count++;
country.setCheck(true);
}
else
{
country.setCheck(false);
count--;
if(count == 0) activity.actionMode.finish();
}
}
});
return convertView;
}
int count = 0;
public class ViewHolder{
TextView nameEn;
TextView nameVi;
ImageView flag;
CheckBox check;
}
ContextualActionMode.java
public class ContextualActionMode extends Activity {
ArrayList<MyCountry> countries = new ArrayList<MyCountry>();
ListView listView;
CountryAdapter adapter;
ActionMode.Callback callback = new ActionMode.Callback() {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.mnDelete:
for (int i = 0; i < countries.size(); i++)
{
if (countries.get(i).isCheck()) {
countries.remove(countries.get(i));
countries.get(i).setChecked(false)
}
}
adapter.notifyDataSetChanged();
mode.finish();
return true;
default:
break;
}
return false;
}
};
maybe error here, i find some solutions, but nothing work
i try to change the loop, because the list start index from 0
// i can fix it, thank a lot to Armaan Stranger
link to my source for who has the same problem with me
mediafire.com/?agnvic06c69cvw0
and edit in CountryAdapter.java
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setChecked(false); --> right here, i forgot to add set check false as default.
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Try this:
Just add this line before your onCheckChanged() event like this.
viewHolder.nameEn.setText(countries.get(position).getNameEn());
viewHolder.nameVi.setText(countries.get(position).getNameVi());
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setChecked(false);
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
count = 0;
for (MyCountry country : countries) {
if(country.isCheck())
count++;
}
if(isChecked)
{
if(activity.actionMode == null || count == 0)//chua co
activity.actionMode = activity.startActionMode(activity.callback);
count++;
country.setCheck(true);
}
else
{
country.setCheck(false);
count--;
if(count == 0)
activity.actionMode.finish();
}
}
});
Hope it Helps!!
You have to just set your listview before notifyDataSetChanged();
public void dellistview() {
listviewdata.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
SparseBooleanArray selected = listAdapterData.getCheckedItemPositions();
if (selected != null) {
try {
for (int i = (selected.size() - 1); i >= 0; i--)
{
if (selected.valueAt(i)) {
String str[] = arrList.get(selected.keyAt(i));
fmdbAccess.removelistitm(sourceTable, str[1]);
if (arrList != null)
arrList.remove(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
selected.clear();
arrList = fmdbAccess.getGenricTable(sourceTable, colName);
if (arrList != null && arrList.size() > 0)
setListAdapter();
listAdapterData.notifyDataSetChanged();
// finish();
}
}
Try to use android:checked="false" to CheckBox in your xml file please

Categories

Resources