Hossein Mobasher
Hossein Mobasher

Reputation: 4472

Filling ListView out

I'm working on project and main layout contains ListView. I want to dynamically add subItems to ListViewItem View. My main layout is as follows :

main_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="TextView"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >
    </ListView>
</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:background="@android:color/transparent" >

</LinearLayout>

And I want to add TextView to returned view in getView with following properties :

  1. Height : fill_parent
  2. Width : wrap_content
  3. Text : Text View Item

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) this.M_context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, parent, false);
    
        return convertView;
    }
    

Please note that, I want to dynamically add subItems to ListViewItem. You can suppose that, I have array of TextView and want to fill my listView using this Items. How can I do this scenario ? I can not find any result with searching on other threads and aslo Google. So, Could any one please help me to solve my problem ?

Thanks in Advance :)

Upvotes: 0

Views: 110

Answers (1)

rahul
rahul

Reputation: 6487

This was my getView() method.

main.xml

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@+id/button1"
    android:cacheColorHint="@android:color/transparent"
    android:divider="#00ff00"
    android:fastScrollEnabled="true"
    android:focusable="false"
    android:listSelector="#000000"
    android:paddingTop="5dp"
    android:saveEnabled="true" >
</ListView>

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Adapter

 class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        /*convertView = mInflater.inflate(R.layout.item, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
        holder = (ViewHolder) convertView.getTag();

        holder.getAppName().setText(str.get(position));*/

        if(position==0)
        {
            ImageView iv = new ImageView(getApplicationContext());
            iv.setBackgroundResource(R.drawable.ic_launcher);
            convertView = iv;
        }
        else if(position==1)
        {
            TextView tv = new TextView(getApplicationContext());
            tv.setText("text view");
            convertView = tv;
        }
        else
        {
            EditText et = new EditText(getApplicationContext());
            et.setText("ET go home");
            convertView = et;
        }

        return convertView;
    }

    private class ViewHolder {
        private View pathRow;
        private TextView appNameView = null;

        public ViewHolder(View row) {
            pathRow = row;
        }

        public TextView getAppName() {
            if (null == appNameView) {
                appNameView = (TextView) pathRow.findViewById(R.id.tv);
            }
            return appNameView;
        }
    }
}

The commented out portion is the one used to be. The code below it is the one which I used to dynamically generate the view and return them. my listitem.xml still has a textView in it. But the adapter sets the view which we are returning from getView(). This works for me.

I really don;t see the point of the LinearLayout in your item.xml. You can do that in your own code.

Upvotes: 1

Related Questions