no value returned from EditText .getText() method - android

I am stuck with a simple problem of getting text from editText.
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder placeLLDialog= new AlertDialog.Builder(PlacesActivity.this);
LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.place_add_dialog, null);
placeLLDialog.setView(R.layout.place_add_dialog);
final EditText place = view.findViewById(R.id.placeName);
final EditText lati = view.findViewById(R.id.placeLati);
final EditText longi = view.findViewById(R.id.placeLongi);
placeLLDialog.setTitle("Add Place with Latitude and Longitude")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG, "Helloooo" + place.getText().toString());
if(!place.getText().toString().equals("") &&
!lati.getText().toString().equals("") &&
!longi.getText().toString().equals("")) {
Log.e(TAG, "Hello" + place.getText().toString());
final Places places = new Places(place.getText().toString(),
lati.getText().toString(), longi.getText().toString());
mPlacesViewModel.insert(places);
}
closeFABMenu();
}
})
.setNegativeButton("Cancel", null)
.show();
}
When I am doing this, I am not getting the value of place, lati and longi,
i.e. "place.getText().toString()" is empty.
Can anybody kindly help me with this strange problem?

You are inflating the view here:
final View view = inflater.inflate(R.layout.place_add_dialog, null);
but you are not using it just one line after:
placeLLDialog.setView(R.layout.place_add_dialog);
So you should be setting:
placeLLDialog.setView(view);

fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder placeLLDialog = new AlertDialog.Builder(PlacesActivity.this);
LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.place_add_dialog,null);
placeLLDialog.setView(view);
final EditText place=(EditText)view.findViewById(R.id.placeName);
final EditText lati = (EditText)view.findViewById(R.id.placeLati);
final EditText longi = (EditText)view.findViewById(R.id.placeLongi);
placeLLDialog.setTitle("Add Place with Latitude and Longitude")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG, "Helloooo" + place.getText().toString());
if(!place.getText().toString().equals("") && !lati.getText().toString().equals("") && !longi.getText().toString().equals("")) {
Log.e(TAG, "Hello" + place.getText().toString());
final Places places = new Places(place.getText().toString(),lati.getText().toString(), longi.getText().toString());
mPlacesViewModel.insert(places);
}
closeFABMenu();
}
})
.setNegativeButton("Cancel", null)
.show();
}
});

Related

Android Listview multiple buttons click

