Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a sized ListView in activity - android

I am looking at this tutorial: http://developer.android.com/resources/tutorials/views/hello-listview.html

Is it possible to embed the list in my current activity so I can set its height. My layout looks like this:

<?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="@drawable/mobile_vforum_bg"
    >
    <ImageView 
        android:src="@drawable/mobile_vforum" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:id="@+id/logo"
        android:layout_marginLeft="57px"
        android:layout_marginTop="10px">
    </ImageView>
    <TextView 
        android:layout_height="30px"
        android:layout_width="fill_parent"
        android:id="@+id/welcomeText"
        android:textSize="16px"
        android:textColor="#317c73"
        android:background="#eeeeee"
        android:shadowColor="#333333"
        android:shadowDx="1"
        android:shadowDy="1"
        android:paddingTop="4px"
        android:paddingLeft="7px">
    </TextView>
    <ListView 
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"
        android:id="@+id/projectList">
    </ListView>
</LinearLayout>

You can see my ListView at the end of the layout. Basically I have an image and line of text and I want the list to reside below that. The tutorial link I posted extends ListActivity which is where I get confused since I am in my current layouts java file that extends Activity. Any help or suggestions is appreciated. Thanks.

like image 525
Ronnie Avatar asked Apr 09 '26 00:04

Ronnie


1 Answers

Yes, it is possible to do this. I've done it a few times, and my approach was to create a custom Adapter for the list by extending android.widget.BaseAdapter. This may be more than you need to solve your problem, but hopefully this gives you what you need.

Here is the main Activity layout:

<?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:padding="6dip"
    android:background="@drawable/backrepeat"
>
<Button
    android:id="@+id/cat_done_button"
    android:layout_width="75dip"
    android:layout_height="50dip"
    android:layout_marginTop="10dip"
    android:layout_marginLeft="6dip"
    android:text="@string/done"
/>
<EditText
    android:id="@+id/category_entry"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:hint="@string/category_hint"
    android:layout_margin="10dip"
    android:layout_below="@+id/cat_done_button"
/>
<ImageButton
    android:id="@+id/category_add_button"
    android:background="#00000000"
    android:src="@drawable/ic_menu_add_ld"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_marginLeft="5dip"
    android:scrollbars="vertical"
    android:fadingEdge="vertical"
    android:layout_alignBottom="@+id/category_entry"
    android:layout_toRightOf="@+id/category_entry"
/>
<ListView  
    android:id="@+id/category_list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dip"
    android:layout_marginBottom="5dip"
    android:background="#00000000"
    android:layout_below="@+id/category_notification"
/>
</RelativeLayout>

Here is the separate layout for each row in the list:

<?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:padding="6dip"
    android:background="#00000000"
>
<TextView  
    android:id="@+id/category_name"
    android:layout_width="200dip" 
    android:layout_height="wrap_content" 
    android:textSize="20sp"
    android:layout_marginTop="10dip"
    android:scrollbars="vertical"
    android:fadingEdge="vertical"
    android:layout_alignParentLeft="true"
    android:background="#00000000"
/>
<ImageButton
    android:id="@+id/category_delete_button"
    android:background="#00000000"
    android:src="@drawable/ic_menu_delete_ld"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:scrollbars="vertical"
    android:fadingEdge="vertical"
    android:layout_alignParentRight="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
/>
</RelativeLayout>

Here is how the main activity (CategoryActivity) sets up its List object:

private void setUpCategoryList() {
        categoryList = (ListView) this.findViewById(R.id.category_list);

        categoryAdapter = new CategoryAdapter(this, android.R.layout.simple_list_item_1,
                categoryManager.getCategoriesList());
        categoryList.setAdapter(categoryAdapter);
    }

Here is the custom CategoryAdapter:

public class CategoryAdapter extends BaseAdapter implements OnClickListener {

    private CategoryActivity catActivity;
    private List<String> categoryList;
    private final String CAT_DELETED = "Category has been deleted.";
    private final String DEFAULT_DELETE = "Cannot delete Default category.";
    private final String NON_EMPTY_DELETE = "Category has entries.  Entries re-assigned to Default category.";

    public CategoryAdapter(CategoryActivity catActivity, int resource, List<String> items) {
        this.catActivity = catActivity;
        categoryList = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String category = categoryList.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) catActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.category_row_layout, null);
        }

        TextView categoryView = (TextView) convertView.findViewById(R.id.category_name);
        categoryView.setText(category);

        ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.category_delete_button);
        deleteButton.setOnClickListener(this);
        deleteButton.setTag(category);

        return convertView;
    }

    @Override
    public void onClick(View view) {
//Do stuff here
}
like image 96
Geoff Avatar answered Apr 10 '26 20:04

Geoff