【发布时间】:2019-05-16 05:21:20
【问题描述】:
我正在通过一个 while 循环,该循环对于我的适配器具有不同的 Strings。但它没有显示任何内容,但是如果我传递一个数组,它就可以工作。我不知道我是否只能传递一个String,以及覆盖方法的return是否正确。
public class ReportAdapter extends BaseAdapter {
private Context context;
private String debtor;
private String receiver;
private BigDecimal difference;
private String groupName;
LayoutInflater inflater;
public ReportAdapter(Context applicationContext, String debtor, String receiver, BigDecimal difference, String groupName) {
this.context = applicationContext;
this.debtor = debtor;
this.receiver = receiver;
this.difference = difference;
this.groupName = groupName;
inflater = LayoutInflater.from(applicationContext);
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return 0;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
DecimalFormat df = new DecimalFormat("##.00");
SharedPreferences sharedPrefs = context.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPrefs.edit();
convertView = inflater.inflate(R.layout.report_list_view, null);
TextView debtor = convertView.findViewById(R.id.debtor_tv);
TextView receiver = convertView.findViewById(R.id.receiver);
TextView difference = convertView.findViewById(R.id.difference_tv);
final CardView paidCard = convertView.findViewById(R.id.paid_cardview);
final Switch paidSwitch = convertView.findViewById(R.id.paid_switch);
debtor.setText(debtor.toString());
receiver.setText(receiver.toString());
difference.setText(String.valueOf(df.format(difference) + "€"));
paidSwitch.setChecked(sharedPrefs.getBoolean(groupName + "_checkValue" + position, false));
if (paidSwitch.isChecked()) {
// Set green background
paidCard.setCardBackgroundColor(Color.parseColor("#FF2E7D32"));
}
paidSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Set green background
paidCard.setCardBackgroundColor(Color.parseColor("#FF2E7D32"));
editor.putBoolean(groupName + "_checkValue" + position, isChecked);
editor.commit();
} else {
// Set red background
paidCard.setCardBackgroundColor(Color.parseColor("#FFB71C1F"));
editor.putBoolean(groupName + "_checkValue" + position, isChecked);
editor.commit();
}
}
});
return convertView;
}
列表视图没有出现。难道构造函数只接受 Array 类型?或者我可以通过String? getCount() 和 getItem() 方法必须返回 0?因为使用 ArrayList 我返回 arraylist.get(position),但使用 String,我应该返回什么?
【问题讨论】:
-
当然您可以将
String值传递给您的ListView。我的问题是:为什么要为单个条目使用ListView?这是很多开销。为什么ListView没有显示任何值是因为getCount()方法指定了列表中显示的行数。 -
@Barns 不,我需要显示一个动态列表视图..不是单个条目..我认为我可以使用字符串,但必须使用 ArrayList。谢谢!
标签: java android listview android-adapter