开发者

Android Grid View databind

I have my own class arraylist and I need to bind that list and show only some fields to user. I have tried this by creating my own adapter class by extending BaseAdapter class. But I just showed only one field(firstname) I need to show more. here is my adapter class,

private class MyGriAdapter extends BaseAdapter{

        ArrayList<Doctor> data;

        public MyGriAdapter(ArrayList<Doctor> data){
            this.data = data;
        }
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView vv = new TextView(getApplicationContext());
           开发者_运维百科 vv.setTextColor(Color.BLACK);
            vv.setText(data.get(position).firstname);

            return vv;
        }

        @Override
        public Object getItem(int arg0) {
            return data.get(arg0);
        }

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

and my button click event I'm binding data (doctorResultList is the Doctor type arraylist),

GridView grid_main = (GridView)findViewById(R.id.GridView01);
MyGriAdapter grdAdapter = new MyGriAdapter(doctorResultList);
grid_main.setAdapter(grdAdapter);


The easiest way is to use an xml layout for your items, and use the adapter to inflate that item, and then change it's contents :

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            // Create new view
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.item, parent, false);
        }

        // Set information
        TextView titleView = (TextView)convertView.findViewById(R.id.title);
        titleView.setText(data.get(position).title);

        TextView otherView = (TextView)convertView.findViewById(R.id.other);
        titleView.setText(data.get(position).other);

        return convertView;
    }

When convertView is null, you create a new one, and then in every case you reuse that view. The xml layout could be something 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="wrap_content">

    <TextView android:id="@+id/title" android:layout_width="fill_parent"
        android:layout_height="match_parent" />

    <TextView android:id="@+id/other" android:layout_width="fill_parent"
        android:layout_height="match_parent" />
</LinearLayout>

This can be further optimized by keeping a reference on the inflater, and using the view's tag system to keep track of inner views, but that's a bit off topic... If you want to learn more about it, I recommend the Google IO conference World of ListView

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