?
Solved

Android Listview with button and textbox on same activity

Posted on 2011-10-08
11
Medium Priority
?
1,185 Views
Last Modified: 2016-02-19
Hi,

Complete newbie here to Android. Im using mono android to work with Android using C#. All set up and emulator running. Im tryin to get a listview to display below 2 other controls (Button and Text box) however whatever i try is not displaying the listview. All examples i have found show a simple listview but no other controls on the activity.

Is it possible to have a listview with other controls on same activity?

I have (i think) bound the list using a listadapter and pointed it to my listview but nothing is getting displayed
Main.amxl
---------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
      android:id="@+id/TrackSearch"
      android:text="Enter Track Number Here"
      android:layout_width="380px"
      android:layout_height="80px"    
  />

    <Button
      android:id="@+id/Trackbutton"
      android:text="Track"
      android:layout_width="100px"
      android:layout_height="80px"
      android:layout_alignParentRight="True"    
  />
   

  </LinearLayout>
  
  <ListView 
     android:id="@+id/Tracklist" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:layout_centerHorizontal="@+id/TrackSearch"
        />

</LinearLayout>
--------------------------------------------------------------
Activity (C#)
-------------
protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            
            //Set our view from the "main" layout resource
           SetContentView(Resource.Layout.Main);
           ListAdapter = new ArrayAdapter<string>(this, Resource.Id.Tracklist, ListOfTracks);

static readonly string[] TrackList= new String[] {"Track1","Track2","Track3"};           
           
        }

Open in new window

0
Comment
Question by:wilko100
  • 6
  • 5
11 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 36940919
you layout prevents listview to be shown, because of 'android:layout_height="fill_parent"' in the 1st sub-layout.

you can set layoutWeight to 1 for the listview OR set height for sub-layout as wrap_content
0
 

Author Comment

by:wilko100
ID: 36944492
Tried both suggestions however, still not returning anything. I then striped out all the other controls to make sure its not be being dumb and just left the list view in:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100px"
    android:layout_height="100px"
    android:padding="10dp"    
    android:id="@+id/Tracklist">
</ListView>

Still does not display anything. Any ideas why it want display my text list?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 36945644
the following is didsplayed perfectly well:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content">

    <EditText
      android:id="@+id/TrackSearch"
      android:text="Enter Track Number Here"
      android:layout_width="380px"
      android:layout_height="80px"    
  />

    <Button
      android:id="@+id/Trackbutton"
      android:text="Track"
      android:layout_width="100px"
      android:layout_height="80px"
      android:layout_alignParentRight="True"    
  />
   

  </LinearLayout>
  
  <ListView 
     android:id="@+id/Tracklist" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:layout_centerHorizontal="@+id/TrackSearch"
        android:layout_weight="1"/>

</LinearLayout>

Open in new window


I didn't try in Mono, but in Eclipse it is OK, so I reckon there is nothing to prevent it to be OK under Mono as well.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:wilko100
ID: 36948310
Ah so frustrating...copied xml in but still not displaying anything. Could it be my code at fault?


 ListAdapter = new ArrayAdapter<string>(this, Resource.Id.Tracklist, ListOfTracks);

static readonly string[] ListOfTracks= new String[] {"Track1","Track2","Track3"};
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 36951330
erm, and where do you assign this adapter to the list? usually it is the opposite in android - ie you define and fill adapter and then set list's adapter so it can fill the items
0
 

Author Comment

by:wilko100
ID: 36959066
Sorry, im not sure what you mean? I have my list adapter, is there something i missed?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 36962234
yes, you have your list adapter, but does your listview know about its existence?
0
 

Author Comment

by:wilko100
ID: 36964162

How do i do that? I thought the listadapter did that. Something like ListView = listadapter;
0
 

Author Comment

by:wilko100
ID: 36964172
Added this:

ListView lv = FindViewById<ListView>(Resource.Id.Tracklist);

           ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.Main, countries);

           lv.Adapter = ListAdapter;

 but still empty
0
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 2000 total points
ID: 36965783
ok, have a look at this one for MonoDroid: http://redth.info/2010/10/12/monodroid-custom-listadapter-for-your-listview

this sample - http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications - gives you some explanations as well (and you can ignore the statements like "This is very important because only a ListActivity will be able to display the ListView" :)), but the idea is this:

1) you inherit from ArrayAdapter and override getView() method which is called by Android to create individual item in the listview at given position
2) setAdapter() for your listview
0
 

Author Closing Comment

by:wilko100
ID: 37011789
Thanks for links. Gave me a chance to grasp the fundamentals
Cheers
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question