My issue is regarding listview multiple buttons click. e.g. my list view contain 3 rows and each row contains 3 buttons. when i click on first button in first row it changes color in last row first button
please check image you will know the problem
I have tried to solve it using button tags, view holder, tried to assign position to button but still nothing happened
public class ViewOrderAdapter extends BaseAdapter {
#SuppressWarnings("unused")
private LayoutInflater mInflater;
Context mcontext;
ArrayList<ViewOrder> view_order_array = null;
SimpleDateFormat sdf;
String currentTime;
String ord_id;
public Button accept_btn, pickup_btn, delieverd_btn, order_view, rest_address, cust_address;
GPSTracker gps;
String latitude, longitude;
private static int ipos;
boolean[] buttonState;
AlertDialog alert;
View view;
OrdersFragment ord_frag;
ViewOrderAdapter view_order_adapter1;
public static final String PREFS_NAME = "MyApp_Settings";
SharedPreferences settings;
SharedPreferences.Editor editor;
SharedPreferences prefs;
public ViewOrderAdapter (Context context, ArrayList<ViewOrder> view_order_array) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mcontext = context;
prefs = mcontext.getSharedPreferences(PREFS_NAME, mcontext.MODE_PRIVATE);
this.view_order_array = view_order_array;
buttonState = new boolean[view_order_array.size()];
gps = new GPSTracker(mcontext);
}
#Override
public int getCount() {
return view_order_array.size();
}
#Override
public Object getItem(int position) {
return view_order_array.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.order_single_row, parent, false);
view = convertView;
sdf = new SimpleDateFormat("HH:mm:ss");
currentTime = sdf.format(new Date());
final ViewOrder v_ordr = view_order_array.get(position);
TextView cname = (TextView)convertView.findViewById(R.id.restaurant_name_view);
cname.setText(v_ordr.getRestaurant());
TextView caddr = (TextView)convertView.findViewById(R.id.restaurant_address_view);
caddr.setText(v_ordr.getRaddress());
accept_btn = (Button)convertView.findViewById(R.id.order_accept_btn);
pickup_btn = (Button)convertView.findViewById(R.id.order_pickup_btn);
delieverd_btn = (Button)convertView.findViewById(R.id.order_delievery_btn);
accept_btn.setTag(position);
pickup_btn.setTag(position);
delieverd_btn.setTag(position);
order_view = (Button)convertView.findViewById(R.id.order_info);
rest_address = (Button)convertView.findViewById(R.id.rest_addrs);
cust_address = (Button)convertView.findViewById(R.id.cstom_addrs);
int id = Integer.parseInt(view_order_array.get(position).getId());
accept_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
AppConstants.btn_id = view_order_array.get(position).getId();
String id = AppConstants.btn_id;
//accept_btn.setId(position);
buttonState[position] = false;
String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth);
ShowMessagesDialogsUtitly.showProgressDialog(mcontext);
RequestParams requestParams = new RequestParams();
requestParams.put("k", token);
requestParams.put("id", id); //order id
requestParams.put("status", Integer.toString(1));
requestParams.put("time", currentTime);
requestParams.put("page", "send");
HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback() {
#Override
public void onCompleteHttpResponse(String whichUrl, String jsonResponse) {
ShowMessagesDialogsUtitly.hideProgressDialog();
if (jsonResponse != null) {
try {
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
if (jsonResponseObj.getInt("status") == 1) {
accept_btn.setBackgroundColor(Color.GRAY);
Intent n = new Intent(mcontext, Order.class);
mcontext.startActivity(n);
((Activity)mcontext).finish();
} else {
Toast.makeText(mcontext, "Order progress error", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
AppUtilites.printStackTrace(e);
}
} else {
Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show();
}
}
});
}
});
pickup_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AppConstants.btn_id = view_order_array.get(position).getId();
String id = AppConstants.btn_id;
String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth);
pickup_btn.setId(position);
ShowMessagesDialogsUtitly.showProgressDialog(mcontext);
RequestParams requestParams = new RequestParams();
requestParams.put("k", token);
requestParams.put("id", id); //order id
requestParams.put("status", Integer.toString(2));
requestParams.put("time", currentTime);
requestParams.put("page", "send");
HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback() {
#Override
public void onCompleteHttpResponse(String whichUrl, String jsonResponse) {
ShowMessagesDialogsUtitly.hideProgressDialog();
if (jsonResponse != null) {
try {
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
Toast.makeText(mcontext, "Order accepted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mcontext, "Order order not accepted", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
AppUtilites.printStackTrace(e);
}
} else {
Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show();
}
}
});
}
});
delieverd_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AppConstants.btn_id = view_order_array.get(position).getId();
String id = AppConstants.btn_id;
String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth);
delieverd_btn.setId(position);
ShowMessagesDialogsUtitly.showProgressDialog(mcontext);
RequestParams requestParams = new RequestParams();
requestParams.put("k", token);
requestParams.put("id", id); //order id
requestParams.put("status", Integer.toString(3));
requestParams.put("time", currentTime);
requestParams.put("page", "send");
HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback() {
#Override
public void onCompleteHttpResponse(String whichUrl, String jsonResponse) {
ShowMessagesDialogsUtitly.hideProgressDialog();
if (jsonResponse != null) {
try {
JSONObject jsonResponseObj = new JSONObject(jsonResponse);
if (jsonResponseObj.getInt("status") == 1) {
} else {
Toast.makeText(mcontext, "Order order not delievered", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
AppUtilites.printStackTrace(e);
}
} else {
Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show();
}
}
});
}
});
order_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.order_view_lay, (ViewGroup)view.findViewById(R.id.order_view_lay_id));
AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setView(layout);
alert = builder.create();
TextView order_id = (TextView) layout.findViewById(R.id.order_no);
order_id.setText(view_order_array.get(position).getOid());
TextView rest_name = (TextView) layout.findViewById(R.id.rstarnt_name);
rest_name.setText(view_order_array.get(position).getRestaurant());
TextView quantity = (TextView) layout.findViewById(R.id.quantity);
quantity.setText(view_order_array.get(position).getOid());
TextView item = (TextView) layout.findViewById(R.id.item);
item.setText(view_order_array.get(position).getMenu());
TextView price = (TextView) layout.findViewById(R.id.price);
price.setText(view_order_array.get(position).getAmount());
TextView subtotal = (TextView) layout.findViewById(R.id.sub_total);
subtotal.setText("XXXX");
TextView total = (TextView) layout.findViewById(R.id.total);
total.setText("XXXX");
Button ok = (Button) layout.findViewById(R.id.ok_popup_order_view_btn);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
alert.show();
}
});
rest_address.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.restaurant_addr_lay, (ViewGroup)view.findViewById(R.id.rest_addr_lay_id));
AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setView(layout);
alert = builder.create();
TextView r_name = (TextView) layout.findViewById(R.id.rst_name_popup);
r_name.setText(view_order_array.get(position).getRestaurant());
TextView r_add = (TextView) layout.findViewById(R.id.rst_addr_popup);
r_add.setText(view_order_array.get(position).getRaddress());
TextView r_phone = (TextView) layout.findViewById(R.id.rst_phone_popup);
r_phone.setText(view_order_array.get(position).getRestaurant());
Button map = (Button) layout.findViewById(R.id.map_to_restaurant_popup);
map.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (gps.canGetLocation()) {
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getRaddress();
String addr = AppConstants.Address_To;
gps.getLocationFromAddress(mcontext, addr);
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
} else {
gps.showSettingsAlert();
}
}
});
Button map_dirct = (Button) layout.findViewById(R.id.directions_to_restaurant_popup);
map_dirct.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (gps.canGetLocation()) {
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getRaddress();
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
} else {
gps.showSettingsAlert();
}
}
});
Button ok = (Button) layout.findViewById(R.id.ok_popup_raddr_btn);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
alert.show();
}
});
cust_address.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.customer_addr_lay, (ViewGroup)view.findViewById(R.id.cust_addr_lay_id));
AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setView(layout);
alert = builder.create();
TextView c_name = (TextView) layout.findViewById(R.id.cst_name_popup);
c_name.setText(view_order_array.get(position).getCustomer());
TextView c_add = (TextView) layout.findViewById(R.id.cst_addr_popup);
c_add.setText(view_order_array.get(position).getCaddress());
TextView c_phone = (TextView) layout.findViewById(R.id.cst_phone_popup);
c_phone.setText(view_order_array.get(position).getCphone());
TextView order_inst = (TextView) layout.findViewById(R.id.cst_order_inst_popup);
order_inst.setText(view_order_array.get(position).getInstruction());
Button map = (Button) layout.findViewById(R.id.map_to_customer_popup);
map.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (gps.canGetLocation()) {
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getCaddress();
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
} else {
gps.showSettingsAlert();
}
}
});
Button map_direct = (Button) layout.findViewById(R.id.directions_to_customer_popup);
map_direct.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (gps.canGetLocation()) {
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
AppConstants.Latitude = latitude.toString();
AppConstants.Longitude = longitude.toString();
double lat = Double.valueOf(latitude);
double lon = Double.valueOf(longitude);
gps.getCompleteAddressString(lat, lon);
AppConstants.Address_To = view_order_array.get(position).getCaddress();
Intent intent = new Intent(mcontext, MapLayout.class);
mcontext.startActivity(intent);
} else {
gps.showSettingsAlert();
}
}
});
Button ok = (Button) layout.findViewById(R.id.ok_popup_caddr_btn);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
alert.show();
}
});
return convertView;
}
}
Try this simple answer:
find your button view by the tag you have set.
((Button) convertView.findViewWithTag(position)).setBackgroundColor(Color.GRAY);
You are encountering this problem because the last accept_btn object your adapter is holding is the last view object it created.
Therefore you are on the right track by setting a tag on the button. But then you have to retrieve that view by the tag.
Let me know if this is helpful.
Thanks.
I have written answer for same problem in gridview HERE. You can use same trick to solve this issue.
// Brief idea
You can implement your own custom OnClickListener and you can associate id/tag/position for each onclick of each item of listview.
yourBtn.setOnclickListener(new BtnClick(position, btnid));
Custom OnclickListener:
Class BtnClick View.OnClickListener
{
int position;
int btnId;
Public BtnClick(int position, int id)
{
this.position = position;'
this.btnId = btnId;
}
#Override
public onClick(View v)
{
// You can add logic here for each item clicked using position and id of each item you passed in constructor
}
}

