setting intent on search results - android

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);
}

Related

Why I can NOT receive extra parameters from Intent in onStartCommand? [duplicate]

Is there any way to check if an extra has been passed when starting an Activity?
I would like to do something like (on the onCreate() in the Activity):
Bundle extras = getIntent().getExtras();
String extraStr = extras.getString("extra");
if (extraStr == null) {
extraStr = "extra not set";
}
But this is throwing a java.lang.NullPointerException.
Thank you.
Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.
Example:
Intent intent = getIntent();
if (intent.hasExtra("bookUrl")) {
bookUrl = b.getString("bookUrl");
} else {
// Do something else
}
Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.
Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.
here is how IN MY CASE I solved it:
Intent intent = getIntent();
if(intent.hasExtra("nomeUsuario")){
bd = getIntent().getExtras();
if(!bd.getString("nomeUsuario").equals(null)){
nomeUsuario = bd.getString("nomeUsuario");
}
}
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
// intent is not null and your key is not null
}
I think you need to check when extras != null
Bundle extras = getIntent().getExtras();
if (extras != null) {
String extraStr = extras.getString("extra");
}else {
extraStr = "extra not set";
}
I would use this solution in your case.
String extraStr;
try {
extraStr = getIntent().getExtras().getString("extra");
} catch (NullPointerException e ) {
extraStr = "something_else";
}
Working Code
If you want to check first Intent have no extra
Intent intent = getIntent();
if (intent.getExtras() == null){
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}else {
if (intent.hasExtra("type")) {
String type = getIntent().getStringExtra("type");
switch (type){
case "showRateUsDialog":
Intent i = new Intent(Splash.this, Main.class);
i.putExtra("type", "showRateUsDialog");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
case "refer":
Intent i2 = new Intent(Splash.this, Refer.class);
i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i2);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
default:
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}
}
}
}

how to add multiple imageview dynamically

im about to make like photo album so we can upload multiple photo (like facebook, etc)
ivAttachment1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(strPhoto1 == "" && PageType.equals("NewRequestMedical")) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
photoTemp = "photo1";
}
else if(strPhoto1 == "" && !PageType.equals("NewRequestMedical"))
{
Toast.makeText(getApplicationContext(), "there is no attachment on this request",
Toast.LENGTH_SHORT).show();
}else {
Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
/* i.putExtra("photo", strPhoto1);*/
Bundle extras = new Bundle();
extras.putString("photo", strPhoto1);
extras.putString("photoNo","photo1");
extras.putString("PageTypeRequest",PageType);
i.putExtras(extras);
startActivityForResult(i,100);
//i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
}
});
ivAttachment2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(strPhoto2 == "" && PageType.equals("NewRequestMedical")) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
photoTemp = "photo2";
}
else if(strPhoto2 == "" && !PageType.equals("NewRequestMedical"))
{
Toast.makeText(getApplicationContext(), "there is no attachment on this request",
Toast.LENGTH_SHORT).show();
}
else
{
Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
/* i.putExtra("photo", strPhoto2);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);*/
Bundle extras = new Bundle();
extras.putString("photo", strPhoto2);
extras.putString("photoNo","photo2");
extras.putString("PageTypeRequest",PageType);
i.putExtras(extras);
startActivityForResult(i,100);
}
}
});
ivAttachment3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(strPhoto3 == "" && PageType.equals("NewRequestMedical")) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
photoTemp = "photo3";
}
else if(strPhoto3 == "" && !PageType.equals("NewRequestMedical"))
{
Toast.makeText(getApplicationContext(), "there is no attachment on this request",
Toast.LENGTH_SHORT).show();
}else
{
Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
Bundle extras = new Bundle();
extras.putString("photo", strPhoto3);
extras.putString("photoNo","photo3");
extras.putString("PageTypeRequest",PageType);
i.putExtras(extras);
startActivityForResult(i,100);
/*i.putExtra("photo", strPhoto3);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);*/
}
}
});
ivAttachment4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(strPhoto4 == "" && PageType.equals("NewRequestMedical")) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
photoTemp = "photo4";
}
else if(strPhoto4 == "" && !PageType.equals("NewRequestMedical"))
{
Toast.makeText(getApplicationContext(), "there is no attachment on this request",
Toast.LENGTH_SHORT).show();
}else
{
Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
Bundle extras = new Bundle();
extras.putString("photo", strPhoto4);
extras.putString("photoNo","photo4");
extras.putString("PageTypeRequest",PageType);
i.putExtras(extras);
startActivityForResult(i,100);
/* i.putExtra("photo", strPhoto4);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);*/
}
}
});
ivAttachment5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(strPhoto5 == "" && PageType.equals("NewRequestMedical")) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
photoTemp = "photo5";
}
else if(strPhoto5 == "" && !PageType.equals("NewRequestMedical"))
{
Toast.makeText(getApplicationContext(), "there is no attachment on this request",
Toast.LENGTH_SHORT).show();
}
else{
Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
Bundle extras = new Bundle();
extras.putString("photo", strPhoto5);
extras.putString("photoNo","photo5");
extras.putString("PageTypeRequest",PageType);
i.putExtras(extras);
startActivityForResult(i,100);
/* i.putExtra("photo", strPhoto5);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);*/
}
}
});
now i just make 10 imageviews and declare all. which is not dynamic. and i convert the photo to bitmap and it will be send to database.
is there any way or library that i can use?
You can have a "Add Image" button like below instead of having multiple images.
Then on "add image" button click you can hit gallery intent with startactivityforresults() for getting the images and onAcivityResult() you can create imageView and add it to container and upload the same.
public void addImageView(Context context, View container){
ImageView imgNew = new ImageView(context);
//add layout params & set image
container.addView(newImage);
// start service to upload the image
}

