是否可以仅将 String 传递给自定义列表视图适配器?
Is it possible to pass only String to a custom listview adapter?
我正在经历一个 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()
方法必须 return 0?因为使用 ArrayList 我 return arraylist.get(position)
,但是使用 String
,我应该 return?
当然可以将 String
值传递给 ListView
。我的问题是:为什么对单个条目使用 ListView
? ListView
会产生大量开销。
为什么 ListView
没有显示任何值是因为方法 getCount()
规定了列表中显示的行数。
更好的解决方案是创建自定义 class--也许 Transactions
,其中包含债务人和接收人的 getter 和 setter。将新的 Transactions
添加到 ArrayList
,以便在 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
class 可能如下所示:
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);
}
注意:
我在标准文本编辑器中输入了这个(没有自动更正)...所以可能会有一些错误。
我正在经历一个 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()
方法必须 return 0?因为使用 ArrayList 我 return arraylist.get(position)
,但是使用 String
,我应该 return?
当然可以将 String
值传递给 ListView
。我的问题是:为什么对单个条目使用 ListView
? ListView
会产生大量开销。
为什么 ListView
没有显示任何值是因为方法 getCount()
规定了列表中显示的行数。
更好的解决方案是创建自定义 class--也许 Transactions
,其中包含债务人和接收人的 getter 和 setter。将新的 Transactions
添加到 ArrayList
,以便在 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
class 可能如下所示:
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);
}
注意:
我在标准文本编辑器中输入了这个(没有自动更正)...所以可能会有一些错误。