Android: ListView shows only first item. (Please refer image provided)
Android: ListView shows only first item. (Please refer image provided)
I'm trying to retrieve data from firebase into an ArrayList
and display the info on a ListView
. I used a custom view, but for some reason, only the first record is being displayed. I double checked by displaying the details using Toast
, and the other records are being shown there, but they aren't being displayed on ListView
.
ArrayList
ListView
Toast
ListView
Android ListView only shows the first item
CustomAdapter.java
class CustomAdapter extends ArrayAdapter<String>
private int layout;
public CustomAdapter(@NonNull Context context, int resource, @NonNull ArrayList<String> objects)
super(context, resource, objects);
layout = resource;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
final String details = getItem(position);
ViewHolder viewHolder;
ViewHolder mainViewHolder = null;
if(convertView == null)
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.dealInfo = convertView.findViewById(R.id.dealinfo);
viewHolder.accept = convertView.findViewById(R.id.accept);
viewHolder.accept.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(getContext(), details, Toast.LENGTH_SHORT).show();
);
convertView.setTag(viewHolder);
else
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.dealInfo.setText(details);
return convertView;
class ViewHolder
TextView dealInfo;
Button accept;
My item Layout for ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...
..
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dealinfo"
android:layout_width="282dp"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/dealinfo"
android:background="#000000"
android:text="Accept"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
My ListView xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:
...
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ManagerActivity">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentTop="true"
android:text="MANAGER"
android:textAlignment="center"
android:gravity="center"
..../>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_below="@+id/title"
android:layout_alignParentBottom="true"
android:background="#1fa781"
android:divider="#ffffff"
android:dividerHeight="6dp">
</ListView>
</RelativeLayout>
EDIT:
I removed the else
statement in getView()
and its working now. Why so?
This is what I did:
else
getView()
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
final String details = getItem(position);
ViewHolder viewHolder;
ViewHolder mainViewHolder = null;
if(convertView == null)
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
viewHolder = new ViewHolder();
viewHolder.dealInfo = convertView.findViewById(R.id.dealinfo);
viewHolder.accept = convertView.findViewById(R.id.accept);
viewHolder.accept.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(getContext(), details, Toast.LENGTH_SHORT).show();
);
convertView.setTag(viewHolder);
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.dealInfo.setText(details);
return convertView;
Check your return statement. You're always returning
convertView
even when you go into the else
branch. When you go into the else
branch you set the values for mainViewHolder
but then ignore that variable and return convertView
.– frozenkoi
Sep 18 '18 at 10:03
convertView
else
else
mainViewHolder
convertView
are your problem solved?
– Radesh
Oct 13 '18 at 12:04
Yes it's solved... Thanks!
– Anjali Nitish
Oct 13 '18 at 12:22
1 Answer
1
Try this and tell me what happend
final String details = getItem(position);
ViewHolder viewHolder;
ViewHolder mainViewHolder = null;
if(convertView == null)
LayoutInflater inflater = LayoutInflater.from(getContext());
//...
//others codes
//...
ViewHolder.dealInfo.setText(details);//ADD THIS LINE
convertView.setTag(viewHolder);
Your suggestion got my app to crash. But with my edit it's working. (Please check what I did in Edit.) I just removed the else clause... Not the else body, and its working
– Anjali Nitish
Sep 18 '18 at 9:32
oh, i'm sorry . check my edited code
– Radesh
Sep 18 '18 at 9:36
forget to change mainViewHolder to ViewHolder
– Radesh
Sep 18 '18 at 9:37
Yes it's working... Thanks
– Anjali Nitish
Oct 13 '18 at 12:23
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
I think you are missing the details to be setup on individual view items. dealinfo
– Rizwan
Sep 18 '18 at 8:23