所以基本上我需要知道标题说什么,我把我的代码:
ListAdapter.java
public class ListAdapter extends BaseAdapter {
public ArrayList<Integer> quantità = new ArrayList<Integer>();
private String[] listViewItems, prezzi;
TypedArray images;
private Context context;
CustomButtonListener customButtonListener;
public ListAdapter(Context context, String[] listViewItems, TypedArray images, String[] prezzi) {
this.context = context;
this.listViewItems = listViewItems;
this.images = images;
this.prezzi=prezzi;
for(int i =0; i< listViewItems.length; i++)
{
quantità.add(0);
//quantity[i]=0;
}
}
public void setCustomButtonListener(CustomButtonListener customButtonListner)
{
this.customButtonListener = customButtonListner;
}
@Override
public int getCount() {
return listViewItems.length;
}
@Override
public String getItem(int position) {
return listViewItems[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row;
final ListViewHolder listViewHolder;
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.sis,parent,false);
listViewHolder = new ListViewHolder();
listViewHolder.tvFruitName = (TextView) row.findViewById(R.id.tvFruitName);
listViewHolder.ivFruit= (ImageView) row.findViewById(R.id.ivFruit);
listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvFruitPrice);
listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew);
listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity);
listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove);
row.setTag(listViewHolder);
}
else
{
row=convertView;
listViewHolder= (ListViewHolder) row.getTag();
}
listViewHolder.ivFruit.setImageResource(images.getResourceId(position, -1));
listViewHolder.tvPrices.setText(prezzi[position]);
try{
listViewHolder.edTextQuantity.setText(quantità.get(position) + "");
}catch(Exception e){
e.printStackTrace();
}
listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (customButtonListener != null) {
customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, + 1);
quantità.set(position,quantità.get(position) + 1);
}
}
});
listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (customButtonListener != null) {
customButtonListener.onButtonClickListener(position,listViewHolder.edTextQuantity,-1);
if(quantità.get(position)>0)
quantità.set(position, quantità.get(position) - 1);
}
}
});
listViewHolder.tvFruitName.setText(listViewItems[position]);
return row;
}
}
然后是 string.xml ,其中我放了我的数组
<resources>
<string name="app_name">Ristorante</string>
<string-array name="fruitName">
<item>Spaghetti allo scoglio</item>
<item>Cherry</item>
<item>Coconut</item>
<item>Grape Fruit</item>
<item>Lemon</item>
<item>Strawberry</item>
<item>Watermelon</item>
</string-array>
<string-array name="fruitImages">
<item>@mipmap/spaghettiscoglio</item>
<item>@mipmap/caffeshakerato</item>
<item>@mipmap/barbabietoleagrodolci</item>
<item>@mipmap/barbadifrate</item>
<item>@mipmap/bellinicocktail</item>
<item>@mipmap/pennefume</item>
<item>@mipmap/alettedipollo</item>
</string-array>
<string-array name="Price">
<item>10 $</item>
<item>12 $</item>
<item>15 $</item>
<item>24 $</item>
<item>3 $</item>
<item>50 $</item>
<item>5 $</item>
</string-array>
最后 Tab1.java ,它给我的错误: - listAdapter = new ListAdapter(this,arrItems,images,prezzi); - listAdapter.setCustomButtonListener(this);
public class Tab1 extends Fragment {
ListView listView;
ListAdapter listAdapter;
String[] arrItems, prezzi;
TypedArray images;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab1,container,false);
arrItems = getResources().getStringArray(R.array.fruitName);
images = getResources().obtainTypedArray(R.array.fruitImages);
prezzi = getResources().getStringArray(R.array.Price);
listView = (ListView) v.findViewById(R.id.customListView);
listAdapter = new ListAdapter(this,arrItems,images,prezzi);
listView.setAdapter(listAdapter);
listView.setScrollingCacheEnabled(false);
listAdapter.setCustomButtonListener(this);
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onButtonClickListener(int position,EditText editText, int value) {
/*
View view = listView.getChildAt(position);*/
int quantity = Integer.parseInt(editText.getText().toString());
quantity = quantity + 1 * value;
if(quantity<0)
quantity=0;
editText.setText(quantity + "");
}
}
答案 0 :(得分:0)
listAdapter.setCustomButtonListener(this);
- this
应指向CustomButtonListener
对象,但事实并非如此。如果CustomButtonListener
是接口,那么
public class Tab1 extends Fragment implements CustomButtonListener
下面:
listAdapter = new ListAdapter(this,arrItems,images,prezzi);
应该是
listAdapter = new ListAdapter(getActivity(),arrItems,images,prezzi);
因为您要传递Context