Cannot setImageResource - java

I'm trying to build an android app to stream online radio.
Currently it starts to shape up and works like this: When I click on a gridview item in MainActivity, it starts the AndroidMediaPlayer activity that streams a fixed url and shows a fixed image that doesn't represent the users selection.
Code: https://github.com/sthlmj/IORadio/tree/clickable-funktionen/app/src/main/java/se/mookito/ioradio
Now I am trying to make it work properly, so that if I click on a certain gridview item, the image and the stream url should play according to the selection. I started by trying to get the picture to be displayed correctly by trying to pass clicked position from the MainActivity
of the items ArrayList in MyAdapter via intent so that I can setImageResource on the second activity AndroidMediaPlayer according to selected image from the first activity MainActivity.
In the second activity I tried to get the intent data:
//Selected image id
int position = i.getExtras().getInt("id");
MyAdapter myAdapter = new MyAdapter(this);
//Set image id
ImageView imageView = (ImageView) findViewById(R.id.mp3Image);
imageView.setImageResource(myAdapter.items.get(position));
But I got: 'setImageResource(int)' in 'android.widget.ImageView' cannot be applied to '(se.mookito.ioradio.MyAdapter.Item)'

Call imageView.setImageResource(myAdapter.items.get(position).drawableId); instead.

Related

TabLayout Fragment Issue

Current layout
The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..
Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:
CompletableFuture.runAsync(this::prepare);
recyclerView.post(() ->
{
adapter.list = newList;
adapter.notifyDataSetChanged();
progress.setVisibility(View.INVISIBLE);
});
Now the issue that I am facing:
Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.
TabViewerAdapter extends FragmentStatePagerAdapter
The adapter is initialised using ChildFragmentAdapter as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());
I know behaviour "0" is deprecated so tried with "1" as well as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());
Tried invalidating views, notifying adapter and many other.. No use.
I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.
This means, I am missing something and tried the onResume method. But didn't work.
Am I doing something stupid or against design patterns or conventions? No idea what to do..
Please help, thank you.
I resolved this issue by doing the things below:
Used a SharedViewModel to handle the list
Used ListAdapter for RecyclerView
All good now.

Showing data in one activity depends on choice in others activity listView - how to

I am developing an android news app which has goal to show titles in one activity in listView, and by clicking on each title it must open another activity and show description, photo and also title. These are pulled from web in JSON.
For now, I have list view in first activity which has been populated with titles from JSON,using AsyncTask and SimpleAdapter, and now, I need some theory boost how to make further progress.
So, how can I select one title and by this title get other parts of JSON and show them in another activity?
Any advice?
Have an onClickListener on the items in listView. In the onClickListener get the title and start a new activity. For the new activity add the data you need to send.
Intent intent = new Intent(this, DetailActivity.class);
// add the data here
intent.putExtra("title", title);
startActivity(intent);
In DetailActivity.java
String title = getIntent().getExtras().getString("title");
make an API call with the above title

Espresso RecyclerView inside ViewPager

I am new to Android Espresso library. Trying to test click on RecyclerView item inside the ViewPager. I've searched the Internet but found nothing about ViewPager. So my question is: how to access RecyclerView inside a ViewPager using Espresso to perform click?
Let's suppose you want to tap on buttonClick, which is the id of a button inside your RecyclerView.
I use this code:
onView(allOf(withId(R.id.buttonToClick), isCompletelyDisplayed()))
.perform(click());
This gets the view with this id which is currently displayed and performs a click on it.
Hope this helps.
P.S.: Also, on espresso-contrib there are RecyclerViewActions that maybe you can use: actionOnHolderItem, actionOnItem and actionOnItemAtPosition. You can do something like:
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

Android Expandable list view - webpage

I am working on an android app. I finished my expandable list view and have every thing working. I want to add some more feature on the expandable list view.
What I want to do is. When the expandable list view is expanded, I want to click on the child and than lead me to a webpage.
Is there a method to do this?
Thanks for the help
String url = "http://www.website.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

Android Add image to listviewitem after click on button

In my application I have a listview with products. Above the listview in the actionbar I have a button "edit list". When you click on the button I want to add an image to each listviewitem in the listview. When you click on the image you delete it from the list. Now I added an imageview to the listviewitem and set die visibility to gone.
But I don't know how to change the visibility with a click on a button.
So the question: How do you add an imageview in listviewitem with click on button? Or how do you change the visibility property of an imageview in an Listviewitem?
You can use a static parameter like int uiUpdate=0 and use an adapter to add data to list view and check uiUpdate in getView of your adapter. When user click on button set uiUpdate=1 and reffresh your list with this : yourlist.setListAdapter(new EfficientAdapter);
in getView of EfficientAdapter check value of uiUpdate if it's 1 set visibility VISIBLE : yourimageview.setVisibility(View.VISIBLE); efficientAdapter is something like this : http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

Categories

Resources