从另一个片段输入更新片段中的Listview

时间:2017-03-02 18:43:46

标签: android listview android-fragments fragment

我需要你的帮助来解决我的问题。我想在平板电脑设备上制作POS应用程序。我在这里有2个片段。我想在我的购物车中更新右侧片段中的listview。填充购物车的数据来自我的小键盘片段。因此,当我点击加号按钮时,购物车列表将使用我的输入更新数据。我的问题是数据无法更新,我的自定义适配器中出现错误null。请帮我。提前致谢。 UI RegisterFragment.Java

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.project.pos.posandroid.Adapter.CartAdapter;
import com.project.pos.posandroid.Adapter.Pager;
import com.project.pos.posandroid.Model.CartModel;
import com.project.pos.posandroid.R;
import com.project.pos.posandroid.Utils.CartPreference;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegisterFragment extends Fragment implements TabLayout.OnTabSelectedListener{

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private CartPreference cartPreference;
    List<CartModel> cart;
    private CartAdapter cartAdapter;
    ListView lvCart;

    public RegisterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_register, container, false);
        getActivity().setTitle("Register");

        tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText("Library"));
        tabLayout.addTab(tabLayout.newTab().setText("Numpad"));

        viewPager = (ViewPager) rootview.findViewById(R.id.pager);
        Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(this);

        cartPreference = new CartPreference();
        cart=cartPreference.getFavorites(getActivity());
        cartAdapter=new CartAdapter(getActivity(),cart);

        lvCart=(ListView) rootview.findViewById(R.id.lvCart);

        if(cart==null) {
            Toast.makeText(getActivity(), "No Data Available", Toast.LENGTH_SHORT).show();
        }
        else {
            if(cart.size()==0){
                Toast.makeText(getActivity(), "Your Cart Still Empty", Toast.LENGTH_SHORT).show();
            }
            else {
                int temp = 0, temp2 = 0;

                for (int i = 0; i < cart.size(); i++) {
                    temp = temp + (Integer.parseInt(cartAdapter.getItem(i).getHarga())* Integer.parseInt(cartAdapter.getItem(i).getQty()));
                }
            }
            refreshDisplay();
        }

        return rootview;
    }

    public void refreshDisplay() {

        cartAdapter= new CartAdapter(getActivity(), cart);
        lvCart.setAdapter(cartAdapter);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}

NumpadFragment.Java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.project.pos.posandroid.Model.CartModel;
import com.project.pos.posandroid.R;
import com.project.pos.posandroid.Utils.CartPreference;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NumpadFragment extends Fragment{
    private TextView txtScreen;
    private boolean stateError;
    private boolean lastNumeric;
    CartPreference cartPreference;
    Button nol,satu,dua,tiga,empat,lima,enam,tujuh,delapan,sembilan,clear,add;
    private RegisterFragment registerFragment;
    List<CartModel> cart;


    String curentText;


    public NumpadFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_numpad, container, false);
        // Inflate the layout for this fragment
        registerFragment = new RegisterFragment();
        cartPreference = new CartPreference();
        txtScreen = (TextView) rootview.findViewById(R.id.txtScreen);
        curentText = txtScreen.getText().toString();
        add = (Button) rootview.findViewById(R.id.btnAdd);
       registerFragment = new RegisterFragment();

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CartModel prod=new CartModel("Amount","0",txtScreen.getText().toString());
                cartPreference.addFavorite(getActivity(),prod);
            registerFragment.refreshDisplay();

            }
        });


        return rootview;
    }
}

CartAdapter.Java

public class CartAdapter extends ArrayAdapter<CartModel> {
private Context context;
List<CartModel> cartModels;
CartPreference cartPreference;

public CartAdapter(Context context, List<CartModel> cartModels){
    super(context, R.layout.list_cart,cartModels);
    this.context=context;
    this.cartModels=cartModels;
    cartPreference = new CartPreference();
}

private class ViewHolder {
    TextView nama;
    TextView qty;
    TextView harga;
}

@Override
public int getCount() {
    return cartModels.size();
}

@Override
public CartModel getItem(int position) {
    return cartModels.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_cart, null);
        holder = new ViewHolder();
        holder.nama = (TextView) convertView.findViewById(R.id.tvItemName);
        holder.qty= (TextView) convertView.findViewById(R.id.tvQty);
        holder.harga= (TextView) convertView.findViewById(R.id.tvPrice);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    CartModel product = (CartModel) getItem(position);
    holder.nama.setText(product.getNama());
    holder.qty.setText(product.getQty());
    holder.harga.setText(product.getHarga());

    return convertView;
}


/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(CartModel checkProduct) {
    boolean check = false;
    List<CartModel> favorites = cartPreference.getFavorites(context);
    if (favorites != null) {
        for (CartModel product : favorites) {
            if (product.equals(checkProduct)) {
                check = true;
                break;
            }
        }
    }
    return check;
}

@Override
public void add(CartModel product) {
    super.add(product);
    cartModels.add(product);
    notifyDataSetChanged();
}

@Override
public void remove(CartModel product) {
    super.remove(product);
    cartModels.remove(product);
    notifyDataSetChanged();
}

}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.project.pos.posandroid, PID: 3693
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                  at android.view.LayoutInflater.from(LayoutInflater.java:232)
                  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:181)
                  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:166)
                  at com.project.pos.posandroid.Adapter.CartAdapter.<init>(CartAdapter.java:0)
                  at com.project.pos.posandroid.Fragment.RegisterFragment.refreshDisplay(RegisterFragment.java:85)
                  at com.project.pos.posandroid.Fragment.NumpadFragment$12.onClick(NumpadFragment.java:215)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

0 个答案:

没有答案