This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I am successfully create an onClick based on adapter getPosition.
the common way is that making onClicklistener at onBindViewHolder
#Override
public void onBindViewHolder (final MyViewHolder holder, final int position){
holder.mId.setText(itemList.get(position).getId());
holder.itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
final Intent intent;
if (position == 0) {
intent = new Intent(context, MyActivity.class);
} else if (position == 1) {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
But, what I am trying achieve is, the parameter that I'm gonna use to go to next Activity is the getId.
I've modified my code to this.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String idNumber = itemList.get(position).getId();
final Intent intent;
if (idNumber == "7") {
intent = new Intent(context, MyActivity.class);
} else if (idNumber == "10") {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
Log.e("YOUR ID NUMBER IS", idNumber);
Toast.makeText(context, "Recycle Click" + idNumber,Toast.LENGTH_SHORT).show();
}
});
When I run it, and when I click on any item, it keeps going to MyActivity3
But, on the Log and toast says, the right idNumber that I click.
Sometimes if(StringValue == StringValue1) does not gives expected result. try replacing
if (idNumber == "7")
with
if (idNumber.equals("7"))
so your code will be like,
if (idNumber.equals("7")) {
intent = new Intent(context, MyActivity.class);
} else if (idNumber.equals("10")) {
intent = new Intent(context, MyActivity2.class);
} else {
intent = new Intent(context, MyActivity3.class);
}
context.startActivity(intent);
Happy Coding.
Related
here Heloo guys im working on an app with auto suggestion and search the problem is when i type a letter in the searchfield and the suggestion changes still the items in the search launches the same intents according to ther position and not the actual intended synonym class how can i set specific intent on the search entries such that even if the position changes still the same activity is launched by a diffrent search entry whose name corresponds with the search result
first screenshot
second screen
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the search menu action bar.
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_search, menu);
// Get the search menu.
MenuItem searchMenu = menu.findItem(R.id.app_bar_menu_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchMenu);
mSearchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchAutoComplete.setThreshold(0);
mSearchAutoComplete.setBackgroundColor(getResources().getColor(R.color.tabcolor));
mSearchAutoComplete.setTextColor(Color.BLACK);
mSearchAutoComplete.setDropDownBackgroundResource(android.R.color.darker_gray);
/* Create a new ArrayAdapter and add data to search auto complete object.
how can i set each word when onclicked to open the corresponding class no matter the position
*/
String dataArr[] = {"Kiambu county", "Kisumu county", "Kitui county", "Laikipia county", "Lamu county", "Meru county", "Mombasa county", "Muranga county", "Nairobi county", "Nakuru county", "Narok county", "kajiado county", "Uansingishu county", "Makueni County", "Machakos county"};
ArrayAdapter<String> newsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dataArr);
mSearchAutoComplete.setAdapter(newsAdapter);
// Listen to search view item on click event.
mSearchAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
Intent intent = new Intent(County.this, Kiambu.class);
startActivity(intent);
}
if (i == 1) {
Intent intent = new Intent(County.this, Kisumu.class);
startActivity(intent);
}
if (i == 2) {
Intent intent = new Intent(County.this, Kitui.class);
startActivity(intent);
}
if (i == 3) {
Intent intent = new Intent(County.this, Laikipia.class);
startActivity(intent);
}
if (i == 4) {
Intent intent = new Intent(County.this, Lamu.class);
startActivity(intent);
}
if (i == 5) {
Intent intent = new Intent(County.this, Meru.class);
startActivity(intent);
}
if (i == 6) {
Intent intent = new Intent(County.this, Mombasa.class);
startActivity(intent);
}
if (i == 7) {
Intent intent = new Intent(County.this, Muranga.class);
startActivity(intent);
}
if (i == 8) {
Intent intent = new Intent(County.this, Nairobi.class);
startActivity(intent);
}
if (i == 9) {
Intent intent = new Intent(County.this, Nakuru.class);
startActivity(intent);
}
if (i == 10) {
Intent intent = new Intent(County.this, Narok.class);
startActivity(intent);
}
if (i == 11) {
Intent intent = new Intent(County.this, Kajiado.class);
startActivity(intent);
}
if (i == 12) {
Intent intent = new Intent(County.this, Singishu .class);
startActivity(intent);
}
if (i == 13) {
Intent intent = new Intent(County.this, Makueni.class);
startActivity(intent);
}
if (i == 14) {
Intent intent = new Intent(County.this, Machakos.class);
startActivity(intent);
}
}
});
return super.onCreateOptionsMenu(menu);
}
help out im really stuck
The problem is you have added condition base on i which gives the current position of adapter item clicked.
So, when you search something, it filters the data and displays it accordingly, and when you click on say 1st item, it will always open Kiambu class.
You need to update your condition inside your onItemClick method to something like:
if (newsadapter.getItem(i).equals(dataArr[0])) {
Intent intent = new Intent(County.this, Kiambu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[1])) {
Intent intent = new Intent(County.this, Kisumu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[2])) {
Intent intent = new Intent(County.this, Kitui.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[3])) {
Intent intent = new Intent(County.this, Laikipia.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[4])) {
Intent intent = new Intent(County.this, Lamu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[5])) {
Intent intent = new Intent(County.this, Meru.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[6])) {
Intent intent = new Intent(County.this, Mombasa.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[7])) {
Intent intent = new Intent(County.this, Muranga.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[8])) {
Intent intent = new Intent(County.this, Nairobi.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[9])) {
Intent intent = new Intent(County.this, Nakuru.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[10])) {
Intent intent = new Intent(County.this, Narok.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[11])) {
Intent intent = new Intent(County.this, Kajiado.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[12])) {
Intent intent = new Intent(County.this, Singishu.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[13])) {
Intent intent = new Intent(County.this, Makueni.class);
startActivity(intent);
} else if (newsadapter.getItem(i).equals(dataArr[14])) {
Intent intent = new Intent(County.this, Machakos.class);
startActivity(intent);
}
im kinda new to programming and would like to make some sort of like a login and register page. so from my login page i click register and it would go to the register page and i want the username/password i get from the register page to be used in the previous login page to login into the app. But i cant seem to set the username/password using the result. Only able to set the textview. pls help heres the code
public class MainActivity extends AppCompatActivity {
private EditText Name;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText)findViewById(R.id.etName);
Password = (EditText)findViewById(R.id.etPass);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button)findViewById(R.id.btnLogin);
Info.setText("No Of Attempts Remaining: 5");
Login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validate(Name.getText().toString(),Password.getText().toString());
}
});
}
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
else{
counter--;
Info.setText("No Of Attempts Remaining: " + String.valueOf(counter));
if(counter == 0){
Login.setEnabled(false);
}
}
}
public void facebooklogin(View myview){
Intent intent = new Intent(this, MenuActivity.class);
startActivity(intent);
}
public void register(View myview) {
Intent i = new Intent(this, RegisterActivity .class);
startActivityForResult(i, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
String reginame = data.getStringExtra("NAME");
String regipass = data.getStringExtra("PASS");
Name.setText("" + reginame);
Password.setText("" + regipass);
}
}
}
How do i set the
private void validate(String userName, String userPassword){
if(userName.equals("") && userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
to be equal to the onActivityResult reginame and regipass
your condition is wrong it should be like this:-
private void validate(String userName, String userPassword){
if(!userName.equals("") && !userPassword.equals("")){
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
startActivity(intent);
}
First Activity.
Send information
Intent send = new Intent(MainActivity.this, Main2Activity.class);
send.putExtra("login",editText.getText());
send.putExtra("password",editText1.getText());
startActivity(send);
Second Activity.
Get Information
if(getIntent()!=null){
Intent intent = getIntent();
editText.setText(intent.getStringExtra("login")) ;
editText1.setText(intent.getStringExtra("password")) ;
}
So I'm new to programming. I have a string array named values that has about 150 strings in it. Instead of using a ton of if statements I wan't to to use a for loop that each time it cycles through the loop increments to the next element in the array. I'm sure it's a super simple fix but I just can't solve it. Thanks for any advice!
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String route = values[position];
int i;
for (i=0; i < values.length;i++) {
if (route.equals(values[0])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[0]);
startActivity(intent);
}
values++;
}
/*if (route.equals(values[0])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[1]);
startActivity(intent);
}
if (route.equals("Main Wall")) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", "Map of Main Wall");
startActivity(intent);
}
if (route.equals("1. Shark Bait - 5.9")) {
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Shark Bait");
startActivity(intent);
}
*/
}
Inside of the loop, replace the hard coded 0's with "i". This will allow your code to be ran for each iteration of the loop. For example, the i will be replaced with 0, then 1, then 2, etc.
for (int i=0; i<values.length; i++) {
if (route.equals(values[i])) {
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[i]);
startActivity(intent);
}
}
Also, there is no need to add a counter for values at the end, since it is handled by the i++ in the for loop. Hope that helps!
Looks like you can use switches...if you need both int and string comparisons you can use two switches to do that.
String route = values[position];
switch(position) {
case 0:
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", routeDetail[0]);
startActivity(intent);
return;
case 1:
// Do stuff
return;
}
switch(route) {
case "Main Wall":
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Map of Main Wall");
startActivity(intent);
return;
case "Shark Bait":
Intent intent = new Intent(MainActivity.this, RouteDetails.class);
intent.putExtra("route", "Shark Bait");
startActivity(intent);
return;
}
in My Main Activity I am pasing data using the data using intent using the putextra()
method
private void editHandler()
{
//send the details to another form
if (itemID > 0)
{
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtra("bundle", values);
startActivity(intent);
}
in the next Activity I am receiving that intent
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addor_edit);
todoNote = (EditText)findViewById(R.id.txt_todoNote);
todoDate = (EditText) findViewById(R.id.txt_dateTODO);
todoTime = (EditText) findViewById(R.id.txt_timeTODO);
todoalarm =(ToggleButton) findViewById(R.id.toggle_alarm);
alarmEnable = (ImageView) findViewById(R.id.img_alarmEnable);
canceltodo = (Button) findViewById(R.id.btn_cancelTODO);
maketodo = (Button) findViewById(R.id.btn_makeTODO);
//Receiving intent
Bundle bundle = getIntent().getBundleExtra("bundle");
getValuesForEdit(bundle);
todoalarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(todoalarm.getText().equals("ON"))
{
alarmEnable.setImageResource(R.drawable.dark_alarm);
alarm = 1;
}
else
{
alarmEnable.setImageResource(0);
alarm = 0;
}
}
});
maketodo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addnewTODO();
}
});
}
private void getValuesForEdit(Bundle bundle)
{
// if the Edit Button is pressed get all values from listView
ID = bundle.getInt("id");
todoNote.setText(bundle.getString("text"));
todoDate.setText(bundle.getString("date"));
todoTime.setText(bundle.getString("time"));
alarm = bundle.getInt("alarm");
if (alarm == 1)
{
todoalarm.setText("ON");
alarmEnable.setImageResource(R.drawable.dark_alarm);
}
}
and The application crashes
am I doing wrong with intents?? which is the right way to pass data between
activities?? suggestions and advises are needed...
thanks!!
Intent intent = new Intent(this,AddorEdit.class);
intent.putExtra("text",singleItem.getText());
intent.putExtra("date", singleItem.getDate());
intent.putExtra("time", singleItem.getDate());
intent.putExtra("id", singleItem.getItemID());
intent.putExtra("alarm", singleItem.getAlarm());
startActivity(intent);
//While get this data:
ID = getIntent().getIntExtra("id");
todoNote.setText(getIntent().getStringExtra("text"));
todoDate.setText(getIntent().getStringExtra("date"));
todoTime.setText(getIntent().getStringExtra("time"));
alarm = getIntent().getIntExtra;
Try this
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtras(values);
startActivity(intent);
and
Bundle bndl = this.getIntent().getExtras()
Is there a way to call multiple strings with just one intent, like in a webview activity and using the if / else clause like this:
public void openGo(View v) {
TextView display = (TextView) findViewById(R.id.display);
String check = display.getText().toString();
if (check.contentEquals("1")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (check.contentEquals("10")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page2.html");
startActivity(i);
If string 1, and 12 and 16 have the same webview page, how do you link them together? Is this possible like below? NOTICE THE BOLD TEXTS.
public void openGo(View v) {
TextView display = (TextView) findViewById(R.id.display);
String check = display.getText().toString();
**if (check.contentEquals("1,12,16")) {**
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (check.contentEquals("10")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/errorpage.html");
startActivity(i);
Thank you.
use
if (check.contentEquals("1") || check.contentEquals("12") || check.contentEquals("16") )
I dont think what you use work correctly