Search code examples
androidxmllistviewarraylistcustom-adapter

Custom object with Custom Adapter


I am trying to make an app, which will plot my custom value_item layout on the listview with help of custom adapter. My value_item layout consist of 3 textviews aligned horizontally. But when I try to plot the layout in the listview, only the third textview get displayed. I have tried a lot to figure out the problem , but I am not able to catch the issue location. Code for the value Adapter is given below:

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;
    }
}

And Code for the value_item layout is:

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

Solution

  • Here you are setting all text on val. in this case your final statement is executed.

    change like

    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());
    

    Hope it works for you.