【问题标题】:Android ListView displays only first element of List<string passed to itAndroid ListView 仅显示传递给它的 List<string 的第一个元素
【发布时间】:2017-01-06 12:44:42
【问题描述】:
    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 来调用它 当我尝试为传递的列表的大小敬酒时,我得到了正确的大小 但是只有列表的第一项显示在屏幕上

我的 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>

【问题讨论】:

标签: android listview


【解决方案1】:
   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;
            }

【讨论】:

  • 我稍微编辑了我的答案!你试过编辑过的吗!
  • @HsRaja,感谢您的链接。我的问题是由嵌套滚动视图未填充视口引起的
  • 感谢大家的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 2020-03-24
  • 1970-01-01
相关资源
最近更新 更多