从CustomListViewAdapter

时间:2018-05-12 05:55:44

标签: java android firebase-realtime-database

我希望通过从firebase数据库中检索数据来创建一个包含listview的片段。这个listview获取具有按钮和搜索条的卡片。我想要的是拥有一个全局变量来监听这些按钮。我尝试在片段本身中创建onClicklistener但是没有成功。然后我为适配器本身上的这些按钮创建了onClicklistener,它正在工作。现在,当我使用buttonclick创建Toast时,Toast字符串正如我所料。但问题是我希望这个Toasted字符串存储在全局变量之类的地方,以便我也可以在另一个片段中使用它。所以我用过:

 String alpha = ((MyApplication) getContext()).getCartitem();
                ((MyApplication)getContext()).setCartitem("XYZ");

在我的适配器类中单击侦听器本身但应用程序崩溃显示错误日志" 首先无法强制转换为com.fastfrooot.fastfrooot.MyApplication "。首先是我的包含Fragment和MyApplication的Activity是类扩展应用程序。

MyApplication.java

package com.fastfrooot.fastfrooot;

import android.app.Application;
import android.widget.Button;


public class MyApplication extends Application {
 private boolean[] cartsitem = {
  false,
  false,
  false,
  false,
  false,
  false
 };
 private String orderitem;
 private String pkname;
 private boolean oncomp = false;

 private Button[] cartbuttons = new Button[20];
 private String cartitem = "Alpha";



 public boolean[] getcartsitem() {
  return cartsitem;
 }

 public void setcartsitem(boolean[] cartsitem) {
  this.cartsitem = cartsitem;
 }

 public String getorderitem() {
  return orderitem;
 }

 public void setorderitem(String orderitem) {
  this.orderitem = orderitem;
 }

 public String getpkname() {
  return pkname;
 }

 public void setpkname(String pkname) {
  this.pkname = pkname;
 }

 public boolean getoncomp() {
  return oncomp;
 }

 public void setoncomp(boolean oncomp) {
  this.oncomp = oncomp;
 }

 public Button[] getcartbuttons() {
  return cartbuttons;
 }

 public void setCartbuttons(Button[] cartbuttons) {
  this.cartbuttons = cartbuttons;
 }

 public String getCartitem() {
  return cartitem;
 }

 public void setCartitem(String cartitem) {
  this.cartitem = cartitem;
 }


}
public class CustomListAdapterfir extends ArrayAdapter < Cardfir > {

 private static final String TAG = "CustomListAdapter";

 private Context mContext;
 private int mResource;
 private int lastPosition = -1;

 private int procount;
 String Cartkeitem;

 private static class ViewHolder {
  TextView title;
  ImageView image;
  TextView Status;
  Button cartbutton;
  SeekBar seekbar;
 }



 public CustomListAdapterfir(Context context, int resource, List < Cardfir > objects) {
  super(context, resource, objects);
  mContext = context;
  mResource = resource;





  //sets up the image loader library
  setupImageLoader();
 }

 @NonNull
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {



  //get the persons information
  final String title = getItem(position).getTitle();
  String imgUrl = getItem(position).getImgURL();
  final String Status = getItem(position).getStatus();
  Button cartbutton = getItem(position).getCartbutton();
  final SeekBar seekBar = getItem(position).getSeekbar();



  try {


   //create the view result for showing the animation
   final View result;

   //ViewHolder object
   final ViewHolder holder;

   if (convertView == null) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);
    holder = new ViewHolder();
    holder.title = (TextView) convertView.findViewById(R.id.cardTitle);
    holder.Status = (TextView) convertView.findViewById(R.id.cardstat);
    holder.image = (ImageView) convertView.findViewById(R.id.cardImage);
    holder.seekbar = (SeekBar) convertView.findViewById(R.id.seekBarf);
    holder.cartbutton = (Button) convertView.findViewById(R.id.Addbutton);




    result = convertView;

    convertView.setTag(holder);
   } else {
    holder = (ViewHolder) convertView.getTag();
    result = convertView;
   }



   lastPosition = position;

   holder.title.setText(title);
   holder.Status.setText(Status);

   holder.cartbutton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

     int seekerpos = holder.seekbar.getProgress() + 1;




     Cartkeitem = title + " " + String.valueOf(seekerpos);
     Toast.makeText(mContext, Cartkeitem, Toast.LENGTH_SHORT).show();
     String alpha = ((MyApplication) getContext()).getCartitem();
     ((MyApplication) getContext()).setCartitem("XYZ");



    }
   });

   holder.seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
   });

   //create the imageloader object
   ImageLoader imageLoader = ImageLoader.getInstance();

   int defaultImage = mContext.getResources().getIdentifier("@drawable/logo", null, mContext.getPackageName());


   DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
    .cacheOnDisc(true).resetViewBeforeLoading(true)
    .showImageForEmptyUri(defaultImage)
    .showImageOnFail(defaultImage)
    .showImageOnLoading(defaultImage).build();

   imageLoader.displayImage(imgUrl, holder.image, options);

   return convertView;
  } catch (IllegalArgumentException e) {
   Log.e(TAG, "getView: IllegalArgumentException: " + e.getMessage());
   return convertView;
  }

 }


 private void setupImageLoader() {

  DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
   .cacheOnDisc(true).cacheInMemory(true)
   .imageScaleType(ImageScaleType.EXACTLY)
   .displayer(new FadeInBitmapDisplayer(300)).build();

  ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
    mContext)
   .defaultDisplayImageOptions(defaultOptions)
   .memoryCache(new WeakMemoryCache())
   .discCacheSize(100 * 1024 * 1024).build();

  ImageLoader.getInstance().init(config);

 }
}
public class Tab1fragment extends Fragment implements View.OnClickListener {

 private static final String TAG = "TAG1 fragment";
 private ListView mListView;
 DatabaseReference Complete = FirebaseDatabase.getInstance().getReference();
 DatabaseReference Products = Complete.child("Products");
 ValueEventListener productlistener;
 final ArrayList < Cardfir > list = new ArrayList < Cardfir > ();
 String productname;
 String producttype;
 final ArrayList < Button > Cartbuttons = new ArrayList < Button > ();
 final ArrayList < SeekBar > Seekbars = new ArrayList < SeekBar > ();
 int productnumber = -1;
 Button[] Cartsbut = new Button[20];



 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.tab1_fragment, container, false);

  mListView = (ListView) view.findViewById(R.id.listview);



  productlistener = Products.addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {

    for (final DataSnapshot delphi: dataSnapshot.getChildren()) {

     productnumber = productnumber + 1;
     productname = delphi.child("Name").getValue().toString();
     producttype = delphi.child("Type").getValue().toString();
     list.add(new Cardfir("drawable://" + R.drawable.trans, productname, producttype));
    }

    CustomListAdapterfir adapter = new CustomListAdapterfir(getActivity(), R.layout.card_layout_liveorder, list);
    mListView.setAdapter(adapter);
    Products.removeEventListener(productlistener);

   }

   @Override
   public void onCancelled(DatabaseError databaseError) {

   }
  });



  return view;
 }

 @Override
 public void onClick(View view) {
  Toast.makeText(getContext(), "HI", Toast.LENGTH_SHORT).show();
 }
}

0 个答案:

没有答案