在 android 的列表视图中添加多个文本视图

add multiple textview in listview in android

这是我的适配器 class 我是 android 的初学者,我想使用视图持有者在列表视图中添加多个文本视图,并且还应该有一个按钮

   package com.example.navigationdrawerexample;

    /**
     * Created by Dev2 on 12/7/2015.
     */

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class listadapterfdgdfg extends ArrayAdapter<String> {
    customButtonListener customListner;

    public interface customButtonListener {
        public void onButtonClickListner(int position,String value);
    }

    public void setCustomButtonListner(customButtonListener listener)
    {
        this.customListner = listener;
    }

    private Context context;

    private ArrayList<String> data = new ArrayList<String>();
    private ArrayList<String> Price = new ArrayList<String>();

    public listadapterfdgdfg(Context context, ArrayList<String> dataItem,ArrayList<String> price) {
        super(context, R.layout.child_row,dataItem);
        this.data = dataItem;
        this.Price=price;
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        ViewHolder viewHolder2;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.child_row, null);
            viewHolder = new ViewHolder();
            viewHolder2 = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
            viewHolder.prices =(TextView)convertView.findViewById(R.id.pricemine);

            //viewHolder2.prices= (TextView)convertView.findViewById(R.id.pricemine);
            viewHolder.button = (Button) convertView.findViewById(R.id.childButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder2 = (ViewHolder) convertView.getTag();
        }
        final String temp = getItem(position);

        viewHolder.text.setText(temp);

        final String temp1 =getItem(position);

        viewHolder.prices.setText(temp1);

        viewHolder.button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (customListner != null) {
                    customListner.onButtonClickListner(position,temp);
                }
            }
        });

        return convertView;
    }

    public class ViewHolder {
        TextView text;
        TextView prices;
        Button button;
    }
}

这里我这样称呼这个class

我在 xml 中添加了价格的文本视图,还在列表适配器的构造函数中添加了价格数组列表,它在屏幕中显示了数据项的数据 它显示与价格相同的项目名称我知道这个问题但不知道如何解决这个问题 问题在这里

public listadapterfdgdfg(Context context, ArrayList<String>   dataItem,ArrayList<String> price) {
        super(context, R.layout.child_row,dataItem);
        this.data = dataItem;
        this.Price=price;
        this.context = context;
    }

它显示价格就像我声明价格数据它显示 itmename 就像我声明 itemlis 数据

想要添加物品的价格

这里是我告诉你的完整答案:

1- 数据的模型 class

public class MyData{
    private String name = "";
    private double price = 0;

    //Setters and Getters...
}

2- 适配器 class,重命名为 MyDataListAdapter

public class MyDataListAdapter extends ArrayAdapter<MyData> {
    private ArrayList<MyData> data = null;

    public MyDataListAdapter(Context context, ArrayList<MyData> dataList) {
        super(context, R.layout.child_row,dataList);
        this.data = dataList;
        this.context = context;
    }//constructor


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.child_row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.childTextView);
            viewHolder.prices =(TextView)convertView.findViewById(R.id.pricemine);
            viewHolder.button = (Button) convertView.findViewById(R.id.childButton);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //you can use data.get(position) too
        final MyData myDataItem = getItem(position);

        viewHolder.text.setText(myDataItem.getName());
        viewHolder.prices.setText(myDataItem.getPrice());
        viewHolder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (customListner != null) {
                    //pass name or price, or pass myDataItem object and handle it at onButtonClickListner()
                    customListner.onButtonClickListner(position,myDataItem.getName());
                }

            }
        });

        return convertView;
    }

    public class ViewHolder {
        TextView text;
        TextView prices;
        Button button;
    }
}//MyDataListAdapter

现在从activity或任何地方,实例化适配器

ArrayList<MyData> alldata = new ArrayList();
alldata.add(dataItem1); //.. add all items needed
MyDataListAdapter adapter = new MyDataListAdapter (context, alldata);
mListView.setAdapter(adapter);