Android how to display 2 listviews in one activity one after the other
Use Like this:
Remove Linear layout. use relative layout and inside that place your two list view like this.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollojt"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00" >
</ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:background="#0f0" >
</ListView>
</RelativeLayout>
</ScrollView>
add Utility.java
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
In your Activity:
lv1.setAdapter(adapter);
lv2.setAdapter(adapter);
Utility.setListViewHeightBasedOnChildren(lv1);
Utility.setListViewHeightBasedOnChildren(lv2);
multiple listview in one activity
You should extends Activity
instead of ListActivity
in your code.
And in your activity's layout xml file you should take two different id for that two list view.
See below reference links for more details...
http://www.coderzheaven.com/2012/03/02/a-simple-layout-with-two-listviews/
Multiple Listviews in single Activity in Android?
Android how to display 2 listviews in one activity one after the other
Two different Listviews in one Activity, only one gives right ItemClicks
The reason is that you are using different datasets to populate your listviews but in onItemClick
you are fetching the data from only one of the datasets although you are selecting an item from any of the listviews.
First List uses "CustomListViewValuesArrCT" dataset
ListView listCT = (ListView)findViewById(R.id.listView);
adapterCT=new CustomAdapter(CustomListView, CustomListViewValuesArrCT,res);
listCT.setAdapter( adapterCT);
Second List uses "CustomListViewValuesArr" dataset
ListView list= ( ListView )findViewById( R.id.listView1 );
adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
list.setAdapter(adapter);
BUT
Fetching data only from "CustomListViewValuesArr" in either list item selection .
public void onItemClick(int mPosition)
{
// You are fetching value from "CustomListViewValuesArr" only
ListModel tempValues = ( ListModel ) CustomListViewValuesArr.get(mPosition);
.....
}
You need to fix your flow so that upon list item click you fetch the data from that dataset using which you set the adapter for that particular list.
How to show multiple ListView in one activity?
I have found answer to my question. Answers provided by other users may be are correct too but they were too long for such a lazy person like me:) the most suitable way was to put listviews inside scrollview but they displayed only first item of list. I have just changed ListView in my xml-file into ExpandedListView. More detailed answer here http://joerichard.net/android/android-listview-not-wrap_content-inside-scrollview/ . Credits to author of this blog Joe Richard
One Activity, two ListViews (two Fragments?)
I think you do not need two ListViews at all. You need to implement one ListView with headers as described here.
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Item> items = new ArrayList<Item>();
items.add(new Header("Header 1"));
items.add(new ListItem("Text 1", "Rabble rabble"));
items.add(new ListItem("Text 2", "Rabble rabble"));
items.add(new ListItem("Text 3", "Rabble rabble"));
items.add(new ListItem("Text 4", "Rabble rabble"));
items.add(new Header("Header 2"));
items.add(new ListItem("Text 5", "Rabble rabble"));
items.add(new ListItem("Text 6", "Rabble rabble"));
items.add(new ListItem("Text 7", "Rabble rabble"));
items.add(new ListItem("Text 8", "Rabble rabble"));
TwoTextArrayAdapter adapter = new TwoTextArrayAdapter(this, items);
setListAdapter(adapter);
}
}
Related Topics
How to Pause/Resume Thread in Android
How to Set Internet Options for Android Emulator
Distance Calculation from My Location to Destination Location in Android
Android: Viewpager and Horizontalscrollview
How to Open a Different Activity on Recyclerview Item Onclick
Failed to Resolve: Com.Android.Support:Appcompat-V7:27.+ (Dependency Error)
Invoke-Customs Are Only Supported Starting with Android 0 --Min-API 26
How to Write Character & in Android Strings.Xml
How to Detect the Swipe Left or Right in Android
Removing Log Call Using Proguard
Java.Lang.Noclassdeffounderror: Javax.Activation.Datahandler in Android
Add New Item Count to Icon on Button - Android
How to Fix "Unexpected Element <Queries> Found in <Manifest>" Error