gpt4 book ai didi

java - 是否可以仅将 String 传递给自定义 ListView 适配器?

转载 作者:行者123 更新时间:2023-12-02 01:45:08 24 4
gpt4 key购买 nike

我正在经历一个 while 循环,它为我的适配器提供了不同的字符串。但它没有显示任何内容,但是如果我传递一个数组,它就可以工作。我不知道是否只能传递 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;
}

ListView 不出现。难道构造函数只接受Array类型?或者我可以传递一个StringgetCount()getItem() 方法必须返回 0?因为对于 ArrayList,我返回 arraylist.get(position),但是对于 String,我应该返回什么?

最佳答案

当然可以通过String值您的ListView 。我的问题是:为什么使用 ListView对于单个条目?一个ListView产生大量开销。

为什么 ListView没有显示任何值是因为方法 getCount()指示列表中显示的行数。

更好的解决方案是创建一个自定义类 - 也许 Transactions其中包含债务人和接收人的 getter 和 setter。添加新的TransactionsArrayList对于要显示在 ListView 中的每一行...即使只有一行。

<小时/>

可能的解决方案::

public class MyListAdapter extends ArrayAdapter<Transaction> {

private static final String TAG = MyListAdapter.class.getSimpleName();


public MyListAdapter(Context context, ArrayList<Transaction> transactionList) {
super(context, 0, transactionList);
}

@Override
public View getView(int position, View convertView, ViewGroup parent){

Transaction transactionData = getItem(position);

if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout_2, parent, false);
}

TextView textView1 = (TextView) convertView.findViewById(R.id.textView1);
TextView textView2 = (TextView) convertView.findViewById(R.id.textView2);


String debtor = transactionData.getDebtor();
String receiver = transactionData.getReceiver();
textView1.setText(debtor);
textView2.setText(receiver);

return convertView;
}
}

自定义Transaction类可能如下所示:

public class Transaction {

private String debtor = "";
private String receiver = "";

public Transaction(){
}
public Transaction(String debtor, String receiver){
this.debtor = debtor;
this.receiver = receiver;
}

public void setDebtor(String debtor){ this.debtor = debtor; }
public void setReceiver(String receiver){ this.receiver = receiver; }

public String getDebtor(){ return this.debtor; }
public String getReceiver() { return this.receiver; }
}

现在您可以简单地填充列表:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// There would of course be a better way to populate the data!!
ArrayList<> transactionList = new ArrayList<>();
transactionList.add(new Transaction("Mike","Bob"));


MyListAdapter theAdapter = new MyListAdapter(this, arrayDriverListData);

ListView transaction_list = (ListView) findViewById(R.id.transaction_list);

transaction_list.setAdapter(theAdapter);
}

注意:
我在标准文本编辑器中输入了此内容(无自动更正功能)...因此可能存在一些错误。

关于java - 是否可以仅将 String 传递给自定义 ListView 适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53788171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com