Java 是否可以只向自定义listview适配器传递字符串?

Java 是否可以只向自定义listview适配器传递字符串?,java,android,listview,android-adapter,Java,Android,Listview,Android Adapter,我正在通过一个while循环,该循环对我的适配器具有不同的字符串。但是它没有显示任何内容,但是如果我传递一个数组,它就会工作。我不知道是否只能传递字符串,以及重写方法的返回是否正确 public class ReportAdapter extends BaseAdapter { private Context context; private String debtor; private String receiver; private BigDecimal difference; priva

我正在通过一个while循环,该循环对我的适配器具有不同的
字符串。但是它没有显示任何内容,但是如果我传递一个数组,它就会工作。我不知道是否只能传递
字符串
,以及重写方法的
返回是否正确

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

列表视图不显示。构造函数是否只接受数组类型?或者我可以传递一个
字符串
getCount()
getItem()
方法必须返回0?因为对于ArrayList,我返回
ArrayList.get(position)
,但是对于
字符串
,我应该返回什么?

当然,您可以将
字符串
值传递到
列表视图
。我的问题是:为什么对单个条目使用
ListView
ListView
会产生大量开销

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

一个更好的解决方案是创建一个自定义类——可能是
Transactions
,其中包含债务人和接收者的getter和setter。为要在
列表视图中显示的每一行添加一个新的
事务
数组列表
…即使它只是一行


可能的解决办法:

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;
    }
}
现在,您可以简单地填充列表:

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);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//当然有更好的方法来填充数据!!
ArrayList transactionList=新建ArrayList();
添加(新事务(“Mike”、“Bob”);
MyListAdapter theAdapter=新的MyListAdapter(此为arrayDriverListData);
ListView事务\u列表=(ListView)findViewById(R.id.transaction\u列表);
事务_list.setAdapter(适配器);
}
注意:

我在一个标准的文本编辑器中输入了这个(没有自动更正)。。。因此可能会有一些错误。

当然,您可以将
字符串
值传递给
列表视图
。我的问题是:为什么对单个条目使用
ListView
ListView
会产生大量开销

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

一个更好的解决方案是创建一个自定义类——可能是
Transactions
,其中包含债务人和接收者的getter和setter。为要在
列表视图中显示的每一行添加一个新的
事务
数组列表
…即使它只是一行


可能的解决办法:

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;
    }
}
现在,您可以简单地填充列表:

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);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//当然有更好的方法来填充数据!!
ArrayList transactionList=新建ArrayList();
添加(新事务(“Mike”、“Bob”);
MyListAdapter theAdapter=新的MyListAdapter(此为arrayDriverListData);
ListView事务\u列表=(ListView)findViewById(R.id.transaction\u列表);
事务_list.setAdapter(适配器);
}
注意:

我在一个标准的文本编辑器中输入了这个(没有自动更正)。。。因此可能会有一些错误。

当然,您可以将
字符串
值传递给
列表视图
。我的问题是:为什么对单个条目使用
ListView
?这是很大的开销。
ListView
为什么不显示任何值,是因为方法
getCount()
规定了列表中显示的行数。@Barns不,我需要显示动态ListView..不是一个条目..我想我可以使用字符串,但需要使用ArrayList。非常感谢。当然,您可以将
字符串
值传递给
列表视图
。我的问题是:为什么对单个条目使用
ListView
?这是很大的开销。
ListView
为什么不显示任何值,是因为方法
getCount()
规定了列表中显示的行数。@Barns不,我需要显示动态ListView..不是一个条目..我想我可以使用字符串,但需要使用ArrayList。非常感谢。