【问题标题】:Custom object with Custom Adapter使用自定义适配器的自定义对象
【发布时间】:2018-11-09 05:44:59
【问题描述】:

我正在尝试制作一个应用程序,它将借助自定义适配器在列表视图上绘制我的自定义 value_item 布局。我的 value_item 布局由 3 个水平对齐的文本视图组成。但是当我尝试在列表视图中绘制布局时,只显示第三个文本视图。我已经尝试了很多来找出问题所在,但我无法找到问题所在。 值适配器的代码如下:

public class ValueAdapter extends ArrayAdapter<Value>{


    public ValueAdapter(Context context, List<Value> values)
    {
        super(context,0,values);
    }

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

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(
                    R.layout.value_item, parent, false);
        }
        Value channel = getItem(position);

            TextView val = (TextView) listItemView.findViewById(R.id.val_view);
            val.setText(channel.getval()+" ");

            TextView mtime = (TextView) listItemView.findViewById(R.id.time_view);
            val.setText(channel.getTime());

            TextView mdate = (TextView) listItemView.findViewById(R.id.date_view);
            val.setText(channel.getDate());

        return listItemView;
    }
}

value_item 布局的代码是:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5"
    android:background="#424242"
    android:padding="8dp"
    android:layout_margin="3dp">

<TextView
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:textSize="25dp"
    tools:text="Hello"
    android:layout_marginLeft="10dp"
    android:id="@+id/val_view"
    />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:textSize="24dp"
        tools:text="24-12-2200"
        android:id="@+id/time_view"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:textSize="24dp"
        tools:text="24-12-2200"
        android:id="@+id/date_view"
        />
</LinearLayout>

【问题讨论】:

    标签: android xml listview arraylist custom-adapter


    【解决方案1】:

    您在此处设置val 上的所有文本。在这种情况下,您的最终语句将被执行。

    换个方式

    TextView val = (TextView) listItemView.findViewById(R.id.val_view);
                val.setText(channel.getval()+" ");
    
                TextView mtime = (TextView) listItemView.findViewById(R.id.time_view);
                mtime .setText(channel.getTime());
    
                TextView mdate = (TextView) listItemView.findViewById(R.id.date_view);
                mdate .setText(channel.getDate());
    

    希望它对你有用。

    【讨论】:

    • 没问题,快乐编码,编码时要注意。
    猜你喜欢
    • 2016-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多