【问题标题】:Is it possible to pass only String to a custom listview adapter?是否可以仅将字符串传递给自定义列表视图适配器?
【发布时间】: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 类型?或者我可以通过StringgetCount()getItem() 方法必须返回 0?因为使用 ArrayList 我返回 arraylist.get(position),但使用 String,我应该返回什么?

【问题讨论】:

  • 当然您可以将String 值传递给您的ListView。我的问题是:为什么要为单个条目使用ListView?这是很多开销。为什么ListView 没有显示任何值是因为getCount() 方法指定了列表中显示的行数。
  • @Barns 不,我需要显示一个动态列表视图..不是单个条目..我认为我可以使用字符串,但必须使用 ArrayList。谢谢!

标签: java android listview android-adapter


【解决方案1】:

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

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

更好的解决方案是创建一个自定义类——也许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 类可能如下所示:

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 中输入了这个(没有自动更正)......所以可能会有一些错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2011-12-31
    相关资源
    最近更新 更多