How to properly display a custom dialog in an adapter - java

I have a custom adapter in my app, and i launch several activities from there, one of the populated lists in the adapter takes you back to the login screen which works fine, but I decided to add a confirmation dialog before taking the user to the login screen, but I get an error saying android.content.ActivityNotFoundException: No Activity found to handle Intent { }
This is what I've implemented so far
AccountAdapter.java
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title;
ImageView icon;
MyViewHolder(View view) {
super(view);
view.setClickable(true);
view.setOnClickListener(this);
mCtx = view.getContext();
title = view.findViewById(R.id.title);
icon = view.findViewById(R.id.icon);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
switch (getAdapterPosition()){
case 0:
intent = new Intent(mCtx, SettingsActivity.class);
break;
case 1:
intent = new Intent(mCtx, ProfileActivity.class);
break;
case 2:
intent = new Intent(mCtx, ChangePasswordActivity.class);
break;
default:
CustomAlertDialog alert = new CustomAlertDialog((Activity)mCtx);//faulty code
alert.show();
break;
}
mCtx.startActivity(intent);
}
}
CustomAlertDialog.java
public class CustomAlertDialog extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
// public Button yes, no;
public TextView yes, no;
public CustomAlertDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_alert_dialog);
yes = (TextView) findViewById(R.id.btn_yes);
// no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
// no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
break;
default:
break;
}
dismiss();
}
}
AccountFragment.java (where I populate the account adapter)
private void prepareAccountData() {
// if (mAdapter == null) {
// return;
// }
AccountModel options = new AccountModel("Settings", R.drawable.ic_action_settings);
accountList.add(options);
options = new AccountModel("Update Profile", R.drawable.ic_social_person_outline);
accountList.add(options);
options = new AccountModel("Change Password", R.drawable.ic_hardware_security);
accountList.add(options);
options = new AccountModel("Logout", R.drawable.ic_action_exit_to_app);
accountList.add(options);
mAdapter.notifyDataSetChanged();
}
and this is the code I use for direct logout which works aswell
((Activity)mCtx).finish();
SharedPreferences sharedPreferences = mCtx.getSharedPreferences("key", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
intent = new Intent(mCtx, LoginActivity.class);
How can I implement the custom dialog in my adapter?

Your code has bug you don't init intent in case show dialog, you should move logic into onClick in your Dialog, move this mCtx.startActivity(intent); into case
#Override
public void onClick(View v) {
switch (getAdapterPosition()){
case 0:
mCtx.startActivity(new Intent(mCtx, SettingsActivity.class));
break;
case 1:
mCtx.startActivity(new Intent(mCtx, ProfileActivity.class));
break;
case 2:
mCtx.startActivity(new Intent(mCtx, ChangePasswordActivity.class));
break;
default:
CustomAlertDialog alert = new CustomAlertDialog((Activity)mCtx)
alert.show();
break;
}
}

Related

Cannot get the secondSpinner to refresh itself

Can someone look at my code its really simple created just to practice my skills. But the language spinner works but the gender spinner is buggy. It will work once i change the language then change the gender and then go back to the gender i originally wanted. Code is shown. Thanks. Have tried putting onItemSelected() for spinnerGen, it does not solve the issue. Researching about this since last night cant find any solution
public class MainActivity extends AppCompatActivity {
private Button btnGo, btnAbout;
private Spinner spinner, spinnerGen;
private EditText addName;
private ArrayList<String> options;
private ArrayList<String> optionsGen;
private String allOption = "";
private String genOption = "";
private String genOptionSec = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPage();
options = new ArrayList<>();
options.add("English");
options.add("Bengali");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, options);
spinner.setAdapter(adapter);
adapter.setNotifyOnChange(true);
adapter.notifyDataSetChanged();
optionsGen = new ArrayList<>();
optionsGen.add("Male");
optionsGen.add("Female");
ArrayAdapter<String> adapterGen = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, optionsGen);
spinnerGen.setAdapter(adapterGen);
adapterGen.setNotifyOnChange(true);
adapterGen.notifyDataSetChanged();
spinnerMethod();
}
private void initPage() {
btnGo = findViewById(R.id.btnGo);
btnAbout = findViewById(R.id.btnAbout);
spinner = findViewById(R.id.spinner);
spinnerGen = findViewById(R.id.spinnerGen);
addName = findViewById(R.id.addName);
}
public void spinnerMethod() {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
allOption = spinner.getSelectedItem().toString();
switch (allOption) {
case "English" :
newMethodEnglish();
break;
case "Bengali" :
newMethodBengali();
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void newMethodEnglish() {
genOption = spinnerGen.getSelectedItem().toString();
switch (genOption) {
case "Male":
onClickEngMale(); //In these method Im just navigating to another activity
break;
case "Female":
OnClickEngFemale();
break;
default:
break;
}
}
public void OnClickEngFemale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String femaleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, EngFemale.class);
intent.putExtra("DataFemale", femaleData);
startActivity(intent);
}
});
}
public void onClickEngMale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String maleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, EngMale.class);
intent.putExtra("Data", maleData);
startActivity(intent);
}
});
}
public void newMethodBengali() {
genOptionSec = spinnerGen.getSelectedItem().toString();
switch (genOptionSec){
case "Male":
onClickBanMale();
break;
case "Female":
onClickBanFemale();
default:
break;
}
}
public void onClickBanMale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String maleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, BanMale.class);
intent.putExtra("DataBan", maleData);
startActivity(intent);
}
});
}
public void onClickBanFemale() {
btnGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String femaleData = addName.getText().toString();
Intent intent = new Intent(MainActivity.this, BanFemale.class);
intent.putExtra("BanFemale", femaleData);
startActivity(intent);
}
});
}
}

