I have a ListView with an adapter. My problem is that I don't receive the last position from the getView() function and instead receive the second last position twice. What could be the issue?
Find below the relevant parts of the adapter.
static class ItemViewHolder {
private RelativeLayout messageRow;
private LinearLayout messageContainer;
private TextView messageText;
private TextView messageSentTime;
private TextView PictureReportTextView;
private ImageView messageState;
private ImageView messagePicture;
private WebView messageGifView;
private ProgressBar messageProgressbar;
private int position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
final ItemViewHolder subViews;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.message_list_item, parent, false);
subViews = new ItemViewHolder();
subViews.messageRow = (RelativeLayout) rowView.findViewById(R.id.message_row);
subViews.messageContainer = (LinearLayout) rowView.findViewById(R.id.message_container);
subViews.messageText = (TextView) rowView.findViewById(R.id.message_text);
subViews.messageSentTime = (TextView) rowView.findViewById(R.id.message_sent_time);
subViews.messagePicture = (ImageView) rowView.findViewById(R.id.message_picture);
subViews.messageProgressbar = (ProgressBar) rowView.findViewById(R.id.message_picture_progress_bar);
subViews.PictureReportTextView = (TextView) rowView.findViewById(R.id.message_picture_report);
subViews.messageState = (ImageView) rowView.findViewById(R.id.message_state);
rowView.setTag(subViews);
} else {
subViews = (ItemViewHolder) rowView.getTag();
}
final MessageModel messageObj = getValues().get(position);
subViews.position = position;
subViews.messagePicture.setVisibility(View.GONE);
subViews.messageProgressbar.setVisibility(View.GONE);
subViews.PictureReportTextView.setVisibility(View.GONE);
subViews.messagePicture.imageURl = "";
if (messageObj.getMessage().equals("")) {
subViews.messageText.setVisibility(View.GONE);
subViews.messageText.setText("");
} else {
subViews.messageText.setVisibility(View.VISIBLE);
subViews.messageText.setText(messageObj.getMessage());
}
Log.d(TAG, "for position: " + position + " - " + messageObj.getMessage());
Log.d(TAG, "for position: " + position + " - " + messageObj.getMessageId());
subViews.messageSentTime.setText(DateTimeUtils.stringForMessageListDate(messageObj.getSentTime()));
if (messageObj.getMessagePicture().equals("") || messageObj.getMessagePicture().equals("null")) {
subViews.messagePicture.setVisibility(View.GONE);
subViews.PictureReportTextView.setVisibility(View.GONE);
subViews.messageContainer.setMinimumWidth(0);
subViews.messageContainer.setMinimumHeight(0);
if (subViews.messageGifView != null) {
subViews.messageGifView.setVisibility(View.GONE);
subViews.messageGifView.loadUrl("about:blank");
}
} else {
subViews.PictureReportTextView.setVisibility(View.VISIBLE);
subViews.messagePicture.setVisibility(View.VISIBLE);
loadMessagePicture(messageObj, subViews, false);
}
if (messageObj.getFlagged() == 1) {
subViews.messageText.setText(reportString);
subViews.messageText.setVisibility(View.VISIBLE);
subViews.PictureReportTextView.setVisibility(View.GONE);
if (subViews.messageGifView != null) {
subViews.messageGifView.setVisibility(View.GONE);
}
subViews.messagePicture.setVisibility(View.GONE);
subViews.messageSentTime.setVisibility(View.GONE);
} else {
subViews.messageSentTime.setVisibility(View.VISIBLE);
}
subViews.messageRow.setMinimumWidth(0);
return rowView;
}
And the message_list_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/message_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
>
<LinearLayout
android:id="@+id/message_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp">
<ProgressBar
android:id="@+id/message_picture_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
/>
<ImageView
android:id="@+id/message_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:maxHeight="300dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/message_picture"
android:layout_weight="1"
android:textSize="17sp"
android:textIsSelectable="true"
android:textColor="@color/black_lighter" />
</LinearLayout>
<TextView
android:id="@+id/message_sent_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="bottom"
android:layout_below="@+id/message_container"
android:layout_alignStart="@+id/message_container"
android:layout_alignLeft="@+id/message_container"
android:textSize="10sp"
android:singleLine="true"
android:maxLines="1"
android:gravity="bottom"
android:textColor="@color/grey"
android:textStyle="normal" />
<ImageView
android:id="@+id/message_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/message_container"
android:layout_toRightOf="@+id/message_sent_time"
android:visibility="gone"
/>
<TextView
android:id="@+id/message_picture_report"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_alignTop="@+id/message_sent_time"
android:layout_alignRight="@+id/message_container"
android:text="Report"/>
</RelativeLayout>
Logs: My List has 49 objects. I get this on going to the activity. Notice it starts with position 48.
for position: 48 - hi
for position: 48 - 43802939
for position: 48 - hi
for position: 48 - 43802939
for position: 47 - hey
for position: 47 - 43791969
for position: 46 - hey
for position: 46 - 43791349
for position: 45 - haha
for position: 45 - 43782981
for position: 44 - ha
for position: 44 - 43782875
for position: 43 - hello
for position: 43 - 43782725
for position: 42 - hello
for position: 42 - 43782132
for position: 41 - name
for position: 41 - 43781951
On scrolling above a bit and immediately coming down again, I receive the position 49.
for position: 40 - lol
for position: 40 - 43756370
for position: 40 - lol
for position: 40 - 43756370
for position: 49 - hehehe
for position: 49 - 43803671
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.