Dynamic content for Android layout - java

Pardon the question title because I'm not really sure of the right term(s) to use.
I am just familiarizing myself with ListView and what I want to do is this:
-Suppose I have a Dog class which has String name, String breed, and int age.
-I would then make many instances of Dog and put them into a ListView which displays only their names.
-I would also have one layout, lets say doginfo.xml, that has the text "Name:", "Breed:", and "Age:".
So how can make the values of the clicked item (ie: name, breed, age) display on the next activity, which is doginfo.xml?

I'm presuming you have an array/arraylist of your dogs and you are adding them in order to your listview. Check out the onItemClickListener of this exmaple in the android doc:
http://developer.android.com/resources/tutorials/views/hello-listview.html
In your click listener use "int position" to get the object from your array/arraylist. Once you have this information, you can pass it easily between activities using "bundle.putString()" and "b.getString()".
Look at this simple tutorial for information on passing data between activities:
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/

It's easy. Once you got your ListView, set an OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onClick(AdapterView<?> parent, View view, int position, long id) {
//Get your Dog object from the 'position' parameter you get here.
//You should save the 'Dogs' in an array, so you can access them here easily.
Dog d = mDogsArray[position];
Intent intent = new Intent(YourActivity.this, DogInfoActivity.class);
intent.putExtra("name", d.getName());
intent.putExtra("breed", d.getBreed());
intent.putExtra("age", d.getAge());
startActivity(intent);
}
}
If you want to add/remove dogs dynamically, you can use the method ListView.setListAdapter to change the views the list view is showing. Create an array adapter this way:
ArrayAdapter<String> dogs = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
and add dogs this way:
dogs.add(dogInfoString)

Let me reply the step that you need to follow:
Create an arraylist of dog class object ArrayList<DogClassObject>
Now in 1st activity, just take the name from every object from ArrayList and add one by one name in ArrayList<String>.
Use the ArrayList to display inside the ListView of 1st activity.
To implement this, go through this example: Android Simple ListView
Now, to implement on click listener and to display dog_info, perform the below steps:
Implement OnItemClickListener inside the ListView.
Follow the answer given by #Jong but pass the clicked object from the ArrayList (you can get the clicked object from ArrayList by position argument).
Get the dog information and display name, age, breed info inside the dog_info
To implement this, go through this example: Android - Custom ListView with 2 TextViews

Related

Why is my RecyclerView showing Incorrect order of CardView items?

I have a RecyclerView of CardViews of Food (class) objects. A weird thing that is happening is that when I'm using a method to add a new Food object to the ArrayList that I'm using, and notifying the adapter that a value has been added, the Card gets added on screen, but it has a different set of values.
I'm using 2 placeholder Food objects to show on screen at default, and pressing the Add button is supposed to add a new object with name "Name", but instead I get another copy of the first placeholder.
When too many Cards are added to screen, swiping up and down changes the order of the cards, and suddenly, the new items (with the correct data) appear!
What's wrong?
The app doesn't crash btw.
Function to add new Food object to ArrayList called 'foods':
public void addFood(){
foods.add(new Food("Name",0,3,2)); //a constructor
foodAdapter.notifyItemInserted(0);
}
Apparently, the issue was that calling .add(Object) on the ArrayList item 'foods' actually appended the new Object to the end of the list, meanwhile, Adapter.notifyItemInserted(0) made the Recycler view think that Position 0 got the new Card, whereas it was actually Position last.
This made the wrong item show up at the RecyclerView.
So, as obvious, the fix is to use the ArrayList.add(position,object) constructor instead of .add(object), where position shows where the new element was inserted.
So, the correct code is:
public void addFood(){
foods.add(0, new Food("Name", 0, 3, 2)); //a constructor
foodAdapter.notifyItemInserted(0);
}
If you want the new object to be inserted at a different position, simply pass that position in place of the 0 above.
you just need to change foodAdapter.notifyItemInserted(0) into foodAdapter.notifyDataSetChanged() or foodAdapter.notifyItemInserted(foods.size() - 1).

How to put a recyclerview inside another and receive data from the row selected?

