这是我的适配器类我在android中的初学者,我想在listview中使用视图持有者添加多个textview,并且还应该有一个按钮
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;
}
}
在这里我以这种方式称呼这个课程
我在xml中添加价格的textview也在listadapter的构造函数中添加价格数组列表它在screeen中显示dataitems的数据 它显示相同的项目名称作为价格,我知道问题,但不知道如何解决这个问题 问题出在这里
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;
}
它显示的价格好像我声明它显示的价格数据,就好像我声明了itemlis数据
要添加商品价格
答案 0 :(得分:0)
这里是我告诉你的完整答案:
1-数据的模型类
public class MyData{
private String name = "";
private double price = 0;
//Setters and Getters...
}
2-适配器类,重命名为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
现在从活动或任何地方,实例化适配器
ArrayList<MyData> alldata = new ArrayList();
alldata.add(dataItem1); //.. add all items needed
MyDataListAdapter adapter = new MyDataListAdapter (context, alldata);
mListView.setAdapter(adapter);