Android getting the EditText value from an AlertDialog in a Fragment

I am getting a null pointer exception when I click the OK button. and I am trying to get the value form an EditText in an AlertDialog. I am calling the AlertDialog from a fragment.
I believe this line is the problem
View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
I think it is because I am passing null but I am not sure what eles to pass. Please note I am calling the AlertDialog from a fragment not an Activity.
public class fragment_account extends myFragment {
private final String firebase = "https://xxxxxxxx.firebaseio.com";
private String email = "";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account, container, false);
SharedPreferences sp = getActivity().getPreferences(0);
TextView address = (TextView) view.findViewById(R.id.textViewEmailAddress);
email = sp.getString("email", "");
address.setText(email);
ImageView imageChangePass = (ImageView) view.findViewById(R.id.imageChangePass);
imageChangePass.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
showInputDialog(v);
}
}
);
return view;
}
protected void showInputDialog(final View view) {
try
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View promptView = layoutInflater.inflate(R.layout.passchange_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getContext());
alertDialogBuilder.setView(promptView);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText edittextOldPass = (EditText) view.findViewById(R.id.edittextOldPass);
EditText edittextNewPass = (EditText) view.findViewById(R.id.edittextNewPass);
EditText edittextConfirmPass = (EditText) view.findViewById(R.id.edittextConfirmPass);
final String oldPass = edittextOldPass.getText().toString().trim();
final String newPass = edittextNewPass.getText().toString().trim();
final String confirmPass = edittextConfirmPass.getText().toString().trim();
Firebase ref = new Firebase(firebase);
ref.changePassword(email, oldPass, newPass, new Firebase.ResultHandler() {
#Override
public void onSuccess() {
}
#Override
public void onError(FirebaseError firebaseError) {
}
});
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
catch (Exception e)
{
Log.i("myStuff", e.getMessage());
}
}
}
Any help or suggestions would be greatly appreciated. Thanks
I think you should call findViewById on promptView instead of on view variable
I guess you should initialize all those textview's before setting the layout of your dialog instead of inside your onClick method. As you have to access their values, you'll have to declare them final in order to get what the user typed in inside of your onClick function.