Hi guys i need some help in java im trying to make a recyclerview that have some items and i wanted whenever someone clicked on one item it took him to the other recyclerview but with the data i set it for ?? PLS HELP
if you want to send the data in this specific item.
in the recyclerview class
the list of the items presented in the recyclerview declare them as global variables.
and you will find this method
public void onBindViewHolder(Holderclass holder, final int position)
you can use this position variable to access that specific item in the list.
for example if you have arrayList "list" then.
list.get(position).
save the data in bundle and use putextra() to send it if it is a new intent or setarguments() if it is a new fragment, and then retrieve the data via getextra() or getarguments() in the new activity or fragment. repeat what you did with the first recyclerview
I hope this helps

Is it possible to reduce number of pages in android?

I am making an android app. this app consist of list. by clicking each item of list a new page opens.
this list contains 50 items.I should make 50 activity and their corresponding XML file.
so, is there any way that make this process easier that don't force me to make all this 50 activity one by one?
my reputation is not enough. so I upload related picture.
http://uupload.ir/files/oh1o_1.png
http://uupload.ir/files/zam9_2.png
Not sure what you mean by creating 50 pages. You have NOT to create a new class for each item in the list, instead you will create a new instance of a class. This is what programming, and OOP (Object Oriented Programming) comes into play.. Basically what you need to do is:
Create a "main_activity" class which will contain your list
Create a "item_detail_activity" which will show the details about your clicked item
What you need to write is the logic of "passing" the correct data depending on the clicked item. When an item in the "main_activity" is clicked, you will create a new instance of "item_detail_activity", passing the correct data (through a bundle).
BTW there are a lot of tutorials out there that will help you understand better the logic of an Android application.
If you have a list of items probably means that you have a list of objects which are of the same kind with same structure.
Due to this, you will have 50 pages which show to the user the same item with different values to variables, with the same structure.
You can make the objects in your list parcelable (refer to this), then pass it with the Intent to a second Activity you create, receive it, and at last populate the screen with the item you passed.
If you have troubles or doubts feel free to ask :)
EDIT:
Imagine your listview is called ListView, and the List of items you used to fill the listview is called list in the MainActivity do this:
ListView listView = (ListView) findViewById(R.Id.ListView);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// position is the index of selected item
// Launching new Activity on selecting single List Item
Intent i = new Intent(MainActivity.this, SecondActivity.class);
// sending data to new activity
i.putExtra("item", list.get(position));
startActivity(i);
}
});
in your SecondActivity, with getParcelableExtra("item") you retreieve the item clicked. Here with the variables of this item you can populate the page.
here is the doc for intents.
In secondActivity, if something must disappear if a variable has a certain value or is null, play with visibility or fragment: create an Activity with all case shown, than work with visibility or with fragments and adjust it ;)
Change your activity to fragment activity and write code to display the details corresponding listview click. it easy to follow.

how do i identify which switch was pressed in listView

I have objects in a list view each with a switch.
how do I identify which switch was pressed meaning which item it was included in. does list view have a method to tell me which object it was in the arraylist of the listview
You can easily know which item is clicked in your OnCheckedChangeListener() by setting a tag with some definite information about the current ListView item using button.setTag() (you can put whatever Object you want in this tag). You will need this in your custom Adapter.
Which you call back in your Listener using view.getTag(). This call will return the same Object. You can then use this method to have a distinct id or string (most often the content of the current item in your arrayList) in each separate ListView.

android searching the list view?

i have created listview as follows...
lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, mDisplay));
my question is that i need to make the listview searchable such that if the user enters an alphabet the listview must scroll to that item starting with that alphabet (i dont want it to be filtered) with all the list items visible ...
for example if the user press "f" i want the list to scroll to the 1st item with "f" as follows
i DONT want it to be as follows:
i am sorry if the question is not understandable because i dont know how to put it
ask me if u need more details...
thanks :)
following link might help you Android search list while typing
I don't know if something for your requirement in readly available. But you can implement it through a logic like this.
Whenever user presses say f, check in your arraylist from which position words from f starts. say from 5th position it starts.
and use
listview.smoothScrollToPosition(int position)

Categories