ListView自定义Adapter的position始终为0,仅显示首项问题求助
Hey there, let's fix your ListView issue step by step. Looking at your code and logs, there are a couple of key problems causing your ListView to only show the first item:
1. Incorrect View Type Configuration in CustomAdapter
Your CustomAdapter is treating every list item as a unique view type, which breaks ListView's recycling mechanism and prevents it from rendering items beyond position 0.
What's wrong:
getItemViewType(int position)returnsposition, meaning each item is marked as a distinct view type.getViewTypeCount()returnsrecords.size(), which varies with your data set.- This confuses ListView into thinking it only needs to handle the first item's view type.
Fix:
Since all your items use the samelist_row.xmllayout, you only need one view type. Update these methods inCustomAdapter.java:@Override public int getItemViewType(int position) { return 0; // All items share the same view type } @Override public int getViewTypeCount() { return 1; // Only one type of view to handle }
2. ListView Inside ScrollView Layout Conflict
Placing a ListView inside a ScrollView with android:layout_height="wrap_content" causes measurement issues. The ListView will only calculate the height of the first item and won't expand to show others.
- Fix (Recommended):
Remove the outerScrollViewentirely. Use aLinearLayoutwith vertical orientation, and set the ListView's height to0dpwithlayout_weight="1"—this lets the ListView take all remaining space and scroll properly:<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:stretchColumns="*" > <!-- Your existing TableRows go here --> </TableLayout> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider = "#000" android:dividerHeight = "1dp" android:footerDividersEnabled = "false" android:headerDividersEnabled = "false"> </ListView> </LinearLayout>
3. Broken View Recycling in getView()
Your current getView() only sets text when creating a new view. When views are recycled (for positions 1 and 2), they'll keep the first item's data. Fix this to update content every time getView() is called:
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; final Record record = (Record) records.get(position); if(convertView == null) { LayoutInflater layoutInflater = LayoutInflater.from(context); convertView = layoutInflater.inflate(R.layout.list_row, parent, false); holder = new ViewHolder(); holder.date = convertView.findViewById(R.id.txtDate); holder.counter = convertView.findViewById(R.id.txtCounter); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // Update text for every item, not just new views holder.date.setText(String.valueOf(record.getDate())); holder.counter.setText(String.valueOf(record.getCounter())); return convertView; } // Add a ViewHolder to optimize view lookup static class ViewHolder { TextView date; TextView counter; }
Bonus: Fix the getItem() Method
Your adapter's getItem() returns the position instead of the actual Record object—fix that for proper data handling:
@Override public Object getItem(int position) { return records.get(position); }
After applying these changes, your ListView should display all 3 items correctly.
内容的提问来源于stack exchange,提问作者helyanti