Refresh Does not work at update time

public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
DatabaseHelper mydb;
int pos = 0;
enter code here
public ArrayList<Employee_info> mArrayList = new ArrayList<Employee_info>();
public ListViewAdapter(Context context,ArrayList<Employee_info> arrayList){
mContext=context;
mArrayList=arrayList;
mLayoutInflater=LayoutInflater.from(mContext);
mydb = new DatabaseHelper(mContext);
}
enter code here
#Override
public int getCount() {
return mArrayList.size();
}
enter code here
#Override
public Object getItem(int position) {
return mArrayList.get(position);
}
enter code here
#Override
public long getItemId(int position) {
return position;
}
enter code here
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder=new ViewHolder();
View view=convertView;
if(view == null){
//view = mLayoutInflater.inflate(R.layout.list_view,parent,false);
view = mLayoutInflater.inflate(R.layout.list_view,null);
holder.txtName = (TextView)view.findViewById(R.id.txtName);
holder.txtCode = (TextView)view.findViewById(R.id.txtCode);
holder.txtStatus = (TextView)view.findViewById(R.id.txtStatus);
//// Start //////////////
Button b1 = (Button) view.findViewById(R.id.button1);
Button b2 = (Button) view.findViewById(R.id.button2);
Button b3 = (Button) view.findViewById(R.id.button3);
b1.setTag(position);
final Employee_info emp_info = mArrayList.get(position);
b2.setTag(position);
b3.setTag(position);
final ViewHolder finalHolder = holder;
b3.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
int x = (int) v.getTag();
/* if (itemPos.contains(v)) {
finalHolder.txtCode.setTextColor(Color.RED);
finalHolder.txtName.setTextColor(Color.RED);
finalHolder.txtStatus.setTextColor(Color.RED);
}
else {*/
if (mArrayList.get(x).getGet_status() != null) {
if (mArrayList.get(x).getGet_status().equals("Unpaid")) {
String upd1 = mArrayList.get(x).getGet_code();
String upd2 = mArrayList.get(x).getGet_name();
String upd3 = "Paid";
mydb.UpdateData(upd1, upd2, upd3);
// THis code for only catch Month, year, Employee ID, Status
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(cal.getTime());
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
mydb.insertEmpSalary(upd1, month_name, String.valueOf(thisYear), upd3);
finalHolder.txtStatus.setTextColor(Color.GREEN);
ListViewAdapter.this.notifyDataSetChanged();
} else {
String upd1 = mArrayList.get(x).getGet_code();
String upd2 = mArrayList.get(x).getGet_name();
String upd3 = "Unpaid";
mydb.UpdateData(upd1, upd2, upd3);
finalHolder.txtStatus.setTextColor(Color.RED);
}
}
}
//}
});
b1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(final View position) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try
{
pos = (int) position.getTag();
mydb.deleteData(mArrayList.get(pos).getGet_code());
mArrayList.remove(pos);
ListViewAdapter.this.notifyDataSetChanged();
Toast.makeText(mContext, mArrayList.get(pos).getGet_code() + " Employee is Delete", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
e.printStackTrace();
}
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
b2.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here
alert.setMessage("Enter Your Code");
pos = (int) v.getTag();
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText input = new EditText(mContext);
input.setText(mArrayList.get(pos).getGet_code());
layout.addView(input);
final EditText input1 = new EditText(mContext);
input1.setText(mArrayList.get(pos).getGet_name());
layout.addView(input1);
final EditText input2 = new EditText(mContext);
input2.setText(mArrayList.get(pos).getGet_status());
layout.addView(input2);
alert.setView(layout);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
String srt = input.getEditableText().toString();
Log.d("ccccccc", srt);
String upd1 = input.getText().toString();
String upd2 = input1.getText().toString();
String upd3 = input2.getText().toString();
/*String upd1 = mArrayList.get(pos).getGet_code();
String upd2 = mArrayList.get(pos).getGet_name();
String upd3 = mArrayList.get(pos).getGet_status();
Log.d("Code1",mArrayList.get(pos).getGet_code());
Log.d("Name1",mArrayList.get(pos).getGet_name());
Log.d("Status1",mArrayList.get(pos).getGet_status());*/
Log.d("Code",upd1);
Log.d("Name", upd2);
Log.d("Status", upd3);
boolean isUpdate = mydb.UpdateData(upd1, upd2, upd3);
ListViewAdapter.this.notifyDataSetChanged();
notifyDataSetChanged();
if (isUpdate == true)
{
Log.e("Update Complete", String.valueOf(isUpdate));
ListViewAdapter.this.notifyDataSetChanged();
Toast.makeText(mContext, srt + " Employee is Updated", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(mContext, srt + " Employee is not Updated", Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
dialog.dismiss();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
view.setTag(holder);
} else {
holder=(ViewHolder)view.getTag();
}
Employee_info employee_info = mArrayList.get(position);
holder.txtName.setText(employee_info.getGet_name());
holder.txtCode.setText(employee_info.getGet_code());
holder.txtStatus.setText(employee_info.getGet_status());
return view;
}
private class ViewHolder{
private TextView txtName,txtCode,txtStatus;
public Button b1;
}
}
You can't add values in getView. let's do it in activity

how to disable button in listview

I am new to Android development and I am having a hard time wrapping my head around the android listviews. I pretty much went through entire google search and tried every "possible" solution, but without really getting the listview, I am having a hard time solving this problem.
I want to disable particular button when I click the complete_btn in listview item.
Right now, the complete_btn.setOnClickListener in the else {} part is giving me the null pointer exception (it's fine in the if(converView == null) part). If I remove the code, everything works fine, but even simply commenting out everything in the listener does not work.
I want to eventually disable the button if Yes is clicked in the Alert dialog that pops up when the button is pressed! Could someone please help me with this??
public View getView(final int position, View convertView, final ViewGroup parent) {
View itemView;
if (convertView == null) {
itemView = layoutInflater.inflate(R.layout.activity_selected_delivery_item, parent, false);
final Deliveryltem deliveryltemPosition = epicerieDelivery.selectedDeliveryItem.get(position);
icon = (ImageView) itemView.findViewById(R.id.selected_delivery_img);
name = (TextView) itemView.findViewById(R.id.selected_item_name);
phone_tx = (TextView) itemView.findViewById(R.id.selected_item_phone);
complete_btn = (Button) itemView.findViewById(R.id.selected_complete_btn);
if(deliveryltemPosition.order_taken_str.equals("2")){
complete_btn.setEnabled(false);
}
phone_tx.setText(deliveryltemPosition.recipient_phonenum);
complete_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position , 0);
try{
order_seq = deliveryltemPosition.order_seq;
position_sms = position;
Deliveryltem deliveryltemPosition1 = epicerieDelivery.selectedDeliveryItem.get(position);
String name = deliveryltemPosition1.recipient_name;
String phone = deliveryltemPosition1.recipient_phonenum;
AlertDialog dialog = createdialogBox_finish(name, phone, position);
dialog.show();
}catch (Exception e){
}
}
});
return itemView;
}else{
itemView = convertView;
if(epicerieDelivery.selectedDeliveryItem.size() != 0){
final Deliveryltem deliveryltemPosition = epicerieDelivery.selectedDeliveryItem.get(position);
name = (TextView) itemView.findViewById(R.id.selected_item_name);
phone_tx = (TextView) itemView.findViewById(R.id.selected_item_phone);
complete_btn = (Button) itemView.findViewById(R.id.selected_complete_btn);
if(deliveryltemPosition.order_taken_str.equals("2")){
complete_btn.setEnabled(false);
}
complete_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position , 0);
try{
order_seq = deliveryltemPosition.order_seq;
position_sms = position;
Deliveryltem deliveryltemPosition1 = epicerieDelivery.selectedDeliveryItem.get(position);
String name = deliveryltemPosition1.recipient_name;
String phone = deliveryltemPosition1.recipient_phonenum;
buttons.add(complete_btn);
AlertDialog dialog = createdialogBox_finish(name, phone, position);
dialog.show();
}catch (Exception e){
}
}
});
}
return convertView;
}}
private AlertDialog createdialogBox_finish(String name, String phone, int position1){
final String name_str = name;
final String phone_str = phone;
courier_id = selectedActivity2.courier_id;
int button_pos = position1;
final String message_finish = "message_content";
buttons.get(0).setEnabled(false);
buttons.clear();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("배송을 완료하셨습니까?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try{
SmsManager smsManager = SmsManager.getDefault();
if (message_finish.length() > 60) {
ArrayList<String> contents = smsManager.divideMessage(message_finish);
for(int j = 0; j<contents.size(); j++){
smsManager.sendTextMessage(phone_str, null, contents.get(j), null, null);
}
} else {
smsManager.sendTextMessage(phone_str, null, message_finish, null, null);
}
}catch (Exception e){
}
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
return dialog;
}
Add
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
inside
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
}
method like :
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setEnabled(false);
try{
SmsManager smsManager = SmsManager.getDefault();
if (message_finish.length() > 60) {
ArrayList<String> contents = smsManager.divideMessage(message_finish);
for(int j = 0; j<contents.size(); j++){
smsManager.sendTextMessage(phone_str, null, contents.get(j), null, null);
}
} else {
smsManager.sendTextMessage(phone_str, null, message_finish, null, null);
}
}catch (Exception e){
}
}
});

