开发者

How can I set up an inline ListView?

开发者 https://www.devze.com 2023-02-08 22:22 出处:网络
I\'m just starting to develop on Android so this may be a very basic thing, but. . . I\'m having a really hard time setting up a ListView within a LinearLayout. Maybe it\'s a problem in my XML layout

I'm just starting to develop on Android so this may be a very basic thing, but. . .

I'm having a really hard time setting up a ListView within a LinearLayout. Maybe it's a problem in my XML layout, or maybe it's a problem with my databind开发者_如何学编程ing code.

I'm confused by the many tutorials that say that my Activity should extend ListActivity. Why would this one widget require that my entire UI be changed?

The thing is, I only want the scrolling box of items to occupy a part of this one screen. Is ListView the wrong widget to use? Should I be using a ScrollView instead?

Thanks -- here's my XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/enter_text"
        />

     <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1">

        <EditText 
            android:id="@+id/txtTo"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_weight="1"
            android:width="200px"/>
       </LinearLayout>
       <ListView android:id="@+id/listRecent"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />
  </LinearLayout>

And here's my code:

public class MyApp extends Activity {

private String TAG = "MyApp";

//UI ELEMENTS
EditText txtTo;
ListView listRecent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //GET REFERENCES TO UI ELEMENTS
    listRecent = (ListView) this.findViewById(R.id.listRecent);

    try {

        //----------------------------------------
        // create the grid item mapping
        String[] from = new String[] {"zero", "one", "two", "three"};
        int[] to = new int[] {0, 1, 2, 3};

        // prepare the list of all records
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i < 10; i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("rowid", "" + i);
            map.put("col_1", "col_1_item_" + i);
            map.put("col_2", "col_2_item_" + i);
            map.put("col_3", "col_3_item_" + i);
            fillMaps.add(map);
        }

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.recently_dingdonged, from, to);
        listRecent.setAdapter(adapter);
        //----------------------------------------

    }
    catch(Exception e) {
        Log.e(TAG, "Error here (3116019)", e);
    }

}


ListView should be nested inside of scrollview. By extending listActivity you gain the ability to setAdapter and populate the data into your list view through the main object and not through instantiating the list. This is by no means a requirement though.


You don't need to make your Activity extend ListActivity. You can, but it's not necessary.

In relation with your xml, try this out:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/enter_text"
        />

     <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">

        <EditText 
            android:id="@+id/txtTo"  
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_weight="0.5"
            android:width="200px"/>
       </LinearLayout>
       <ListView android:id="@+id/listRecent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5" />
  </LinearLayout>
0

精彩评论

暂无评论...
验证码 换一张
取 消