Using If Statement with 2D Array - android

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 :) )

Related

setting intent on search results

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

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.

instantiateItem is calling 3 times and button is not clickable

instantiateItem is calling 3 times and buttons are not clickable
#Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
inflater1 = (LayoutInflater) context1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater1.inflate(R.layout.pagerlayout, container,
false);
Log.v("", "CHecking foregroung3");
((ScrollView)itemView. findViewById(R.id.scrollview)).scrollTo(0, 0);
articleTitle_textview = (TextView)itemView.findViewById(R.id.articletitle);
postingTime_textview = (TextView)itemView.findViewById(R.id.postingtime);
articleContent_webview = (WebView)itemView.findViewById(R.id.webview);
csSliderShow=(RelativeLayout)itemView.findViewById(R.id.slide);
countTxt = (TextView)itemView.findViewById(R.id.Count_textView);
previousTxt = (TextView)itemView.findViewById(R.id.prev_textview);
nextText = (TextView)itemView.findViewById(R.id.next_textview);
tab_layout_parent = (RelativeLayout)itemView.findViewById(R.id.tab_layout_parent);
tab_layout = (LinearLayout)itemView.findViewById(R.id.tab_layout);
relatedTxt=(TextView)itemView.findViewById(R.id.related);
relatedArticlesLinearLayoutGridView = (LinearLayout)itemView.findViewById(R.id.linear_related_articles_gridview);
gridView = new ExpandableHeightGridView(ArticleViewActivity.this);
articleContent_webview.invalidate();
//WebView wv = (WebView) findViewById(R.id.webview);
articleContent_webview.getSettings().setBuiltInZoomControls(false);
articleContent_webview.getSettings().setJavaScriptEnabled(true);
try{
/*if (indexofArticle >= 0)
{
if(relatedArticlesLinearLayoutGridView.getChildAt(0) != null)
{
int movement = relatedArticlesLinearLayoutGridView.getChildAt(0).getWidth()* indexofArticle;
//mHoriArticleViewList.scrollTo(movement, 0);
}
}*/
articleTitle_textview.setText(fetchmodel.mTitle);
postingTime_textview.setText(fetchmodel.mPubDate);
String s="<head><style><meta name='viewport' content='target-densityDpi=device-dpi' body{font-size:17px;}/></style></head>";
String addwidth = fetchmodel.mHtmlText;
//Log.v("","Checking Text Before:"+fetchmodel.mHtmlText);
String addwidth1 =addwidth.replace("w=320&q=", "w="+width+"&q=");
articleContent_webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.v("URL : ", url);
if(url.contains("http:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
});
articleContent_webview.getSettings().setJavaScriptEnabled(true);
articleContent_webview.loadDataWithBaseURL("", s+addwidth1, "text/html", "utf8", "");
//Log.v("","Checking Text After:"+addwidth1);
if(fetchmodel.PhotoFeatureList.size() > 0)
{
csSliderShow.setVisibility(View.VISIBLE);
articleTitle_textview.setVisibility(View.VISIBLE);
postingTime_textview.setVisibility(View.VISIBLE);
csSliderShow.invalidate();
int valuimg=imagNum+1;
countTxt.setText("Slide "+valuimg+" of "+fetchmodel.PhotoFeatureList.size());
String image_url = fetchmodel.PhotoFeatureList.get(imagNum).imgUrl;
String image_url1 = image_url.replace("w=100", "w=300");
csImageLoader.DisplayImage(image_url1, R.drawable.loading, csImageSlider, 0);
titleTxt.setText(fetchmodel.PhotoFeatureList.get(imagNum).subTitle);
headlineTxt.setText(fetchmodel.PhotoFeatureList.get(imagNum).headline);
}
else
{
csSliderShow.setVisibility(View.GONE);
articleTitle_textview.setVisibility(View.GONE);
postingTime_textview.setVisibility(View.GONE);
}
try{
if (fetchmodel.sectionContainerList.size()>0 ) {
Log.v("","CHECKING TABS:");
tab_layout_parent.setVisibility(View.VISIBLE);
tab1 = (Button)itemView.findViewById(R.id.tab1);
tab2 = (Button)itemView.findViewById(R.id.tab2);
tab3 = (Button)itemView.findViewById(R.id.tab3);
imgPoster = (ImageView)itemView.findViewById(R.id.imgPoster0);
TextView profile =(TextView)itemView.findViewById(R.id.profile0);
TextView photos =(TextView)itemView.findViewById(R.id.photos0);
TextView videos =(TextView)itemView.findViewById(R.id.videos0);
countTab = 100;
imgLoader.DisplayImage(fetchmodel.sectionContainerList.get(0).minSectionImgURL, R.drawable.loading, imgPoster, 7);
if (fetchmodel.sectionContainerList.size() == 1) {
tab2.setVisibility(View.GONE);
tab3.setVisibility(View.GONE);
tab1.setText(fetchmodel.sectionContainerList.get(0).minSectionTitle);
imgPoster.setVisibility(View.VISIBLE);
profile.setVisibility(View.VISIBLE);
photos.setVisibility(View.VISIBLE);
videos.setVisibility(View.VISIBLE);
}else if (fetchmodel.sectionContainerList.size() == 2) {
tab3.setVisibility(View.GONE);
tab1.setText(fetchmodel.sectionContainerList.get(0).minSectionTitle);
tab2.setText(fetchmodel.sectionContainerList.get(1).minSectionTitle);
imgPoster.setVisibility(View.VISIBLE);
profile.setVisibility(View.VISIBLE);
photos.setVisibility(View.VISIBLE);
videos.setVisibility(View.VISIBLE);
}else if(fetchmodel.sectionContainerList.size() >= 3){
Log.v("","CHECKING TABS:3");
tab1.setText(fetchmodel.sectionContainerList.get(0).minSectionTitle);
tab2.setText(fetchmodel.sectionContainerList.get(1).minSectionTitle);
tab3.setText(fetchmodel.sectionContainerList.get(2).minSectionTitle);
imgPoster.setVisibility(View.VISIBLE);
profile.setVisibility(View.VISIBLE);
photos.setVisibility(View.VISIBLE);
videos.setVisibility(View.VISIBLE);
imgLoader.DisplayImage(fetchmodel.sectionContainerList.get(0).minSectionImgURL, R.drawable.loading, imgPoster, 7);
}
tab1.setTextColor(Color.RED);
tab2.setTextColor(Color.BLACK);
tab3.setTextColor(Color.BLACK);
tab2.setBackgroundColor(getResources().getColor(R.color.darkgreen));
tab3.setBackgroundColor(Color.GRAY);
tab1.setFocusable(false);
tab2.setFocusable(false);
tab3.setFocusable(false);
tab1.setBackgroundDrawable(null);
tab2.setBackgroundResource(R.drawable.back_button);
tab3.setBackgroundResource(R.drawable.back_button);
tab1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
tab1.setTextColor(Color.RED);
tab2.setTextColor(Color.BLACK);
tab3.setTextColor(Color.BLACK);
tab2.setBackgroundColor(getResources().getColor(R.color.darkgreen));
tab3.setBackgroundColor(Color.GRAY);
tab1.setBackgroundDrawable(null);
tab2.setBackgroundResource(R.drawable.back_button);
tab3.setBackgroundResource(R.drawable.back_button);
countTab = 100;
imgLoader.DisplayImage(fetchmodel.sectionContainerList.get(0).minSectionImgURL, R.drawable.loading, imgPoster, 7);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
tab2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
tab1.setTextColor(Color.BLACK);
tab2.setTextColor(Color.RED);
tab3.setTextColor(Color.BLACK);
tab1.setBackgroundColor(Color.GRAY);
tab3.setBackgroundColor(Color.GRAY);
tab2.setBackgroundDrawable(null);
tab1.setBackgroundResource(R.drawable.back_button);
tab3.setBackgroundResource(R.drawable.back_button);
countTab = 200;
imgLoader.DisplayImage(fetchmodel.sectionContainerList.get(1).minSectionImgURL, R.drawable.loading, imgPoster, 7);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
tab3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
tab1.setTextColor(Color.BLACK);
tab2.setTextColor(Color.BLACK);
tab3.setTextColor(Color.RED);
tab1.setBackgroundColor(Color.GRAY);
tab2.setBackgroundColor(Color.GRAY);
tab3.setBackgroundDrawable(null);
tab2.setBackgroundResource(R.drawable.back_button);
tab1.setBackgroundResource(R.drawable.back_button);
countTab = 300;
imgLoader.DisplayImage(fetchmodel.sectionContainerList.get(2).minSectionImgURL, R.drawable.loading, imgPoster, 7);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
imgPoster.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(countTab == 100){
if(fetchmodel.sectionContainerList.get(0).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, ActorProfileActivity.class);
intent.putExtra("personID", fetchmodel.sectionContainerList.get(0).minEntryId);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(0).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, MovieProfileActivity.class);
intent.putExtra("movieID", fetchmodel.sectionContainerList.get(0).minEntryId);
startActivity(intent);
}
}else if (countTab == 200) {
if(fetchmodel.sectionContainerList.get(1).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, ActorProfileActivity.class);
intent.putExtra("personID", fetchmodel.sectionContainerList.get(1).minEntryId);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(1).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, MovieProfileActivity.class);
intent.putExtra("movieID", fetchmodel.sectionContainerList.get(1).minEntryId);
startActivity(intent);
}
}else if (countTab == 300) {
if(fetchmodel.sectionContainerList.get(2).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, ActorProfileActivity.class);
intent.putExtra("personID", fetchmodel.sectionContainerList.get(2).minEntryId);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(2).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, MovieProfileActivity.class);
intent.putExtra("movieID", fetchmodel.sectionContainerList.get(2).minEntryId);
startActivity(intent);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
});
profile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(countTab == 100){
if(fetchmodel.sectionContainerList.get(0).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, ActorProfileActivity.class);
intent.putExtra("personID", fetchmodel.sectionContainerList.get(0).minEntryId);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(0).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, MovieProfileActivity.class);
intent.putExtra("movieID", fetchmodel.sectionContainerList.get(0).minEntryId);
startActivity(intent);
}
}else if (countTab == 200) {
if(fetchmodel.sectionContainerList.get(1).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, ActorProfileActivity.class);
intent.putExtra("personID", fetchmodel.sectionContainerList.get(1).minEntryId);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(1).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, MovieProfileActivity.class);
intent.putExtra("movieID", fetchmodel.sectionContainerList.get(1).minEntryId);
startActivity(intent);
}
}
else if (countTab == 300) {
if(fetchmodel.sectionContainerList.get(2).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, ActorProfileActivity.class);
intent.putExtra("personID", fetchmodel.sectionContainerList.get(2).minEntryId);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(2).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, MovieProfileActivity.class);
intent.putExtra("movieID", fetchmodel.sectionContainerList.get(2).minEntryId);
startActivity(intent);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
});
photos.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(countTab == 100){
if(fetchmodel.sectionContainerList.get(0).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, PhotosLandingActitivity.class);
intent.putExtra("photosID", fetchmodel.sectionContainerList.get(0).minEntryId);
intent.putExtra("type", fetchmodel.sectionContainerList.get(0).minType);
intent.putExtra("actions", "FetchPhotos");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(0).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, PhotosLandingActitivity.class);
intent.putExtra("photosID", fetchmodel.sectionContainerList.get(0).minEntryId);
intent.putExtra("type", fetchmodel.sectionContainerList.get(0).minType);
intent.putExtra("actions", "FetchPhotos");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else if (countTab == 200) {
if(fetchmodel.sectionContainerList.get(1).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, PhotosLandingActitivity.class);
intent.putExtra("photosID", fetchmodel.sectionContainerList.get(1).minEntryId);
intent.putExtra("type", fetchmodel.sectionContainerList.get(1).minType);
intent.putExtra("actions", "FetchPhotos");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(1).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, PhotosLandingActitivity.class);
intent.putExtra("photosID", fetchmodel.sectionContainerList.get(1).minEntryId);
intent.putExtra("type", fetchmodel.sectionContainerList.get(1).minType);
intent.putExtra("actions", "FetchPhotos");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
else if (countTab == 300) {
if(fetchmodel.sectionContainerList.get(2).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, PhotosLandingActitivity.class);
intent.putExtra("photosID", fetchmodel.sectionContainerList.get(2).minEntryId);
intent.putExtra("type", fetchmodel.sectionContainerList.get(2).minType);
intent.putExtra("actions", "FetchPhotos");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(2).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, PhotosLandingActitivity.class);
intent.putExtra("photosID", fetchmodel.sectionContainerList.get(2).minEntryId);
intent.putExtra("type", fetchmodel.sectionContainerList.get(2).minType);
intent.putExtra("actions", "FetchPhotos");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
});
videos.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(countTab == 100){
if(fetchmodel.sectionContainerList.get(0).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, LatestPhotosActivity.class);
intent.putExtra("videoID", fetchmodel.sectionContainerList.get(0).minEntryId);
intent.putExtra("whichscreen", 200);
intent.putExtra("type", "actor");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(0).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, LatestPhotosActivity.class);
intent.putExtra("videoID", fetchmodel.sectionContainerList.get(0).minEntryId);
intent.putExtra("whichscreen", 200);
intent.putExtra("type", "movie");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else if (countTab == 200) {
if(fetchmodel.sectionContainerList.get(1).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, LatestPhotosActivity.class);
intent.putExtra("videoID", fetchmodel.sectionContainerList.get(1).minEntryId);
intent.putExtra("whichscreen", 200);
intent.putExtra("type", "actor");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(1).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, LatestPhotosActivity.class);
intent.putExtra("videoID", fetchmodel.sectionContainerList.get(1).minEntryId);
intent.putExtra("whichscreen", 200);
intent.putExtra("type", "movie");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else if (countTab == 300) {
if(fetchmodel.sectionContainerList.get(2).minType.contains("actor")){
Intent intent = new Intent(ArticleViewActivity.this, LatestPhotosActivity.class);
intent.putExtra("videoID", fetchmodel.sectionContainerList.get(2).minEntryId);
intent.putExtra("whichscreen", 200);
intent.putExtra("type", "actor");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else if(fetchmodel.sectionContainerList.get(2).minType.contains("movie"))
{
Intent intent = new Intent(ArticleViewActivity.this, LatestPhotosActivity.class);
intent.putExtra("videoID", fetchmodel.sectionContainerList.get(2).minEntryId);
intent.putExtra("whichscreen", 200);
intent.putExtra("type", "movie");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}catch(Exception e)
{
e.printStackTrace();
}
if(relatedArticlesLinearLayoutGridView != null && relatedArticlesLinearLayoutGridView.getChildCount() > 0)
{
relatedArticlesLinearLayoutGridView.removeAllViews();
relatedArticlesLinearLayoutGridView = (LinearLayout)findViewById(R.id.linear_related_articles_gridview);
}
gridView.setNumColumns(2);
gridView.setAdapter(new LazyAdapterForRelatedArticles(mContext,fetchmodel.reLatedArticlesList));
gridView.setExpanded(true);
if(fetchmodel.reLatedArticlesList.size() > 0)
{
Log.v("","checking related:");
relatedTxt.setVisibility(View.VISIBLE);
relatedArticlesLinearLayoutGridView.setVisibility(View.VISIBLE);
}
else
{
relatedTxt.setVisibility(View.GONE);
relatedArticlesLinearLayoutGridView.setVisibility(View.GONE);
}
relatedArticlesLinearLayoutGridView.addView(gridView);
previousTxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
try {
imagNum--;
if(imagNum < 0)
{
imagNum=fetchmodel.PhotoFeatureList.size()-1;
}
int valuimg=imagNum+1;
countTxt.setText("Slide "+valuimg+" of "+fetchmodel.PhotoFeatureList.size());
String image_url = fetchmodel.PhotoFeatureList.get(imagNum).imgUrl;
String image_url1 = image_url.replace("w=100", "w=300");
csImageLoader.DisplayImage(image_url1, R.drawable.loading, csImageSlider, 0);
titleTxt.setText(fetchmodel.PhotoFeatureList.get(imagNum).subTitle);
headlineTxt.setText(fetchmodel.PhotoFeatureList.get(imagNum).headline);
getTracker().trackPageView("ArticleViewPage- Image:"+fetchmodel.PhotoFeatureList.get(imagNum).imgUrl);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
nextText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{try {
imagNum++;
if(imagNum > fetchmodel.PhotoFeatureList.size()-1)
{
imagNum=0;
}
int valuimg=imagNum+1;
countTxt.setText("Slide "+valuimg+" of "+fetchmodel.PhotoFeatureList.size());
String image_url = fetchmodel.PhotoFeatureList.get(imagNum).imgUrl;
String image_url1 = image_url.replace("w=100", "w=300");
csImageLoader.DisplayImage(image_url1, R.drawable.loading, csImageSlider, 0);
titleTxt.setText(fetchmodel.PhotoFeatureList.get(imagNum).subTitle);
headlineTxt.setText(fetchmodel.PhotoFeatureList.get(imagNum).headline);
getTracker().trackPageView("ArticleViewPage- Image:"+fetchmodel.PhotoFeatureList.get(imagNum).imgUrl);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
pageLoadNum = 5050;
strEntryId=csFetchArticle.reLatedArticlesList.get(position).minEntryID;
imagNum=0;
if(csFetchArticle.PhotoFeatureList != null && csFetchArticle.PhotoFeatureList.size() > 0)
{
csFetchArticle.PhotoFeatureList.clear();
}
if(csFetchArticle.reLatedArticlesList != null && csFetchArticle.reLatedArticlesList.size() > 0)
{
csFetchArticle.reLatedArticlesList.clear();
}
articleContent_webview = (WebView)findViewById(R.id.webview);
sendEmptyMessageSync(SHOW_PROGRESS_DIALOG);
sendEmptyMessageAsync(FETCH_ARTICLE);
}
});
}catch(Exception e){
e.printStackTrace();
}
((ViewPager) container).addView(itemView);
//csViePageAdapter.notifyDataSetChanged();
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}

alertdialog redirection to url

I used the android alertdialog in order to redirect to a url, the redirection should go according to user choice, here is the code:
final CharSequence[]stringArray = {"1" ,"2" , "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (stringArray.toString().equals("1")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st));
startActivity(intent);
}
else if (stringArray.toString().contains("2")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
but when I click 1 or 2 there is no redirection to the url
what is wrong with the code?
You are checking your arrays items which is not valid way to implement. You need to check for selected item's id which you can get from onclick method only.
As the array count starts from "0" so can check your selected item with "0 to 2" where your 0 will be considered as "1" selected and so on.
Check out below code:
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (item == 0)
// if (stringArray.toString().equals("1"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st));
startActivity(intent);
} else if (item == 1)
// else if (stringArray.toString().contains("2"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
Please use following code:-
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (position == 0) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st));
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st2));
startActivity(intent);
}
else if (position == 2) {
// write some code
}
}
});
AlertDialog alert = builder.create();
alert.show();

Categories

Resources