saving and getting spinner items with shared preferences

I have a problem with saving spinner items to shared preferences. By problem I mean I really don't know how to do it. It would be nice if someone could actually explain it to me if someone knows.Thanks in advance :)
so I have set my button 1 to open a dialog with spinner and in spinner i have 3 items (declared in strings.xml). I should use shared preferences to set spinner to item 1 :"-" by default, and if user selects item 2 "lang1" or "lang2" it should be saved in app. So that when i open app next time its on "lang1" or "lang2" depends on selection.
Everything else is working fine.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
int spinnerPosition;
public Button btnTrans, btnSelect,btnInput,btnCheck;
public TextView tv_title, tv_message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect= (Button) findViewById(R.id.btnLanguage);
btnInput = (Button) findViewById(R.id.btnInput);
btnTrans = (Button) findViewById(R.id.btnTranslate);
btnTrans.setOnClickListener((View.OnClickListener) this);
btnInput.setOnClickListener(this);
btnSelect.setOnClickListener(this);
Spinner lang_spinner = (Spinner) findViewById(R.id.lang_spinner);
}
public void onClick(View v)
{
int id=v.getId();
switch (id){
case R.id.btnLanguage:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.language_dialog, null);
mBuilder.setTitle("select language");
final Spinner mSpinner = (Spinner) mView.findViewById(R.id.lang_spinner);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lang_array));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SharedPreferences.Editor editor = sharedpreferences.edit();
//SELECTING LANGUAGE switch case (translate btn)-
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Object item = adapterView.getItemAtPosition(position);
if (item != null) {
switch (position) {
case 0:
item = "-";
btnTrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.nista, Toast.LENGTH_LONG).show();
}
});
break;
case 1:
item = "lang1";
btnTrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.hrvatski, Toast.LENGTH_LONG).show();
}
});
break;
case 2:
item = "lang2";
btnTrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.english, Toast.LENGTH_LONG).show();
}
});
break;
}
}
//on selected save
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
//nothing selected "-"
}
});
I am rep is less then 50 so I am posting link here for shared preference
https://www.google.com/amp/s/www.geeksforgeeks.org/shared-preferences-in-android-with-examples/amp/
Get your Lang from spinner and store value in share preference.

How to display both data in Edittext without losing one of them?

