只能将String传递给自定义列表视图适配器吗?

时间:2018-12-14 23:29:07

标签: java android listview android-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()方法必须返回0?因为使用ArrayList,我返回arraylist.get(position),但是使用String,我应该返回什么?

1 个答案:

答案 0 :(得分:2)

当然,您可以将String的值传递给ListView。我的问题是:为什么要对单个条目使用ListViewListView会产生大量开销。

为什么ListView不显示任何值,是因为方法getCount()指示列表中显示了多少行。

一个更好的解决方案是创建一个自定义类-也许Transactions包含用于债务人和接收人的获取器和设置器。对于要在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类可能如下所示:

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);
}

注意:
我在标准TextEditor中键入了此信息(没有自动更正)...因此可能存在一些错误。