在适配器中使用ViewHolder模式,我列出了ListView中的一些类别。代码工作正常,直到我不滚动ListView。要快速观察问题,请查看屏幕截图...
ListActivity.class
public class ListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Map<String, String> vehicleDetails = new LinkedHashMap<>();
vehicleDetails.put("Chassis Number", "UVXZ123TR3546");
vehicleDetails.put("Engine Number", "12332-WF1231");
vehicleDetails.put("Model Number", "MODEL ONE OF TRUCK");
vehicleDetails.put("Vehicle Type", "FARM TRUCK");
Map<String, String> dealerDetails = new LinkedHashMap<>();
dealerDetails.put("Dealer Name", "Mr. Abcd Pqrstuv");
dealerDetails.put("Dealer Contact Number", "1234567890");
dealerDetails.put("Dealer Contact Number 2", "0987654321");
Map<String, String> driverDetails = new LinkedHashMap<>();
driverDetails.put("Driver Name", "Mr. Xyz Abcdefg");
driverDetails.put("Driver Contact Number 1", "1234567890");
driverDetails.put("Driver Contact Number 2", "0987654321");
Map<String, String> yardDetails = new LinkedHashMap<>();
yardDetails.put("Yard Code", "YARD - 6");
yardDetails.put("Plant", "P-2134");
yardDetails.put("Storage Location", "1000");
yardDetails.put("Zone Code", "1");
yardDetails.put("Bay Number", "2");
Map<String, String> vehicleStatus = new LinkedHashMap<>();
vehicleStatus.put("Dispatch Date", "12 Jan 2016 00:00:00");
vehicleStatus.put("Estimated Delivery Date", "12 Feb 2016 00:00:00");
vehicleStatus.put("Delivery Date", "13 Feb 2016 00:00:00");
vehicleStatus.put("Status", "Delivered");
Map<String, Map<String, String>> data = new LinkedHashMap<>();
data.put("Vehicle Details", vehicleDetails);
data.put("Dealer's Details", dealerDetails);
data.put("Yard Details", yardDetails);
data.put("Driver's Details", driverDetails);
data.put("Vehicle Status", vehicleStatus);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new CategoryAdapter(this, R.layout.layout_category_list, data));
}
}
CategoryAdapter.class
public class CategoryAdapter extends ArrayAdapter<HashMap<String, HashMap<String, String>>> {
private final Activity context;
private final int layoutId;
private Map<String, Map<String, String>> data;
private Map<String, String> map;
public CategoryAdapter(Activity context, int layoutId, Map<String, Map<String, String>> data) {
super(context, layoutId);
this.context = context;
this.layoutId = layoutId;
this.data = data;
this.map = new LinkedHashMap<>();
}
@Override
public int getCount() {
return data.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(layoutId, parent, false);
viewHolder = new ViewHolder();
viewHolder.table = (TableLayout) convertView.findViewById(R.id.table);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String title = new ArrayList<>(data.keySet()).get(position);
viewHolder.txtTitle.setText(title);
map.clear();
map = data.get(title);
ArrayList<String> keys = new ArrayList<>(map.keySet());
for (int i = 0; i < keys.size(); i++) {
TableRow row = new TableRow(context);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(4, 10, 4, 10);
TextView txtKey = new TextView(context);
txtKey.setTextSize(17);
txtKey.setTextColor(Color.BLACK);
txtKey.setText(keys.get(i));
TextView txtValue = new TextView(context);
txtValue.setTextSize(15);
txtValue.setText(map.get(keys.get(i)));
linearLayout.addView(txtKey);
linearLayout.addView(txtValue);
row.addView(linearLayout);
viewHolder.table.addView(row);
}
return convertView;
}
private static class ViewHolder {
private TableLayout table;
private TextView txtTitle;
}
}
category_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/cardView"
style="@style/DetailCardView"
android:layout_margin="5dp"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView android:id="@+id/txtTitle" style="@style/DetailGroupTitleText" android:text="TITLE" />
</TableRow>
</TableLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
现在问题出在getView()
方法中。它给出了意想不到的结果我的意思是车辆详细信息随附的车辆详细信息或车辆状态列出了车辆状态等等。只有在我滚动ListView时才会发生这种情况,否则一切都很好。
请帮帮我怎样才能解决这个问题呢?
Downvoters!请保持冷静,我真的需要帮助...
答案 0 :(得分:0)
尝试这样的方法,复制并粘贴/替换您的代码。
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
final LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(layoutId, parent, false);
viewHolder = new ViewHolder();
viewHolder.table = (TableLayout) convertView.findViewById(R.id.table);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String title = new ArrayList<>(data.keySet()).get(position);
viewHolder.txtTitle.setText(title);
map.clear();
map = data.get(title);
final ArrayList<String> keys = new ArrayList<>(map.keySet());
for (int i = 0; i < keys.size(); i++) {
final TableRow row = new TableRow(context);
final LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(4, 10, 4, 10);
final TextView txtKey = new TextView(context);
txtKey.setTextSize(17);
txtKey.setTextColor(Color.BLACK);
txtKey.setText(keys.get(i));
final TextView txtValue = new TextView(context);
txtValue.setTextSize(15);
txtValue.setText(map.get(keys.get(i)));
linearLayout.addView(txtKey);
linearLayout.addView(txtValue);
row.addView(linearLayout);
viewHolder.table.addView(row);
}
return convertView;
}
希望这有帮助。
答案 1 :(得分:0)
您需要知道getView
每次ListView
调用滚动后需要获取新项目。这意味着您的循环会重复填充新视图而不会清除旧视图。我想这是你问题的原因,所以你应该从结果中清除TableRow
,如果它包含在那里。