droidfish
droidfish

Reputation: 301

Advice for custom listview with title

i want to add a custom listview with an title aboce the list. I found some hints but could fix my problem. So maybe someone here can give a clue:

my class (includes two more classes)

package com.droidfish.apps.acli;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListProduct2 extends Activity {
private static ArrayList<String> sProducts;
private static String[] sPro;
private Integer iType;

private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return sPro.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.listitemdef);
            Log.d("Create item", sPro[position]);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(sPro[position]);

        return convertView;
    }

    static class ViewHolder {
        TextView text;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listproducts);
    savedInstanceState = this.getIntent().getExtras();
    sProducts = savedInstanceState.getStringArrayList("aircrafts");
    sPro = (String[]) sProducts.toArray(new String[0]);
    iType = savedInstanceState.getInt("type");
    ListView l1 = (ListView) findViewById(R.id.ListView01);
    TextView titleList = (TextView) findViewById(R.id.titleProductList);
    TextView titleList2 = new TextView(this);
    titleList2.setText("Manufacturer");
    l1.addHeaderView(titleList2);
    l1.setAdapter(new EfficientAdapter(this));

(this,android.R.layout.simple_list_item_1 , sPro)); }

private static final String[] country = { "Iceland", "India", "Indonesia",
        "Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia",
        "Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
        "Luxembourg" };

}

my layout for listview

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

<TextView
    android:id="@+id/titleProductList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Title" />
</LinearLayout>

layout the list item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5px"
android:paddingLeft="5px"
android:paddingTop="5px" >
<TextView
    android:id="@+id/listitemdef"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="hi"
    android:textColor="#FFFF00" >
</TextView>
</LinearLayout>

Any advice is welcome

Upvotes: 0

Views: 468

Answers (2)

dmon
dmon

Reputation: 30168

I see your problem, you're not specifying the orientation attribute of your LinearLayout, and the default is horizontal. So the textview is being added to the right of your listview. Also, the easiest way to create a title view is to use the layout_weight attribute. So, try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >  <!-- missing orientation-->
  <TextView
    android:id="@+id/titleProductList"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Title" />

  <ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" > <!-- weight attribute -->
</ListView>


</LinearLayout>

Upvotes: 0

Suchi
Suchi

Reputation: 10039

You could use the addHeaderView method described here

Upvotes: 1

Related Questions