I have two Edittexts in MainActivity: etFrom and etTo.
When clicked etFrom goes to ListFrom activity and etTo to ListTo activity.ListFrom and ListTo activities contain Listview and after choosing an item it comes back to Main activity and displays results in edittexts. I use MainActivity.putExtra("To",v); and etTo.setText(getIntent().getStringExtra("To")); to get data from an activity.
After clicking on first edittext, choosing an item from Listview and coming back to Main Activity, when I do the same for second edittext the first edittext becomes empty. And the same when I do first one, second one becomes empty.First Second
ListTo Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_to);
etSearchTo=(EditText) findViewById(R.id.etSearchTo);
listViewTo=(ListView) findViewById(R.id.listViewTo);
initList();
listViewTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
Intent intent=new Intent(ListTo.this,SearchRide.class);
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
intent.putExtra("To",v);
startActivity(intent);
}
});
ListFrom Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_from);
listView=(ListView) findViewById(R.id.listviewFrom);
editText=(EditText) findViewById(R.id.etsearchFrom);
initList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
Intent intent=new Intent(ListFrom.this,SearchRide.class);
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
intent.putExtra("From",v);
startActivity(intent);
}
});
SearchRide as Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_ride);
etDate=(EditText) findViewById(R.id.etDate);
TextView tFrom=(TextView) findViewById(R.id.tFrom);
TextView tTo=(TextView) findViewById(R.id.tTo);
etFrom=(EditText) findViewById(R.id.etFrom);
etTo=(EditText) findViewById(R.id.etTo);
Button bSearch=(Button) findViewById(R.id.bSearch);
ImageView iplussign=(ImageView) findViewById(R.id.iplussign);
ImageView iminussign=(ImageView) findViewById(R.id.iminussign);
final TextView tNumber=(TextView) findViewById(R.id.tNumber);
etTo.setText(getIntent().getStringExtra("To"));
tNumber.setText(String.valueOf(p));
iplussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p<=9) {
p++;
tNumber.setText(String.valueOf(p));
}
}
});
iminussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p>=2) {
p--;
tNumber.setText(String.valueOf(p));
}
}
});
etFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ListFrom=new Intent(SearchRide.this,ListFrom.class);
startActivity(ListFrom);
}
});
etFrom.setText(getIntent().getStringExtra("From"));
etTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ToList =new Intent(SearchRide.this,ListTo.class);
startActivity(ToList);
}
});
etTo.setText(getIntent().getStringExtra("To"));
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog dialog= new DatePickerDialog(SearchRide.this,R.style.MyDatePickerStyle,listener,calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH),calendar.get(Calendar.YEAR));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
use startActivityForResult() to start ListFrom and ListTo Activities and the receive the result from these activities in OnActivityResult() method.
SearchRide as Main Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_ride);
etDate=(EditText) findViewById(R.id.etDate);
TextView tFrom=(TextView) findViewById(R.id.tFrom);
TextView tTo=(TextView) findViewById(R.id.tTo);
etFrom=(EditText) findViewById(R.id.etFrom);
etTo=(EditText) findViewById(R.id.etTo);
Button bSearch=(Button) findViewById(R.id.bSearch);
ImageView iplussign=(ImageView) findViewById(R.id.iplussign);
ImageView iminussign=(ImageView) findViewById(R.id.iminussign);
final TextView tNumber=(TextView) findViewById(R.id.tNumber);
tNumber.setText(String.valueOf(p));
iplussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p<=9) {
p++;
tNumber.setText(String.valueOf(p));
}
}
});
iminussign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(p>=2) {
p--;
tNumber.setText(String.valueOf(p));
}
}
});
etFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ListFrom = new Intent(SearchRide.this,ListFrom.class);
startActivityForResult(ListFrom,0);
}
});
etTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ToList = new Intent(SearchRide.this,ListTo.class);
startActivityForResult(ToList,1);
}
});
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerDialog dialog= new DatePickerDialog(SearchRide.this,R.style.MyDatePickerStyle,listener,calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH),calendar.get(Calendar.YEAR));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
dialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if(resultCode == Activity.RESULT_OK){
etFrom.setText(data.getStringExtra("From"));
}
} else if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
etTo.setText(data.getStringExtra("To"));
}
}}
ListFrom code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_from);
listView=(ListView) findViewById(R.id.listviewFrom);
editText=(EditText) findViewById(R.id.etsearchFrom);
initList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
Intent intent = new Intent();
intent.putExtra("From",v);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
ListTo code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_to);
etSearchTo=(EditText) findViewById(R.id.etSearchTo);
listViewTo=(ListView) findViewById(R.id.listViewTo);
initList();
listViewTo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String v="";
switch (position) {
case 0:
v = items[0];
break;
case 1:
v = items[1];
break;
case 2:
v = items[2];
break;
case 3:
v = items[3];
break;
case 4:
v = items[4];
break;
case 5:
v = items[5];
break;
}
Intent intent = new Intent();
intent.putExtra("To",v);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
Here is what you can do. Put both from and to values as extra when starting an activity and return them both as result. This way you will be able to remember it. I am assuming you just do not want to use static variables.
// in ListTo activity
intent.putExtra("To",v);
intent.putExtra("From", getIntent().getStringExtra("From"));
// in ListFrom activity
intent.putExtra("From",v);
intent.putExtra("To", getIntent().getStringExtra("To"));
// in Main activity
etTo.setText(getIntent().getStringExtra("To"));
etFrom.setText(getIntent().getStringExtra("From"));
// when starting ListFrom activity
Intent ListFrom=new Intent(SearchRide.this,ListFrom.class);
intent.putExtra("From", getIntent().getStringExtra("From"));
intent.putExtra("To", getIntent().getStringExtra("To"));
startActivity(ListFrom);
// when starting ListTo activity
Intent ToList =new Intent(SearchRide.this,ListTo.class);
intent.putExtra("From", getIntent().getStringExtra("From"));
intent.putExtra("To", getIntent().getStringExtra("To"));
startActivity(ToList);
PS: I have not tested the above code. You can experience null pointer errors, but it should give you the general idea.

