On click in adapter class (ListView) - java

I need some help with my on click in my adapter class.
This is the code that I want to run when an item is clicked. connectOrDisconnectUser()
private void connectOrDisconnectUser(final int position) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
RelationshipRemoved.this);
builder.setMessage("Do you want to un tag " + usersInfo.get(position).get(
RelationshipRemoved.likedOne))
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
//do something
public void onClick(DialogInterface dialog, int randomvar) {
Log.d("OkHttpRemove", "getting removing!");
String username = (globalInt);
String UT = usersInfo.get(position).get(
RelationshipRemoved.likedOne);
String type = "remove";
BackgroundWorker backgroundWorker = new BackgroundWorker(getApplicationContext());
backgroundWorker.execute(type, username, UT);
startActivity(new Intent(RelationshipRemoved.this, MainActivity.class));
Toast.makeText(context, "UnTagged!",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
The file that holds the adapter is called AdapterClass and the class that uses the adapter class is Called RelationshipRemoved. When I try to write this code. I get an error on RelationshipRemoved.this on this line RelationshipRemoved.this);, startActivity and RelationshipRemoved.this on this line startActivity(new Intent(RelationshipRemoved.this, MainActivity.class));
What's wrong?

Related

How to give Choice of Numbers to Call using Intent.ACTION_CALL?

I am trying to give the option of two numbers on a click of a button to make a call on the number directly but when I am selecting one of the numbers it is going to the dialer screen but "Ljava.lang.CharSequence" is written there and a dialog box with a message service is not supported pops up.
Here is my code:-
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence numbers[] = new CharSequence[] {"02212345678","+14356789809"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +numbers;
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
}
});
builder.show();
}
});
Change
String call = "tel:" +numbers;
To
String call = "tel:" +numbers[which];
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String number = "02212345678";
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(number , new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +number ;
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
}
});
builder.show();
}
});
This is the complete code for giving choice of numbers and calling the selected number on a Button Click. I hope this will help other members too.
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence numbers[] = new CharSequence[] {"+1xxxxxxxxx","+1xxxxxxxxxx"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +numbers[which];
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
// the user clicked on colors[which]
}
});
builder.show();
}
});

intent in alart dialog [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
intent in alart dialog how pass in next intent
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
view.getContext().startActivity(new Intent(activity,DetailView.class));
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
you need to use activity or context, for example, please refer below code
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
activity.startActivity(new Intent(activity, DetailView.class));
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
you can get activity or context from the contractor.
Change this part of the code:
view.getContext().startActivity(new Intent(activity,DetailView.class));
To this:
Intent intent = new Intent(FirstActivity.this,Screen2Activity.class);
startActivity(intent);
Full code:
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
Intent intent = new Intent(FirstActivityName.this,Screen2ActivityName.class);
startActivity(intent);
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}

Execute after close dialog

