带有自定义适配器的自定义对象
Custom object with Custom Adapter
我正在尝试制作一个应用程序,它将在自定义适配器的帮助下在列表视图上绘制我的自定义 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>
此处您将在 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());
希望对你有用。
我正在尝试制作一个应用程序,它将在自定义适配器的帮助下在列表视图上绘制我的自定义 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>
此处您将在 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());
希望对你有用。