Android ListView仅显示List的第一个元素<string passed =“”to =“”it =“”

时间:2017-01-06 12:44:42

标签: android listview

=“”
     public class MyAdapter extends BaseAdapter implements ListAdapter {

    private List<String> events = new ArrayList<String>();
    private Context context;

    public MyAdapter(List<String> events, Context context) {
        this.events = events;
        this.context = context;
        String a = String.valueOf(events.size());
        Toast.makeText(context,a,Toast.LENGTH_LONG).show();
    }

    @Override
    public int getCount() {
        return events.size();
    }

    @Override
    public Object getItem(int position) {
        return events.get(position);
    }

    @Override
    public long getItemId(int position) {
        //return events.get(position).getId();
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.list_item_layout, null);
        }

        TextView listtv = (TextView) view.findViewById(R.id.label1);
        listtv.setText(events.get(position));

        Button delbutton = (Button) view.findViewById(R.id.del_button);

        delbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"You didn' code this feature yet",Toast.LENGTH_LONG).show();
            }
        });

        return view;
    }
}


  

我正在创建一个MyAdapter的对象,并通过传递List和Activity.this来调用它   当我尝试烘烤传递的列表的大小我得到适当的大小   但是只有列表的第一项显示在屏幕上

MY Xml`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00000000">

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/label1"
        android:padding="10dp"
        android:textSize="15dp"
        android:textStyle="bold"
        android:gravity="center"
    >

</TextView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/del_button"
        android:background="#00000000"
        android:drawableTop="@drawable/ic_close_black_24dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingTop="10dp"
        >
       </Button>
</RelativeLayout>`

我的主要布局文件是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_cal_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.test123.CalView"
    tools:showIn="@layout/activity_cal_view">
    <ListView
        android:id="@+id/list_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </ListView>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

   public static class ViewHolder{

        public TextView listTV;
        public Button delButton;

    }      



@Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View view = convertView;
                ViewHolder holder;

                if (view == null) {
                    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = layoutInflater.inflate(R.layout.list_item_layout, null);

        //Edited this part
holder = new ViewHolder
                holder.listTV = (TextView) view.findViewById(R.id.label1);

                holder.delbutton = (Button) view.findViewById(R.id.del_button);

     view.setTag(holder);
            } else holder=(ViewHolder)view.getTag();

                listtv.setText(events.get(position));

                delbutton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context,"You didn' code this feature yet",Toast.LENGTH_LONG).show();
                    }
                });

                return view;
            }