Open several layouts from a single activity (listview) - android

I created a list of drugs in my main activity using a ListView in Android Studio.
Since drugs are many and have same characteristics (indication, contraindication, posology) I'd like to know if there is an alternative to create an activity for each drug. In other words: is it possible to open different layouts from one single activity? Is it possible to create a "template" layout and insert only the contents (posology and so on...)? I tried with Fragment but I failed miserably. Alternatively, creating a lot of activity can slow down my application? Thank you (I apologyze for my terrible english grammar)

What I would suggest doing is creating something like a DrugDetail activity which you pass information about the drug to. This means instead of creating loads of activities for every drug you just create one activity and pass it all the drugs information.
So what you would do is set an onItemClickListener onto your drug listview the. Inside that listener method have something like this
Intent intent = new Intent(MyActivity.this, DrugDetail.class);
intent.putExtra("drugName", "drugnamestring");
intent.putExtra("drugDescription", "drugDescriptionString");
startActivity(intent);

In your case the best practice is creating base layout (extends common layouts e.g. FrameLayout or RelativeLayout) witch you will inflate in your own class witch will extends ListView class for all items of this custom ListView. When user clicks on list's item it extends and shows him all information about particular drug. This video is all you need, enjoy! http://www.youtube.com/watch?v=mwE61B56pVQ

Related

First Activity to Second Activity (Intent, Database and Adapter)

I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks

Load RecyclerList Data From One Class To Another Class's RecyclerList

I have the RecyclerList Using Sqlite Database; the Data from Sqlite is Showing In the List in A Class. What I want to do is; As the Item is Selected From One Class In the List Position Get Load To Another Class.
My Question Is How Is It Possible To Do This. I have Searched A Found An Application On Play store Which is Doing the Same Thing I want..
I have ViewPager and I want this thing, As the ListItem From 1st Class is Selected It should Load to Another Class List... And If comeback and select another It shows again to another class list with the Item Already added...
you can implement a listener for this. Create an interface which communicates between fragment to fragment or fragment o activity. Ref: https://developer.android.com/training/basics/fragments/communicating

How to update the UI in android

I have an activity A with a listview and upon selecting some items in the list, and navigating to another activity B with the selected items in the list of Activity A. And made some modifications. After return to A. My screen is not updating with the latest modifications.
Please guide me how to do this. Thank you in advance.
If you have unified data storage for both activities (so that you know that this problem is purely UI issue, because the data itself is updated upon returning from activity B), then you should call this method on your ListView adapter:
yourListView.getAdapter().notifyDataSetChanged()
In order to display what you want, you need to have some TextViews in your other activity and then just add a text to them to display what you need.
When you get the data, just do like this in the onCreate method:
TextView tv = (TextView)inflate.findViewById(R.id.myTextView);
tv.setText(theDataStringYouReceived);

Android: Dynamically Change Activity Title to Text of Selected List Item

I'm new to Android development.
I created a simple master-detail app that starts with a simple, vertical scrolling list of topics.
When the user selects a topic, a details screen appears, replacing the first screen, with a list of details that pertain to the selected topic.
I want the title for the details screen to show the topic the user has selected on the first page, but haven't been able to solve the problem after working for almost a week.
All I need to know is, Can this be done? Not looking for someone to solve this for me, but maybe a hint or a link to a tutorial that shows how this can be done.
Note: I'd post a drawing of what I want to do, but I'm new here and don't have 10 reputation yet.
Thanks,
SonCoder
Not exactly sure what you want but either way..
-You have a listview. Each view (the data) in the listview should be a represented by a model. (aka a separate class containing specific information that you want to represent for each listitem.
-Write a custom list adapter (extend from base adapter).
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
In the getView method of this class you load the the String field of the model that you want in the textview.
-Make sure to use the viewholder pattern in the adapter above. I noticed the example doesnt use one. This speeds up scrolling in the list because there are much fewer calls to findViewById.
-in the list activity set up a View onClick listener. This should create an intent (for launching an activity) or a fragment transaction (for fragments). Send the instance of your entire model (will get from
parent.getAdapter().getItem(position);
in the on click method) into the detail activity.
-if you want to set a textview title just get the textview and set it from the model. It will be the same filed you inflated in the getView method of the adapter.
-if you want to set the titile in the actionbar set:
this.getActionBar().setTitle(title)
This is simple. Just send extra data in the intent that starts the activity and then in the activity's onCreate read the data and then use the setTitle(myString) method from the activity.
setTitle(String title) can be called from anywhere using the activity by the way.
So, your in your listadapter, then you set a listener on your view right? A simple onClickListener on the whole "root" view is just fine.
In the listener you say something in the ways of this:
Intent intent = new Intent(myActivity, MySubActivity.class);
intent.putExtra(key, titleName);
myActivity.startActivity(intent);
Note that the activity reference should be set in the constructor of the adapter and that the "key" String is something you get from your strings.xml. Do not duplicate these in code since if you change one and forget to change the others you might get some wierd NPEs.
Continue in your MySubActivity's onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String key = getString(R.string.my_title_key);
String title = intent.getString(key);
setTitle(title);
}
NOTE: I'm not sure of all method names are correct and such but something like this.

Android: TabHostFactory.createTabContent not being called for all tabs?

I have an app that needs tabs with one list in the content of each tab. Those lists need to refresh several times without changing the tab. The lists represent trees of possible choices, so the user clicks element 4, for example, and I have to refresh the list to show the elements under 4.
The easiest (and probably ugliest) way I found to accomplish this is to attach a listener to the list "currently" shown, and launch "this" activity again (with some extras relating the element the user clicked), effectually recreating the whole view, tabs and everything. This allows me to keep each state of the lists independent, and makes the back support easy enough. --I said it was ugly!
For that to work, I create the tabs inside onCreate, like this:
TabHost.TabSpec spec1 = tabHost.newTabSpec("TOC").setIndicator("TOC", res.getDrawable(R.drawable.ico_table_of_contents)).setContent(this);
tabHost.addTab(spec1);
TabHost.TabSpec spec2 = tabHost.newTabSpec("BLAH").setIndicator("BLAH", res.getDrawable(R.drawable.blah)).setContent(this);
tabHost.addTab(spec2);
The .setContent(this) works because this activity extends TabActivity and implements TabHost.TabContentFactory. The createTabContent function returns this:
return from(this).inflate(R.layout.new_list, getTabHost().getTabContentView(), true);
no matter which tab I'm in.
After this, I update the List with an adapter, like this:
private void updateList(ArrayList<Element> items) {
ElementAdapter adapter = new ElementAdapter(this, R.layout.element, items);
ListView list = (ListView) findViewById(R.id.theListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {...}
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
The problem is that the first tab works great, but the second doesn't. It needs just the same view as the first one (the list showing a list of elements), but it shows nothing-zippo-nada, just the tabs. The ArrayList I pass in has elements in both cases.
Walking the code, I noticed that in my createTabs, the createTabContent is called ONCE, on tabHost.addTab(spec1) (for the first tab), but NOT when adding the second.
What gives? Why do I get an empty screen instead of the list, if both cases have elements to show?
I hope this makes sense. It's a bizarre problem to explain and I'm not sure if I'm making much sense.
Any help will be appreciated.
Thanks
llappall
I have never tried using the same TabContentFactory more than once. I would have expected what you used here to work, particularly given the tag parameter to createTabContent().
That being said, try using multiple TabContentFactory objects and see if that helps (e.g., use instances of anonymous inner classes).

Categories

Resources