I'm going to try and explain what I want to achieve. So I currently have a customlistviewadapter, each row can have a different view. The user will then click on any row to change the value of that specific row. When the user clicks on a row, then an alert dialog pops up where the content of that row can be changed, when "Ok" is pressed the new value is saved.
Here is the code:
// Inflate correct layout & populate variables
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
List_Entry varStrings = getItem(position);
Button write_btn;
LayoutInflater layoutInflater = LayoutInflater.from(context);
switch (getItemViewType(position)) {
case HEADING_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_heading_lay, null, false);
}
if (varStrings != null) {
TextView name = (TextView) view.findViewById(R.id.head_name);
if (name != null) {
name.setText(varStrings.getHEAD_NAME());
}
}
break;
case STANDARD_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_std_lay, null, false);
}
if (varStrings != null) {
TextView name = (TextView) view.findViewById(R.id.tv_std_name);
TextView value = (TextView) view.findViewById(R.id.tv_std_val);
TextView unit = (TextView) view.findViewById(R.id.tv_std_unit);
if (name != null) {
name.setText(varStrings.getSTD_NAME());
}
if (value != null) {
value.setText(varStrings.getSTD_VALUE());
}
if (unit != null) {
unit.setText(varStrings.getSTD_UNIT());
}
}
break;
case RATIO_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_ratio_lay, null, false);
}
if (varStrings != null) {
TextView name = (TextView) view.findViewById(R.id.tv_ratio_name);
TextView prim = (TextView) view.findViewById(R.id.tv_ratio_prim_val);
TextView sec = (TextView) view.findViewById(R.id.tv_ratio_second_val);
if (name != null) {
name.setText(varStrings.getRATIO_NAME());
}
if (prim != null) {
prim.setText(varStrings.getRATIO_PRIM());
}
if (sec != null) {
sec.setText(varStrings.getRATIO_SECOND());
}
}
break;
case CHECK_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_check_lay, null, false);
}
if (varStrings != null) {
CheckBox check = (CheckBox) view.findViewById(R.id.cb_check_name);
if (check != null) {
check.setText(varStrings.getCHECK_NAME());
check.setChecked(varStrings.getCHECK_VALUE());
}
}
break;
case DATE_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_date_lay, null, false);
}
if (varStrings != null) {
TextView name = (TextView) view.findViewById(R.id.tv_date_name);
TextView day = (TextView) view.findViewById(R.id.tv_date_day_var);
TextView month = (TextView) view.findViewById(R.id.tv_date_month_var);
TextView year = (TextView) view.findViewById(R.id.tv_date_year_var);
if (name != null) {
name.setText(varStrings.getDATE_NAME());
}
if (day != null) {
day.setText(varStrings.getDATE_DAY());
}
if (month != null) {
month.setText(varStrings.getDATE_MONTH());
}
if (year != null) {
year.setText(varStrings.getDATE_YEAR());
}
}
break;
case TIME_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_time_lay, null, false);
}
if (varStrings != null) {
TextView name = (TextView) view.findViewById(R.id.tv_time_name);
TextView hour = (TextView) view.findViewById(R.id.tv_time_hour_val);
TextView minute = (TextView) view.findViewById(R.id.tv_time_min_val);
if (name != null) {
name.setText(varStrings.getTIME_NAME());
}
if (hour != null) {
hour.setText(varStrings.getTIME_HOUR());
}
if (minute != null) {
minute.setText(varStrings.getTIME_MINUTE());
}
}
break;
case WRITE_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_btnwrite_lay, null, false);
}
break;
case SPINNER_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_spin_lay, null, false);
}
// Determine Drop List to use and return adapter with correct drop list
drop_arrayAdapt = getArrayDroplist(position, buttontype);
if (varStrings != null) {
TextView spin_name = (TextView) view.findViewById(R.id.tv_spin_name);
if (spin_name != null) {
spin_name.setText(varStrings.getSPIN_NAME());
}
Spinner spin_val = (Spinner) view.findViewById(R.id.sp_spin_name);
drop_arrayAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin_val.setAdapter(drop_arrayAdapt);
spin_val.setEnabled(false); // Can't click on Spinner
if (spin_val != null) {
spin_val.setSelection(varStrings.getSPIN_POS());
}
spin_val.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("50") || selectedItem.equals("60"))
{
//spinSendValue(buttontype,selectedSubItem,position);
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
spin_val.setEnabled(true); // Can't click on Spinner
}
break;
case DOUB_CHECK_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.doublecheck_entry_lay, null, false);
}
if (varStrings != null) {
CheckBox check1 = (CheckBox) view.findViewById(R.id.cb_doubcheck_name1);
if (check1 != null) {
check1.setText(varStrings.getDoubCHECK_NAME1());
check1.setChecked(varStrings.getDoubCHECK_VALUE1());
}
CheckBox check2 = (CheckBox) view.findViewById(R.id.cb_doubcheck_name2);
if (check2 != null) {
check2.setText(varStrings.getDoubCHECK_NAME2());
check2.setChecked(varStrings.getDoubCHECK_VALUE2());
}
}
break;
case SPIN_CHECK_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_spin_check_lay, null, false);
}
// Determine Drop List to use and return adapter with correct drop list
drop_arrayAdapt = getArrayDroplist(position, buttontype);
if (varStrings != null) {
TextView spin_name = (TextView) view.findViewById(R.id.tv_spincheck_name);
if (spin_name != null) {
spin_name.setText(varStrings.getSPINCHECK_NAME1());
}
Spinner spin_val = (Spinner) view.findViewById(R.id.sp_spincheck_name);
drop_arrayAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin_val.setAdapter(drop_arrayAdapt);
spin_val.setEnabled(false); // Cant click on Spinner
if (spin_val != null) {
spin_val.setSelection(varStrings.getSPINCHECK_POS());
}
CheckBox check = (CheckBox) view.findViewById(R.id.cb_spincheck_name);
if (check != null) {
check.setText(varStrings.getSPINCHECK_NAME2());
check.setChecked(varStrings.getSPINCHECK_VALUE2());
}
}
break;
case DOUB_HEADING_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_doub_heading_lay, null, false);
}
if (varStrings != null) {
TextView textView1 = (TextView) view.findViewById(R.id.head1_name);
if (textView1 != null) {
textView1.setText(varStrings.getDoubHEAD_NAME1());
}
TextView textView2 = (TextView) view.findViewById(R.id.head2_name);
if (textView2 != null) {
textView2.setText(varStrings.getDoubHEAD_NAME2());
}
}
break;
case DOUB_STANDARD_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_doub_std_lay, null, false);
}
if (varStrings != null) {
TextView textView1 = (TextView) view.findViewById(R.id.tv_doubstd_name);
TextView value1 = (TextView) view.findViewById(R.id.tv_doubstd_val1);
TextView value2 = (TextView) view.findViewById(R.id.tv_doubstd_val2);
TextView unit = (TextView) view.findViewById(R.id.tv_doubstd_unit);
if (textView1 != null) {
textView1.setText(varStrings.getDOUBSTD_NAME());
}
if (value1 != null) {
value1.setText(varStrings.getDOUBSTD_VALUE1());
}
if (value2 != null) {
value2.setText(varStrings.getDOUBSTD_VALUE2());
}
if (unit != null) {
unit.setText(varStrings.getDOUBSTD_UNIT());
}
}
break;
case STATUS_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_status_lay, null, false);
}
if (varStrings != null) {
TextView textView = (TextView) view.findViewById(R.id.tv_status_name);
if (textView != null) {
textView.setText(varStrings.getSTATUS_NAME());
}
ImageView imageView = (ImageView) view.findViewById(R.id.im_status_image);
if (imageView != null) {
imageView.setImageResource(varStrings.getSTATUS_IMAGE());
}
}
break;
case SPIN_CHECK_STAT_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_spin_check_stat_lay, null, false);
}
// Determine Drop List to use and return adapter with correct drop list
drop_arrayAdapt = getArrayDroplist(position, buttontype);
if (varStrings != null) {
TextView spin_name = (TextView) view.findViewById(R.id.tv_spinchstat_name);
if (spin_name != null) {
spin_name.setText(varStrings.getSPINCHECKSTAT_NAME1());
}
Spinner spin_val = (Spinner) view.findViewById(R.id.sp_spinchstat_name);
drop_arrayAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin_val.setAdapter(drop_arrayAdapt);
spin_val.setEnabled(false); // Cant click on Spinner
if (spin_val != null) {
spin_val.setSelection(varStrings.getSPINCHECKSTAT_POS());
}
CheckBox check = (CheckBox) view.findViewById(R.id.cb_spinchstat_name);
if (check != null) {
check.setText(varStrings.getSPINCHECKSTAT_NAME2());
check.setChecked(varStrings.getSPINCHECKSTAT_VALUE2());
}
ImageView imageView = (ImageView) view.findViewById(R.id.im_spinchstat_image);
if (imageView != null) {
imageView.setImageResource(varStrings.getSPINCHECKSTAT_IMAGE());
}
}
break;
case STANDARD_STAT_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_std_stat_lay, null, false);
}
if (varStrings != null) {
TextView name = (TextView) view.findViewById(R.id.tv_stdstat_name);
TextView value = (TextView) view.findViewById(R.id.tv_stdstat_val);
TextView unit = (TextView) view.findViewById(R.id.tv_stdstat_unit);
ImageView imageView = (ImageView) view.findViewById(R.id.im_stdstat_image);
if (name != null) {
name.setText(varStrings.getSTDSTAT_NAME());
}
if (value != null) {
value.setText(varStrings.getSTDSTAT_VALUE());
}
if (unit != null) {
unit.setText(varStrings.getSTDSTAT_UNIT());
}
if (imageView != null) {
imageView.setImageResource(varStrings.getSTDSTAT_IMAGE());
}
}
break;
case EVENT_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_event_lay, null, false);
}
if (varStrings != null) {
TextView number = (TextView) view.findViewById(R.id.tv_event_num);
TextView date = (TextView) view.findViewById(R.id.tv_event_date);
TextView time = (TextView) view.findViewById(R.id.tv_event_time);
TextView type = (TextView) view.findViewById(R.id.tv_event_type);
TextView alarms = (TextView) view.findViewById(R.id.tv_event_alarms);
TextView trips = (TextView) view.findViewById(R.id.tv_event_trips);
if (number != null) {
number.setText(varStrings.getEvent_Num());
}
if (date != null) {
date.setText(varStrings.getEvent_Date());
}
if (time != null) {
time.setText(varStrings.getEvent_Time());
}
if (type != null) {
type.setText(varStrings.getEvent_Type());
}
if (alarms != null) {
alarms.setText(varStrings.getEvent_Alarm());
}
if (trips != null) {
trips.setText(varStrings.getEvent_Trip());
}
}
break;
case FAULT_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_fault_lay, null, false);
}
if (varStrings != null) {
TextView number = (TextView) view.findViewById(R.id.tv_fault_num);
TextView date = (TextView) view.findViewById(R.id.tv_fault_date);
TextView time = (TextView) view.findViewById(R.id.tv_fault_time);
TextView type = (TextView) view.findViewById(R.id.tv_fault_type);
TextView trips = (TextView) view.findViewById(R.id.tv_fault_trips);
if (number != null) {
number.setText(varStrings.getFault_Num());
}
if (date != null) {
date.setText(varStrings.getFault_Date());
}
if (time != null) {
time.setText(varStrings.getFault_Time());
}
if (type != null) {
type.setText(varStrings.getFault_Type());
}
if (trips != null) {
trips.setText(varStrings.getFault_Trip());
}
}
break;
case BARCHART_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_barrchart_lay, null, false);
}
if (varStrings != null) {
BarChart barChart = (BarChart) view.findViewById(R.id.barchart);
switch (barStart) {
case NO:
barChart.setDescription("Fundamentals");
barChart.setData(varStrings.getBARCHART_DATA()); // set the data and list of labels into bar_chart
//barChart.animateY(400);
barStart = YES;
break;
case YES:
barChart.setDescription("Fundamentals");
barChart.setData(varStrings.getBARCHART_DATA()); // set the data and list of labels into bar_chart
barChart.notifyDataSetChanged(); // notify that data changed in barchart
barChart.invalidate(); // notify that data changed in barchart
//barChart.animateY(400);
break;
}
}
break;
case WarnAlrTrip_LAYOUT:
if (view == null) {
view = layoutInflater.inflate(R.layout.entry_flags_lay, null, false);
}
if (varStrings != null) {
TextView fname = (TextView) view.findViewById(R.id.var_flag_name);
ImageView wImage = (ImageView) view.findViewById(R.id.var_warn_indicate);
ImageView aImage = (ImageView) view.findViewById(R.id.var_alarm_indicate);
ImageView tImage = (ImageView) view.findViewById(R.id.var_trip_indicate);
if (fname != null) {
fname.setText(varStrings.get_flag_NAME());
}
if (wImage != null) {
wImage.setImageResource(varStrings.get_warn_Image());
}
if (aImage != null) {
aImage.setImageResource(varStrings.get_alarm_Image());
}
if (tImage != null) {
tImage.setImageResource(varStrings.get_trip_Image());
}
}
break;
}
//Todo Followed this link: https://stackoverflow.com/questions/18799216/how-to-make-a-edittext-box-in-a-dialog
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte lay_type = 0;
int al_layout = 0;
lay_type = (byte) getItemViewType(position);
// Only start AlertDialog for these conditions
if (!(buttontype.contains("Actual"))) {
if ((lay_type != EVENT_LAYOUT) && (lay_type != FAULT_LAYOUT) && (lay_type != STATUS_LAYOUT) && (lay_type != HEADING_LAYOUT) && (lay_type != DOUB_HEADING_LAYOUT)) {
// Blocks an attempt to change MLC on a KG relay
if ((relay_pid.equals("KG")) && (position == 1) && (buttontype.contains("Setting")) && (selectedSubItem == SUB_CT_VT_Rel1)){
Utils.toast(getContext(), "Set MLC on KG Relay POT", "CENTER");
}
// All Spinner selections is done by spinner.setOnItemSelectedListener, so the alertDialog will not pop up when selecting a row containing a spinner.
else if ((buttontype.contains("Setting")) && (selectedSubItem == SUB_CT_VT_Rel1) && ((position == 8) || (position == 16))){
Utils.toast(getContext(), "Click on dropdown arrow", "CENTER");
}
// All valid setting changes
else {
// Change layout type for alert dialog based on row item selected
al_layout = alertDialog_Lay_Type(position);
View view = View.inflate(context, al_layout, null);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Edit Value");
//alertDialog.setIcon("Icon id here");
alertDialog.setCancelable(false);
// Populate AlertDialog with correct variables based on selection
alertDialog_Populate(position, view);
// Add positive and Negative Button
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
intentSendValue(position, view);
alertDialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
// create and show the alert dialog
alertDialog.setView(view);
alertDialog.show();
}
}
}
}
});
return view;
}
The problem I have is that some rows contain a spinner, and because I have an OnclickListener on any row, I had to make all spinners nonclickable. otherwise, the spinner could be clicked or the row where the spinner is located can also be clicked which will bring the alert dialog up. So to avoid confusion for the user I disabled the ability to click on a spinner (See inside "case SPINNER_LAYOUT").
The problem is that my boss does not like the grayed-out look of the spinners (It looks like it's non-selectable).
So what I want to do now is to make the spinners selectable in the listview and prevent the user to select that specific row that contains the spinner, this way the spinner will be the normal black (not grayed out). Then I want to create an onItemSelectListener to handle the clicks on spinners.
So Im not sure where to add the OnItemSelectListeners.
Inside the case containing the spinners?
- "case SPINNER_LAYOUT:"
- "case SPIN_CHECK_LAYOUT:"
- "case SPIN_CHECK_STAT_LAYOUT:" If I add the OnItemSelectListeners inside the case structures, then multiple OnItemSelectListeners will be created as many as there are spinners in the listview.
Outside the switch structure, but this I'm not sure how to do because each spinner is defined inside the case structure and can't be used outside of it.
Here is how the listview looks, the rows containing "VL frequency" and "System type" has a spinner in. (I enabled the spinner selection when I took this screenshot, so it's not grayed out in this image).
This is the alert dialog that comes up when the user clicks on any row that contains data.
So I basically need help on how and where to place the setOnItemSelectedListener for the spinners in the customListviewAdapter.
Hope this makes sense. Marinus