0
我在我的Fragment
類中有ListView
。這ListView
有三個項目,ListView
中的每個項目有兩個按鈕,btn +和btn-,以及一個TextView
。ListView中的OnClickListener更新列表中的不同項目
當我運行應用程序並在第一項中按btn +時,我不知道爲什麼,但最後一項中的TextView
的值遞增。當我點擊btn-時,最後的TextView
的值也會減少。
這是我的適配器:
package com.example.lie_.tablayout;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import java.util.ArrayList;
/**
* Created by Ali on 17/08/2017.
*/
public class MenuListPizzaAdapter extends BaseAdapter {
View vv;
TextView txtOrderNUM;
int counter=0;
private ArrayList <BuyMenuListPizza> BuyMenuListPizza;
private Context C;
public MenuListPizzaAdapter(Context c, ArrayList<BuyMenuListPizza> BuyMenuListPizza) {
this.BuyMenuListPizza = BuyMenuListPizza;
this.C=c;
}
@Override
public int getCount() {
return BuyMenuListPizza.size();
}
@Override
public Object getItem(int i) {
return BuyMenuListPizza.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int position, View View, ViewGroup parent)
{
if (View == null) {
View = LayoutInflater.from(C).inflate(R.layout.buy_menu_list_pizza, parent, false);
}
TextView txtitemname = (TextView)View.findViewById(R.id.txtitemname);
SimpleRatingBar ratingbar = (SimpleRatingBar)View.findViewById(R.id.ratingbar);
final TextView txtitemcontent = (TextView)View.findViewById(R.id.txtitemcontent);
ImageView btnOrderADD = (ImageView)View.findViewById(R.id.btnOrderADD);
ImageView btnOrderDEL = (ImageView)View.findViewById(R.id.btnOrderDEL);
ImageView imgitem = (ImageView)View.findViewById(R.id.imgitem);
txtitemname.setText(BuyMenuListPizza.get(position).getName());
ratingbar.setRating(BuyMenuListPizza.get(position).getRating());
txtitemcontent.setText(BuyMenuListPizza.get(position).getContents());
btnOrderADD.setImageResource(BuyMenuListPizza.get(position).getImgplus());
btnOrderDEL.setImageResource(BuyMenuListPizza.get(position).getImgminues());
imgitem.setImageResource(BuyMenuListPizza.get(position).getImgpizza());
Toast.makeText(parent.getContext(),"Position getView : " + position,Toast.LENGTH_LONG).show();
txtOrderNUM = (TextView)View.findViewById(R.id.txtOrderNUM);
txtOrderNUM.setText(BuyMenuListPizza.get(position).getCount() + "");
btnOrderADD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int coun= BuyMenuListPizza.get(position).getCount();
coun++;
txtOrderNUM.setText(coun+"");
}
});
btnOrderDEL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int coun= BuyMenuListPizza.get(position).getCount();
coun--;
if(coun<0){
}
else
txtOrderNUM.setText(coun+"");
}
});
return View;
}
}
這是我的片段類:
public class Pizza extends Fragment {
TextView txtOrderNUM;
ArrayList<BuyMenuListPizza> arrayList;
ListView MenuPizzaList;
Context c;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.pizza, container, false);
MenuPizzaList = (ListView)android.findViewById(R.id.MenuPizzaList);
arrayList=BuyMenuListPizzaCollection.getBuyMenuListPizza();
MenuPizzaList.setAdapter(new MenuListPizzaAdapter(container.getContext(),arrayList));
// MenuPizzaList.setOnItemClickListener(new oncitemlicklistener());
return android;
}
請不要說去xml文件和更改屬性,因爲我改變了我的代碼運行和OnClickListener
是好的,但它不能正常工作。
我搜索了幾天以上,但沒有找到任何東西。