how can i write listview with header? example into

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Sep 14, 2018, 10:30:11 AM via Website

this is i have now, but i want it will be another, screen second picture.
how can i write this? this is my listview
image

image

Reply
  • Forum posts: 28

Sep 14, 2018, 4:38:07 PM via Website

image
First we have an interface that our two list item types will implement:

public interface Item {
public int getViewType();
public View getView(LayoutInflater inflater, View convertView);
}

Then we have an adapter that takes a list of Item

public class TwoTextArrayAdapter extends ArrayAdapter {
private LayoutInflater mInflater;

public enum RowType {
    LIST_ITEM, HEADER_ITEM
}

public TwoTextArrayAdapter(Context context, List<Item> items) {
    super(context, 0, items);
    mInflater = LayoutInflater.from(context);
}

@Override
public int getViewTypeCount() {
    return RowType.values().length;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getItem(position).getView(mInflater, convertView);
}

Then we have classes the implement Item and inflate the correct layouts. In your case you'll have something like a Header class and a ListItem class.

public class Header implements Item {
private final String name;

public Header(String name) {
    this.name = name;
}

@Override
public int getViewType() {
    return RowType.HEADER_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
    View view;
    if (convertView == null) {
        view = (View) inflater.inflate(R.layout.header, null);
        // Do some initialization
    } else {
        view = convertView;
    }

    TextView text = (TextView) view.findViewById(R.id.separator);
    text.setText(name);

    return view;
}

}

And then the ListItem class

public class ListItem implements Item {
private final String str1;
private final String str2;

public ListItem(String text1, String text2) {
    this.str1 = text1;
    this.str2 = text2;
}

@Override
public int getViewType() {
    return RowType.LIST_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
    View view;
    if (convertView == null) {
        view = (View) inflater.inflate(R.layout.my_list_item, null);
        // Do some initialization
    } else {
        view = convertView;
    }

    TextView text1 = (TextView) view.findViewById(R.id.list_content1);
    TextView text2 = (TextView) view.findViewById(R.id.list_content2);
    text1.setText(str1);
    text2.setText(str2);

    return view;
}

}
simple Activity to display it
public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<Item> items = new ArrayList<Item>();
    items.add(new Header("Header 1"));
    items.add(new ListItem("Text 1", "Rabble rabble"));
    items.add(new ListItem("Text 2", "Rabble rabble"));
    items.add(new ListItem("Text 3", "Rabble rabble"));
    items.add(new ListItem("Text 4", "Rabble rabble"));
    items.add(new Header("Header 2"));
    items.add(new ListItem("Text 5", "Rabble rabble"));
    items.add(new ListItem("Text 6", "Rabble rabble"));
    items.add(new ListItem("Text 7", "Rabble rabble"));
    items.add(new ListItem("Text 8", "Rabble rabble"));

    TwoTextArrayAdapter adapter = new TwoTextArrayAdapter(this, items);
    setListAdapter(adapter);
}

}
layout header

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    style="?android:attr/listSeparatorTextViewStyle"
    android:id="@+id/separator"
    android:text="Header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#757678"
    android:textColor="#f5c227" />

Helpful?
Reply

Recommended articles