I want to have multiple listviews in single activity. But only one listview should be displayed at one time. The listviews will be loaded dynamically. So, how can I fill all the four listviews at the same time and display only one?
-
Display only one means, you want to hide the rest of the tree or you want to show only one at a time and on specific action you want to display another. whatever you want to do, firt do one thing, you can implement the code which will load data in all the 3 -4 list views, means create adapters and bind with list view. and then hide the rest of the 3. and you can show them one by one as you needed. – Rahul Patel Apr 05 '10 at 05:19
4 Answers
Instead of using all that code to flip between listviews why not put all your listviews inside a ViewFlipper? It the easiest way an required almost 1 line of code! :)
-Moto

- 41
- 2
You can do one thing for your problem. The reason you will only see the one list view, may be because one list will have the fill parent paramter value for its height. What you have to do is, use absolute layout or just fix the height for each layout. Also use the scrollview to place the controls. Moreover, if you post your XML layout here, then it will be easier to answer your query as to where you need to make changes.

- 59,888
- 27
- 145
- 179

- 1,198
- 7
- 5
You should use a FrameLayout
; it allows z ordering of its child views which means that you can have your ListView
stacked one above the other. You can then toggle visibility of each of these ListViews from your code using View#setVisibility(
) method.
You would create appropriate adapters for each of these ListViews and whenever a ListView is to be drawn the framwork will call getView()
method of the required adapter.

- 36,316
- 26
- 109
- 116
-
I have used mostly the same way of showing multiple listviews as you suggested i.e. using setvisibility parameter. But now what I want is to have the Bitmap image of all the four listviews. Suppose, my activity starts and the data are loaded into all the four listviews by binding them to their respective adapters. Now, when I am trying to fetch the Bitmap using getDrawingCache() it always returns null to me. So, can you let me know how can I fetch the Bitmap of all the four listviews? – sunil Apr 05 '10 at 12:05
I have used mostly the same way of showing multiple listviews as you suggested i.e. using setvisibility.
But now what I want is to have the Bitmap image of all the four
listviews. Suppose, my activity starts and the data are loaded into
all four listviews by binding them to their respective adapters.
Now, when I am trying to fetch the Bitmap using getDrawingCache()
it
always returns null to me.
So, can you let me know how can I fetch the Bitmap of all the four listviews?
public class HomeScreenActivity extends Activity
{
private ImageView imgBabble = null;
private ListView listBabble = null;
private ListView listVille = null;
private ListView listPrivateMsgs = null;
private ListView listBookmarks = null;
private List<String> tempBabble = null;
private List<String> tempVille = null;
private List<String> tempPrivateMsgs = null;
private List<String> tempBookmarks = null;
//RelativeLayouts
private RelativeLayout babbleLayout = null;
private RelativeLayout villeLayout = null;
private RelativeLayout privateMsgsLayout = null;
private RelativeLayout bookmarksLayout = null;
//Bitmap
private Bitmap babbleShrunkView = null;
private Bitmap villeShrunkView = null;
private Bitmap privateMsgsShrunkView = null;
private Bitmap bookmarksShrunkView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.homescreen);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitle);
imgBabble = (ImageView) findViewById(R.id.imgBtnBabble);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.babble_top_logo);
BitmapDrawable bmpDrawable = new BitmapDrawable(bitmapOrg);
imgBabble.setBackgroundDrawable(bmpDrawable);
//Initialize the layouts for Babble, Ville, Private Messages and Bookmarks
babbleLayout = (RelativeLayout) findViewById(R.id.babbleLayout);
babbleLayout.setDrawingCacheEnabled(true);
villeLayout = (RelativeLayout) findViewById(R.id.villeLayout);
villeLayout.setDrawingCacheEnabled(true);
privateMsgsLayout = (RelativeLayout) findViewById(R.id.privatemsgsLayout);
privateMsgsLayout.setDrawingCacheEnabled(true);
bookmarksLayout = (RelativeLayout) findViewById(R.id.bookmarksLayout);
bookmarksLayout.setDrawingCacheEnabled(true);
//Babble
tempBabble = new ArrayList<String>();
tempBabble.add("First Babble");
tempBabble.add("Second Babble");
tempBabble.add("Third Babble");
listBabble = (ListView) findViewById(R.id.listBabble);
listBabble.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempBabble));
//babbleShrunkView = babbleLayout.getDrawingCache();
//babbleLayout = (RelativeLayout) findViewById(R.id.listLayout);
if(babbleShrunkView == null)
{
System.out.println("Babble View is null");
}
//Ville
tempVille = new ArrayList<String>();
tempVille.add("First Ville");
tempVille.add("Second Ville");
tempVille.add("Third Ville");
listVille = (ListView) findViewById(R.id.listVille);
//listVille.setDrawingCacheEnabled(true);
listVille.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempVille));
//villeShrunkView = villeLayout.getDrawingCache();
if(villeShrunkView == null)
{
System.out.println("Ville View is null");
}
//listVille.setVisibility(View.GONE);
//Private Messages
tempPrivateMsgs = new ArrayList<String>();
tempPrivateMsgs.add("First PM");
tempPrivateMsgs.add("Second PM");
tempPrivateMsgs.add("Third PM");
listPrivateMsgs = (ListView) findViewById(R.id.listPrivateMessages);
//listPrivateMsgs.setDrawingCacheEnabled(true);
listPrivateMsgs.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempPrivateMsgs));
//privateMsgsShrunkView = privateMsgsLayout.getDrawingCache();
if(privateMsgsShrunkView == null)
{
System.out.println("Private Messages View is null");
}
//listPrivateMsgs.setVisibility(View.GONE);
//Bookmarks
tempBookmarks = new ArrayList<String>();
tempBookmarks.add("First Bookmarks");
tempBookmarks.add("Second Bookmarks");
tempBookmarks.add("Third Bookmarks");
listBookmarks = (ListView) findViewById(R.id.listBookmarks);
//listBookmarks.setDrawingCacheEnabled(true);
listBookmarks.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tempBookmarks));
//bookmarksShrunkView = bookmarksLayout.getDrawingCache();
if(bookmarksShrunkView == null)
{
System.out.println("Bookmarks Messages View is null");
}
//listBookmarks.setVisibility(View.GONE);
imgBabble.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
System.out.println("Babble Clicked");
babbleShrunkView = babbleLayout.getDrawingCache();
villeShrunkView = villeLayout.getDrawingCache();
privateMsgsShrunkView = privateMsgsLayout.getDrawingCache();
bookmarksShrunkView = bookmarksLayout.getDrawingCache();
BabbleVilleShrinkView.addBitmap(0, resizeBitmap(200, 200, babbleShrunkView));
BabbleVilleShrinkView.addBitmap(1, resizeBitmap(200, 200, villeShrunkView));
BabbleVilleShrinkView.addBitmap(2, resizeBitmap(200, 200, privateMsgsShrunkView));
BabbleVilleShrinkView.addBitmap(3, resizeBitmap(200, 200, bookmarksShrunkView));
Gallery gallery = new Gallery(getApplicationContext());
gallery.setAdapter(new ImageAdapter(getApplicationContext()));
}
});
} // End of class
-
I was able to solve this by setting the setVisibility of all the listviews to Visible and then fetch the bitmap by getDrawingCache. – sunil Jun 01 '10 at 10:39