I have a button that open a dialog for change a number. The button is in activity, and generate a new class names dialogs, for storage different dialogs.
Dialog consigna: Dialogs.class
public String consigna(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctxt);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(ctxt);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data = input.getText().toString();
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data;
}
on click button: MainActivity
//I need change these two values:
final public double temperatura = 200.3;
String newData;
...
public void onClick(View v) {
switch (v.getId()){
case R.id.tempConsigna:
dialog = new Dialogs(MainActivity.this);
String data = dialog.consigna();
newData = data;
break;
...//other cases...
The issue is in newData = data; I doesn't have a data, because the dialog not are closed. The dialog work in other thread,no?
How to change newData var with the dialog result? It is posible into a dialogs class?
You need a callback method implemented via an interface:
public String consigna(final OnConfirm confirm){
final String[] data = new String[1];
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data[0] = input.getText().toString();
confirm.onConfirm(data[0]);
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data[0];
}
public interface OnConfirm {
void onConfirm(String s);
}
And in your Main Activity
Dialogs dialog = new Dialogs(MainActivity.this);
dialog.consigna(new Dialogs.OnConfirm() {
#Override
public void onConfirm(String s) {
Log.d("data", s);
}
});
It will not return result as you want but after click on dialog ok button then access data variable. no need to return.

Sending alert dialog entry to mysql database

Am new to android and i have successfully manged to show data from mysql database in a list and set an alert dialog for when an item is clicked.The Alert dialog has to take in a comment about the item clicked and send it to the mysql database but am stuck at making the connection.
Here is my code
public void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String fname = c.getString(TAG_FNAME);
String lname = c.getString(TAG_LNAME);
String idnum = c.getString(TAG_IDNUM);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_FNAME,fname);
persons.put(TAG_LNAME,lname);
persons.put(TAG_IDNUM,idnum);
personList.add(persons);
}
final ListAdapter adapter = new SimpleAdapter(
SearchActivity.this, personList, R.layout.list_activity,
new String[]{TAG_ID,TAG_FNAME,TAG_LNAME,TAG_IDNUM},
new int[]{R.id.id, R.id.textViewFname, R.id.textViewLname,R.id.textViewIdnum}
);
list.setAdapter(adapter);
//alert dialog
//Create onclick listener class
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* Alert Dialog Code Start*/
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Alert id: "+TAG_IDNUM); //Set Alert dialog title here
alert.setMessage("Enter Your Comment here"); //Message here
// Set an EditText view to get user input
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// convert the input to a string and show in a toast.
String srt = input.getEditableText().toString();
Toast.makeText(SearchActivity.this,"Commenting Added", Toast.LENGTH_LONG).show();
} // End of onClick(DialogInterface dialog, int whichButton)
}); //End of alert.setPositiveButton
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
Toast.makeText(SearchActivity.this,"Commenting cancled",Toast.LENGTH_LONG).show();
dialog.cancel();
}
}); //End of alert.setNegativeButton
AlertDialog alertDialog = alert.create();
alertDialog.show();
/* Alert Dialog Code End*/
// End of onClick(View v)
}
});}
catch (JSONException e){}
}
So i managed to come up with the following but the variables am sending to the php file don't seem to be getting there.
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* Alert Dialog Code Start*/
AlertDialog.Builder alert = new AlertDialog.Builder(context);
final String id_number =((TextView)view.findViewById(R.id.textViewIdnum)).getText().toString();
alert.setTitle("Alert id: "+id_number); //Set Alert dialog title here
alert.setMessage("Enter Your Comment here"); //Message here
// Set an EditText view to get user input
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// convert the input to a string and show in a toast.
String reason = input.getEditableText().toString();
Toast.makeText(SearchActivity.this,"Commenting...", Toast.LENGTH_LONG).show();
final String reason_key = input.getText().toString().trim();
//final String id_number_key = textview;
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.RESULT_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(SearchActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SearchActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
});
Map<String,String> params = new HashMap<String, String>();
params.put(TAG_IDNUM,id_number);
params.put(reason_key,reason);
RequestQueue requestQueue = Volley.newRequestQueue(SearchActivity.this);
requestQueue.add(stringRequest);
} // End of onClick(DialogInterface dialog, int whichButton)
}); //End of alert.setPositiveButton
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
Toast.makeText(SearchActivity.this,"Commenting cancled",Toast.LENGTH_LONG).show();
dialog.cancel();
}
}); //End of alert.setNegativeButton
AlertDialog alertDialog = alert.create();
alertDialog.show();
/* Alert Dialog Code End*/
// End of onClick(View v)
}
});}
catch (JSONException e){}
}

Is it possible to use a same onClick for both AlertDialog and View?

My activity has an accept button witch sends intent to parent through onClickOkayButton method. I have overriden the onBackPressed() method so that it shows an AlertDialog asking the user if he really wishes to leave or if he wants to save the preferences, witch does the exact same action as the accept button.
Is it possible to merge both onClick methods into one so I don't need to copy/paste in case of editing one of those, even if both methods use different parameters?
public void onClickOkayButton(View view) {
// THIS IS THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(this, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBackPressed() {
if (adapter.getCount() != 0) {
showAlertDialog();
} else {
super.onBackPressed();
}
}
public void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.surveyadd_warn_back_message);
builder.setNegativeButton(R.string.surveyadd_warn_back_save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// THIS WAS COPIED FROM THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
});
// UP TO HERE
builder.setPositiveButton(R.string.surveyadd_warn_back_erase, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
You won't be able to use the same callback methods because they are from different interfaces.
What you should do is move all the common code into a third method and just call it from both click handlers like below:
private void setResultAndFinish() {
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!TextUtils.isEmpty(title)) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
And then the handlers:
public void onClickOkayButton(View view) {
setResultAndFinish();
}
#Override
public void onClick(DialogInterface dialog, int which) {
setResultAndFinish();
}

Categories