Android putExtra push data with same key from different buttons

I have a code that uses putextra method to push data from activity to another, I want to push different values using the same key
code:
String int_value = "int_value";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondScreen.class);
intent.putExtra(int_value , 0);
startActivity(intent);
}
});
Button btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondScreen.class);
intent.putExtra(int_value , 1);
startActivity(intent);
}
});
And in the next activity:
int value;
String int_value = "int_value";
View myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);
Intent intent = getIntent();
int temp = intent.getIntExtra(int_value, value);
myLayout = findViewById(R.id.myLayout);
switch (value){
case 0:
myLayout.setBackgroundResource(R.drawable.a);
Log.e("VALUE" , String.valueOf(value));
break;
case 1:
myLayout.setBackgroundResource(R.drawable.b);
Log.e("VALUE" , String.valueOf(value));
break;
}
But the background always changes to a.jpg although I passed 0 & 1 (or a least I thought I did...)
what is the problem here?
just change value in switch to temp
switch ( temp){
case 0:
myLayout.setBackgroundResource(R.drawable.a);
Log.e("VALUE" , String.valueOf(value));
break;
case 1:
myLayout.setBackgroundResource(R.drawable.b);
Log.e("VALUE" , String.valueOf(value));
break;
}
because you are saving the value inside temp
int temp = intent.getIntExtra(int_value, value);

Passing the checkbox data when button is clicked to another activity

So I have this code and it is working. My problem is that how can I pass and show the result in new activity. I set the onclick of the 3 checkbox with selectItinerary
public class topPawikan extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toppawikan);
final_text = (TextView)findViewById(R.id.textView);
final_text.setEnabled(false);
}
public void selectItinerary(View view){
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId())
{
case R.id.checkBox1:
if (checked)
{selection.add("Maglibot");}
else
{selection.remove("Maglibot");}
break;
case R.id.checkBox2:
if (checked)
{selection.add("Kumain");}
else
{selection.remove("Kumain");}
break;
case R.id.checkBox3:
if (checked)
{selection.add("Umupo");}
else
{selection.remove("Umupo");}
break;
}
}
//button
public void finalSelection(View view){
String final_itinerary = "";
for (String Selections : selection)
{
final_itinerary = final_itinerary + Selections + "\n";
}
final_text.setText(final_itinerary);
final_text.setEnabled(true);
}
}
Put params into the new Intent. Start new activity on button click.
Intent intent = new Intent(topPawikan.this, SecondActivity.class);
Bundle b = new Bundle();
b.putString("selection", final_text.getText().toString());
intent.putExtras(b);
startActivity(intent);
Then get the params in your new Activity:
Bundle b = getIntent().getExtras();
String selection = b.getString("selection");

Categories