I'm pretty new on android, i'm currently reading a lot of documentation to understand how to start. I've tried to search before asking here but i'm not able to find relevant information.
My problem is have 2 listview in the same acivity.
The Listview A with a list of category.
The Listview B with a details data (at startup the values showed are based on the category 1 of the listviewA)
When a click on a item of the listviewA the data of the listViewB change to reflect the new selection and show the new list of details data.
Can someone give me the right direction or a link to a tutorial that cover this topic ?
Sorry i'm not able to post any code at the moment.
I am not sure that the type of layout you are trying to make is user friendly. It would give the users far more richer UI experience if you change with an ExpandableListView. You can check out a tutorial here. Using this would give you the chances of engaging the user via an interaction.
Anyway, if you are sure to go with ListView then simply create a root LinearLayout and within this you take two ListViews. But taking the second one as a listview does not make sense. Rather, instead of making the second control a ListView, take a TextView to show the details.Provide the height of both layouts (ListView and TextView) according to your requirement.
Look at this tutorial
2 Listviews:
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ListView
android:id="#+id/listView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
</LinearLayout>
1 ListView 1 TextView:
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
<TextView android:id="#+id/TextView1" android:text="details"/>
</LinearLayout>
UPDATE:
Please explain your problem a bit more. Because, in your scenario the data between the list views is connected with each other. Say, if ListView A is about Products so ListView B is about ProductDetails. So you need to declare a class variable say ProductID which is common entity between both data and set this variable setOnClickListener in ``getView()` of ListView A and fetch the data regarding this just after that and set the Adapter of ListView B with the results of this query (which may be to SQL db or RESTful server).
I have used this strategy before and it worked out well for me.
Use a linear layout to position the two list views, Populate the 'category' listview and set a list adaptor for it. On this listview I set an OnItemSelectedListener which when activated set ups the list-adaptor on the second 'detail' view.
Depending on your exact need you can either replace the detail listview's adapter or just change the content the adapter looks at and tell it to refresh.
Try this:
After get listview by id (list_A) set click listener
list_A.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//here you can get click position from parameter position
// and do somthing in Adapter_B and then call notifyDataSetChanged() for Adapter_B
Adapter_B.notifyDataSetChanged();
}
});
Related
I have several edittexts in my view for taking input for a database query .I'm displaying the query results in a list view below the edittextfields. Because there are 7 edittextfields there is not enough space for the listview. I wanted to put all the edittexts in an expandablelistview.
Problem i'm facing is -only the expandablelistview item is shown -the listview doesnt come up at all --i have #android:id/empty and #android:id/list entries ---but nothing comes up
i can't add the listview into the expandablelistview as that is not allowed + since i need both #android:id/empty and #android:id/list to show query results i need a proper list view.
what are my options --? i could ofcourse display the results in a separate window
If you could show the xml you have right now it would be great but the ideas behind it needs to be something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Edit:
Okay so i figured out that for this layout to function correctly you need to set dome weight so that the linearlayout understand what size to give each children. Use:
android:layout_weight="1"
in the listview and the expandablelistview (1 in both means thay will divide space between them)
actually my problem is same as this guy's.But I don't know how to resolve it.Here's my listview xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:background="#CCCCCC"
android:id="#+id/tabs">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CCCCCC"
android:textSize="20sp"
android:textColor="#000000"
android:text="#string/NewTask"
android:id="#+id/tab1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="#000000"
android:text="#string/Friends"
android:id="#+id/tab2"
/>
<Button
android:layout_width="fill_parent"
android:background="#CCCCCC"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="1"
android:text="#string/AddPeople"
android:textSize="20sp"
android:id="#+id/tab3"
/>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:layout_below="#+id/tabs"
/>
</RelativeLayout>
I am trying to call a function in my listactivity from the onpostExecute of my AsyncTask class.
Here's the function of my listactivity.
public void SetImage(Bitmap bmp,int index)
{
View v = this.ls.getChildAt(index);
ImageView img = (ImageView)v.findViewById(R.id.tasks_userimg);
img.setImageBitmap(bmp);
}
This bmp is the bitmap downloaded in the asynctask class and ls is the listview and the index is the index of the item in the listview.When the postexecute runs I call this function with the arguments to update the listview item image.
The problem is exactly same as I've mentioned in the link i.e when I scroll it gives me error.But he solved the problem by changing the layout width of listview but it is not working here.
Seems that like many others , you have some problems with this special view .
I highly recommend watching the lecture "the world of listView".
They talk about a lot of topics related to listView , including the topic of calling getView multiple times on the same view. In short , the reason why it occurs is because getView is also called for measuring the listView items . You can use (and should) use the convertView in order to avoid un-needed inflating and fetching of data .
About the question itself , you should not use findViewById on the listView items , since they are getting re-used . For example , a view that was set for the 0-th position may be set to the 7-th position in case the user has scrolled down .
if you wish to update a single item from the listView , you can use something like this:
// itemIndex is the index of the item to update
final View v=listView.getChildAt(itemIndex-listView.getFirstVisiblePosition());
//now you update your view according to what you wish .
//you must of course handle the cases that you can't get the view .
Of course , once you do it , you will also have to update your data behind the adapter , so that the next time the user scrolls to this item , it will have the bitmap shown to it.
What you should do depends on the asyncTask you've used .
I assume that you download multiple bitmaps while the listview fills its items . If that's the case , check out this sample , or use my suggestion : have an asyncTask for each viewHolder you have . For each time getView is called , cancel the old one and create a new one for the current item.
1) Define your custom adapter
2) Apply holder pattern
3) Use some image uploader (for example this)
Take a look simple example
I'm making a custom view which contains an ExpandableListView, a CheckedTextView and a ListView :
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filtersDialogView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="200dp"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/filtersListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckedTextView
android:id="#+id/onlyShowAvailableItemsCheckedTextView"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:text="#string/OnlyShowAvailableItems" />
<ListView
android:id="#+id/categoriesListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The problem is that the CheckTextView is fixed and the two lists are scrolling independently (which is normal for that layout). Furthermore when I expand a list item from the ExpandableListView, the CheckedTextView and the other ListView become hidden. I'd like to make a single list with all these elements that could scroll and resize properly when I expand an element of the first list (the cells of the second list are not expandable). How can I do that? Is it possible to make it without having to modify the Adapters of the lists and the controller of the CheckedTextView ?
Thanks
How can I do that? Is it possible to make it without having to modify
the Adapters of the lists and the controller of the CheckedTextView ?
I doubt that, especially as you have two scrolling views, the ExpandableListView and the ListView. I think your only option is a special adapter that will simulate the layout above. I had a custom ExpandableListView adapter that I modified into something that will simulate the layout like the one above. It's something raw so there could be some errors(I've tested it a little) and of course there are other things to work on. The code sample it's a little big so I put it here https://gist.github.com/3046887 .
On my screen I have a list view and a button. my list has like 8 item. I would like my screen to scroll if both these items does not fit in. I don't want my list to have scroll but the complete layout including both list & button. If I use the below layout it only shows on item inside the list and I have to scroll within the list to go to next item.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#android:id/list"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#drawable/round_background" />
<Button android:text="Search" android:id="#+id/carSearchButton"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
You can't put a ListView inside a ScrollView. Of GridView, or whatever View that handles scrolling on the same axis as the ScrollView does. That way the framework wouldn't know which View should handle the scrolling event. This layout won't produce an error when you compile it, but it won't work properly.
What you should do here: dump the outer ScrollView, you don't need it. Only use a ListView, and add the button to the ListView, using .addFooter(), that's the easiest way. This way your button'll appear as a list element, but you don't have to mess around with a custom adapter.
Scythe kind of answers my question but I wanted more then one one control below the list also on another screen I wanted 2 lists. So in order to have the scroll bar working with list view I had to fix the height of the list.
There is something I'm just not getting, and I'm looking for assistance in understanding what is happening here.
I have a custom list adapter (that just extends BaseAdapter) that I have successfully been using to generate and display a list. Now I want to add a static footer to the bottom of my list. After looking at a number of resources (specifically this one) I've come to realize that my reluctance of using XML has to come to an end, and set up the following xml layout in a file called devices_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="#+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ToggleButton android:id="#+id/bottom_control_toggle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Filter Favourites OFF"
android:textOn="Filter Favourites ON"/>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_control_bar">
</ListView>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_empty_list"
android:layout_above="#id/bottom_control_bar"/>
</RelativeLayout>
After some adjustments to the activity that holds the list, I ran the code. I see my footer, (and also the tab widget which is parent to everything), but the area where the list goes is empty.
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.devices_list);
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
list = new DeviceListAdapter(this, AllCur); //make my custom list adapter
setListAdapter(list);
}
Is there some way to link up the ListView widget declared in my xml with my DeviceListAdapter? It's pretty clear to me now that I'm not entirely sure about how this is all working. Any help in clarification would be much appreciated.
You have both the ListView and the TextView set to android:layout_above="#id/bottom_control_bar", which means the TextView will overlap the ListView. And, you have said that your ListView height is 0dip, which will make for an extremely short list.
I would define the ListView as being above the TextView and anchored to the top of the screen (android:layout_alignParentTop="true").
Is there some way to link up the
ListView widget declared in my xml
with my DeviceListAdapter?
You already are, by calling setListAdapter().