Java - for loop cycling through string array

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;
}

RecyclerView onClick get result from getAttribute not getPosition [duplicate]

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.

Using If Statement with 2D Array

I am working with ExpandableListView right now. I need help how to use if-statement, if the condition is in 2D array. I already wrote this code, but when I tap on every child, it always goes to Games activity. What I want is when every child is tapped, new activity (depends on what child) will be opened.
I'm new in OOP. So maybe you can help me. Thanks!
listView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if ( CHILDREN[0][0] == "Management") {
Intent intent = new Intent(Information.this, Games.class);
startActivity(intent);
}
else if ( CHILDREN[0][1] == "Accountant") {
Intent intent = new Intent(Information.this, Test.class);
startActivity(intent);
}
else if ( CHILDREN[0][2] == "Economy") {
Intent intent = new Intent(Information.this, Chat.class);
startActivity(intent);
}
else {
Intent intent = new Intent(Information.this, MainActivity.class);
startActivity(intent);
}
return true;
}
});
If it helps -- This is the string array declaration:
private String[][] CHILDREN = {
{ "Management", "Accountant", "Economy" },
{ "IAB", "Communication" , "Hospitality" },
{ "English", "Theology", "BK", "Elementary" },
{ "Machine", "Electrical", "Industrial" },
{ "Law" },
{ "Doctor" },
{ "Psychology" },
{ "Biology" },
};
You should build your if statement with ref to childPosition
like this
String selString = CHILDREN[groupPosition][childPosition];//parent.getItemAtPosition(childPosition).toString();
then
if ( selString.equals("Management"))
{
Intent intent = new Intent(Information.this, Games.class);
startActivity(intent);
}
and so on...
Try CHILDREN[0][0].equals("Management")) instead of CHILDREN[0][0] == "Management"
listView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if ( CHILDREN[groupPosition][childPosition] == "Management") {
Intent intent = new Intent(Information.this, Games.class);
startActivity(intent);
}
else if ( CHILDREN[groupPosition][childPosition] == "Accountant") {
Intent intent = new Intent(Information.this, Test.class);
startActivity(intent);
}
else if ( CHILDREN[groupPosition][childPosition] == "Economy") {
Intent intent = new Intent(Information.this, Chat.class);
startActivity(intent);
}
else {
Intent intent = new Intent(Information.this, MainActivity.class);
startActivity(intent);
}
return true;
}
});
Try this (hope, final code :) )

Categories

Resources