Setting TextViews from an onClick(View v) within an onClick(View v)?

This line will not set, holder.t1.setText(NewItem);
If I move it to the parent onClick, with a hardcoded string (for testing) it does.
Keep in mind, this is inside a getView method of an ArrayAdapter. I am trying to setText to ListView rows.
Edit:
EXPANDED, COMPLETE getView() -- AS REQUESTED
(Did not have time to edit, will later, sorry!)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.commentlayout, parent,
false);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.labelComment);
holder.t2 = (TextView) convertView.findViewById(R.id.labelDate);
holder.t3 = (TextView) convertView.findViewById(R.id.labelUser);
holder.t3.setTypeface(tf);
holder.t4 = (TextView) convertView
.findViewById(R.id.labelHelpfulCount);
holder.t5 = (TextView) convertView
.findViewById(R.id.labelCommentCount);
holder.ib1 = (ImageView) convertView
.findViewById(R.id.labelChatIcon);
holder.ib2 = (ImageView) convertView
.findViewById(R.id.labelCommentFlag);
holder.rb1 = (RatingBar) convertView
.findViewById(R.id.myCommentsRatingBarSmall);
holder.b1 = (Button) convertView.findViewById(R.id.bReview1);
holder.b2 = (Button) convertView.findViewById(R.id.bReview2);
holder.b3 = (Button) convertView.findViewById(R.id.bReview3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
convertView.setOnCreateContextMenuListener(null);
}
ReviewObject ro = getItem(position);
final String item = ro.item;
final String review = ro.review;
final String username = ro.username;
Long date = Long.valueOf(ro.date);
String rating = ro.ratings;
String voteCount = ro.voteCount;
String chatcount = ro.chatCount;
String cat = ro.cat;
final ArrayList<String> passing = new ArrayList<String>();
passing.add(item);
passing.add(review);
passing.add(cat);
passing.add(username);
String time = "";
time = DateConvert.dateConvert(Long.valueOf(date));
holder.t1.setText(review);
holder.t2.setText(time);
holder.t3.setText(username);
holder.t4.setText(voteCount);
holder.t5.setText(chatcount);
holder.ib1.setImageResource(R.drawable.updown);
holder.ib2.setImageResource(R.drawable.comment);
holder.rb1.setRating(Float.valueOf(rating));
if (rating.equals("0")) {
holder.rb1.setEnabled(false);
}
holder.b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ReviewUser = holder.t3.getText().toString();
String ReviewWords = holder.t1.getText().toString();
Intent intent = new Intent(getContext(), Comments.class);
intent.putExtra("comment", ReviewWords);
intent.putExtra("user", ReviewUser);
intent.putExtra("item", item);
getContext().startActivity(intent);
}
});
if (!Rateit.username.equals(username)) {
holder.b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(getContext());
alertbox.setMessage("Did you like this?");
alertbox.setNegativeButton("Vote Up",
new DialogInterface.OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(DialogInterface arg0,
int arg1) {
String VoteTally = holder.t4.getText()
.toString();
int ReviewCountInt = Integer
.valueOf(VoteTally) + 1;
VoteTally = String.valueOf(ReviewCountInt);
holder.t4.setText(VoteTally);
new HelpfulTask().execute(passing);
}
});
alertbox.setPositiveButton("Vote Down",
new DialogInterface.OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(DialogInterface dialog,
int id) {
String VoteTally = holder.t4.getText()
.toString();
int ReviewCountInt = Integer
.valueOf(VoteTally) - 1;
VoteTally = String.valueOf(ReviewCountInt);
holder.t4.setText(VoteTally);
new UnHelpfulTask().execute(passing);
}
});
alertbox.setNeutralButton("Report Spam",
new DialogInterface.OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(DialogInterface dialog,
int id) {
new SpamTask().execute(passing);
}
});
alertbox.show();
}
});
} else {
holder.b2.setText("Edit");
holder.b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.editreview);
dialog.setTitle("Edit Review");
dialog.show();
final EditText etEdit = (EditText) dialog
.findViewById(R.id.etEditReview);
etEdit.setText(review);
Button bInsert = (Button) dialog.findViewById(R.id.bInsert);
bInsert.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(View v) {
NewItem = etEdit.getText().toString();
if (NewItem.equals("")) {
Toast.makeText(getContext(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
} else {
holder.t1.setText(NewItem);
passing.add(NewItem);
dialog.dismiss();
new EditCommentTask().execute(passing);
}
}
});
}
});
}
if (!Rateit.username.equals(username)) {
holder.b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getContext(), OtherProfile.class);
i.putExtra("userprofile", username);
getContext().startActivity(i);
}
});
} else {
holder.b3.setText("Delete");
holder.b3.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(View v) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(
getContext());
alertbox.setMessage("Are you sure you want to delete your review?");
alertbox.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
});
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
new DeleteReviewTask().execute(passing);
}
});
alertbox.show();
}
});
}
return convertView;
}
From what you've said in the above comments, this is the intended function. Since,
Except, the dialog does dismiss, I get the toast and the task runs correctly.
This is coded into your onClick() here:
bInsert.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(View v) {
NewItem = etEdit.getText().toString();
if (NewItem.equals("")) {
Toast.makeText(getContext(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
} else {
holder.t1.setText(NewItem);
passing.add(NewItem);
dialog.dismiss();
new EditCommentTask().execute(passing);
}
}
});
Specifically, this is executing:
if (NewItem.equals("")) {
Toast.makeText(getContext(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
}
so I am assuming your problem is with NewItem, I never see where you actually initialize it, but I am assuming it .equals("") since this is executing. Try throwing a Log.d or println() just below the line NewItem = etEdit.getText().toString(); to see what the value of NewItem is at this point.

Categories

Resources