Adapter Error Android Studio中的AlertDialog

时间:2019-04-18 04:54:14

标签: android

我遇到一些错误..我有2个recyclerview ..当我单击一个项目时,它会转到另一个recyclerview..i当在第二个recyclerview上单击一个项目时,我会在第二个recyclerview上发出警报对话框。一个项目已经在第二个recyclerview中工作了,但是当我尝试从第一个recyclerview中添加新项目到第二个recyclerview时,它运行良好,直到我单击我刚刚添加的第二个recyclerview上的项目,我得到了这个错误- ->

java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。

如果第二个recyclerview中已存在该项目,我不会收到错误。当我向第二个recyclerview中添加新项目并单击以显示alerdialog时,错误就会出现。

通过我的MainActivity使用Tablet..i的那两个recyclerview..im尝试将getapplicationcontext更改为Mainactivity并更改清单 apptheme,但仍然相同。.

我的manifest.xml

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".Calculator"
        android:theme="@style/ItemDetailDialog"
        android:windowSoftInputMode="stateHidden" />
    <activity
        android:name=".PaymentMethod"
        android:theme="@style/ItemDetailDialog"
        android:windowSoftInputMode="stateHidden" />
    <activity
        android:name=".CloseCounterActivity"
        android:theme="@style/ItemDetailDialog"
        android:windowSoftInputMode="stateHidden" />
    <activity
        android:name=".CashOutActivity"
        android:theme="@style/ItemDetailDialog"
        android:windowSoftInputMode="stateHidden" />
    <activity android:name=".ListCashOut" />
    <activity
        android:name=".CashInActivity"
        android:theme="@style/ItemDetailDialog"
        android:windowSoftInputMode="stateHidden" />
    <activity android:name=".ListCashIn" />
    <activity android:name=".MainActivity"/>
    <activity android:name=".LoyaltyActivity" />
    <activity android:name=".CashierScanActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />
</application>

和我的适配器

public class AdapterItemPurchase extends RecyclerView.Adapter<AdapterItemPurchase.ItemPurchaseViewHolder> {

private Context ctx;
private LayoutInflater inflater;
private ArrayList<Product_Item> itemsList;
//SQLite
DatabaseHelper myDb;

public AdapterItemPurchase(Context ctx, ArrayList<Product_Item> itemsList){
    inflater = LayoutInflater.from(ctx);
    this.ctx = ctx;
    this.itemsList = itemsList;
}

public class ItemPurchaseViewHolder extends RecyclerView.ViewHolder{

    public TextView nameText, qtyText, priceText;
    public RelativeLayout viewBackground,viewForeground;
    public CardView cardView;

    public ItemPurchaseViewHolder(@NonNull View itemView) {
        super(itemView);

        nameText = itemView.findViewById(R.id.txtItemList);
        qtyText = itemView.findViewById(R.id.txtQty2);
        priceText = itemView.findViewById(R.id.txtPriceItem);
        viewBackground = itemView.findViewById(R.id.view_background);
        viewForeground = itemView.findViewById(R.id.view_foreground);
        cardView = itemView.findViewById(R.id.recycler_item_list_pay);
    }

}

@NonNull
@Override
public ItemPurchaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = inflater.inflate(R.layout.recycler_view_pay_list_item, viewGroup, false);
    myDb = new DatabaseHelper(ctx);
    return new ItemPurchaseViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ItemPurchaseViewHolder holder, int position) {
    final Product_Item item = itemsList.get(position);
    final int qty = item.getItemQty();

    holder.nameText.setText(item.getItemDescription());
    holder.qtyText.setText(String.valueOf(item.getItemQty()));
    holder.priceText.setText(String.valueOf(item.getItemAmt()));
    holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showUpdateDialog(qty);

            return true;
        }

    });
}

private void showUpdateDialog(final int qty) {
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(ctx);
    View mView = inflater.inflate(R.layout.popup_update_qty, null);
    final TextView txt_popupQty = mView.findViewById(R.id.txt_popupQty);
    final ImageButton btn_popupMinus = mView.findViewById(R.id.btn_popupMinus);
    final ImageButton btn_popupPlus = mView.findViewById(R.id.btn_popupPlus);
    final Button btn_popupUpdate = mView.findViewById(R.id.btn_popupUpdate);

    txt_popupQty.setText(String.valueOf(qty));

    btn_popupMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int newQty = Integer.parseInt(txt_popupQty.getText().toString());
            if (newQty <=1){
                txt_popupQty.setEnabled(false);
            }else {
                newQty--;
                txt_popupQty.setEnabled(true);
                txt_popupQty.setText(String.valueOf(newQty));
            }
        }
    });

    btn_popupPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int newQty = Integer.parseInt(txt_popupQty.getText().toString());
            newQty++;
            txt_popupQty.setText(String.valueOf(newQty));

        }
    });

    btn_popupUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    mBuilder.setView(mView);
    AlertDialog dialog = mBuilder.create();
    dialog.show();
}

@Override
public int getItemCount() {
    return itemsList.size();
}

public void removeItem(int position){
    myDb.deleteitemPurchased(itemsList.get(position).getItemId());
    itemsList.remove(position);
    notifyItemRemoved(position);
}


public void updateMyData(ArrayList<Product_Item> mItemList){
    this.itemsList = mItemList;
}

}

和我的MainActivity.java

public class MainActivity extends AppCompatActivity implements RecyclerItemTouchHelperListener {

Button CashIn;
Button CashOut;
Button CloseCounter;
Button Loyalty;
Button btnPay;
ImageButton iBtnRefreshItem;
ProgressDialog mRefreshDialog;

private RecyclerView rvGroup, rvItem;
public  static RecyclerView rvList;
private AdapterItem adapterItem;
private AdapterItemGroup adapterGroup;
private static AdapterItemPurchase adapterItemPurchase;

private static final String CONNECTION_PREFS = "CONNECTION_PREFS";

public static ArrayList<Product_Group> groupList;
public static ArrayList<Product_Item> itemList;
public static ArrayList<Product_Item> itemPurchasedList;

//SQLite
DatabaseHelper myDb;

int width = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CashIn  = findViewById(R.id.CashIn);
    CashOut  = findViewById(R.id.CashOut);
    CloseCounter  = findViewById(R.id.CloseCounter);
    iBtnRefreshItem = findViewById(R.id.imgBtnRefreshItem);

    myDb = new DatabaseHelper(this);

    rvList = findViewById(R.id.recycler_ViewList);
    LinearLayoutManager mListManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    rvList.setHasFixedSize(true);
    rvList.setLayoutManager(mListManager);
    rvList.setItemAnimator(new DefaultItemAnimator());
    rvList.setAdapter(adapterItemPurchase);

    ItemTouchHelper.SimpleCallback itemTouchHelperCallback
            = new RecyclerItemTouchHelper(0,ItemTouchHelper.LEFT,this);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(rvList);

    rvGroup = findViewById(R.id.recyclerviewGroup);
    LinearLayoutManager mGroupManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    rvGroup.setHasFixedSize(true);
    rvGroup.setLayoutManager(mGroupManager);
    rvGroup.setAdapter(adapterGroup);

    rvItem = findViewById(R.id.recyclerviewItem);
    RecyclerView.LayoutManager mItemsManager = new GridLayoutManager(getApplication(), 5);
    rvItem.setHasFixedSize(true);
    rvItem.setLayoutManager(mItemsManager);
    rvItem.setAdapter(adapterItem);

    final LinearLayout layout = findViewById(R.id.layoutItem);
    ViewTreeObserver observer = layout.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            width  = layout.getMeasuredWidth();
            width = width / 5;
        }
    });

    CashIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),CashInActivity.class);
            startActivity(intent);
        }
    });

    CloseCounter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),CloseCounterActivity.class);
            startActivity(intent);
        }
    });

    CashOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),CashOutActivity.class);
            startActivity(intent);
        }
    });

    //Loyalty declare
    Loyalty  = findViewById(R.id.Loyalty);

    Loyalty.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),LoyaltyActivity.class);
            startActivity(intent);
        }
    });

    //Payment Method declare
    btnPay  = findViewById(R.id.btnPay);

    btnPay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),PaymentMethod.class);
            startActivity(intent);
        }
    });

    iBtnRefreshItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRefreshDialog = ProgressDialog.show(MainActivity.this, "", "Refreshing item list", true, false);

            myDb.deleteTableItem();

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    getItemGroup();
                    getItem();

                    if (mRefreshDialog.isShowing()) {
                        mRefreshDialog.dismiss();

                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                displayProductionItem();
                            }
                        }, 500);
                    }
                }
            }, 1500);
        }
    });

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            displayProductionItem();
        }
    }, 500);

    displayItemPurchased(MainActivity.this);
}

private void getItemGroup() {
    //get webservice ip
    SharedPreferences sharedPreferences = getSharedPreferences(CONNECTION_PREFS, Context.MODE_PRIVATE);
    String URL = sharedPreferences.getString("WebServicesIP", "");

    String METHOD_NAME = "GetProductItemGroupList";
    String termURL = URL +"/"+ METHOD_NAME;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, termURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray array = new JSONArray(response);

                for (int i = 0; i < array.length(); i++) {

                    JSONObject jsonObject = array.getJSONObject(i);

                    String groupId = jsonObject.getString("ItemGroup");
                    String groupDescription = jsonObject.getString("Description");
                    myDb.insertItemGroup(i, groupId, groupDescription);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());

            AlertDialog.Builder errorDialog = new AlertDialog.Builder(MainActivity.this);
            errorDialog.setPositiveButton("Close", null);
            errorDialog.setTitle("Error");
            errorDialog.setMessage("Error on getting item group list from server. \nCause by : " + error.toString());
            errorDialog.show();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(stringRequest);
}

private void getItem() {
    //get webservice ip
    SharedPreferences sharedPreferences = getSharedPreferences(CONNECTION_PREFS, Context.MODE_PRIVATE);
    String URL = sharedPreferences.getString("WebServicesIP", "");

    String METHOD_NAME = "GetProductItemList";
    String termURL = URL +"/"+ METHOD_NAME;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, termURL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray array = new JSONArray(response);

                for (int i = 0; i < array.length(); i++) {

                    JSONObject jsonObject = array.getJSONObject(i);

                    String itemId = jsonObject.getString("ItemCode");
                    String itemDescription = jsonObject.getString("Description");
                    String itemGroup = jsonObject.getString("ItemGroup");
                    myDb.insertItem(i, itemId, itemDescription, itemGroup);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());

            AlertDialog.Builder errorDialog = new AlertDialog.Builder(MainActivity.this);
            errorDialog.setPositiveButton("Close", null);
            errorDialog.setTitle("Error");
            errorDialog.setMessage("Error on getting item list from server. \nCause by : " + error.toString());
            errorDialog.show();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    requestQueue.add(stringRequest);
}

private void displayProductionItem() {
    groupList = new ArrayList<>();
    itemList = new ArrayList<>();

    Cursor cursorGroup = myDb.getItemGroup();
    while (cursorGroup.moveToNext()) {
        String groupdId = cursorGroup.getString(1);
        String groupDescription = cursorGroup.getString(2);

        Product_Group group = new Product_Group();
        group.setGroupId(groupdId);
        group.setGroupDescription(groupDescription);

        groupList.add(group);
    }

    adapterGroup = new AdapterItemGroup(getApplication(), groupList, width, new ItemsGroupClickListener() {
        @Override
        public void onGroupClick(String groupName) {
            itemList.clear();
            Cursor cursorItem = myDb.getItem(groupName);
            while (cursorItem.moveToNext()) {
                String itemId = cursorItem.getString(1);
                String itemDescription = cursorItem.getString(2);

                Product_Item item = new Product_Item();
                item.setItemId(itemId);
                item.setItemDescription(itemDescription);

                itemList.add(item);
            }

            adapterItem = new AdapterItem(getApplication(), itemList);
            rvItem.setAdapter(adapterItem);
            adapterItem.notifyDataSetChanged();
        }
    });
    rvGroup.setAdapter(adapterGroup);
    adapterGroup.notifyDataSetChanged();
    myDb.close();
}

public static void displayItemPurchased(Context ctx) {
    DatabaseHelper myDb = new DatabaseHelper(ctx);

    itemPurchasedList = new ArrayList<>();

    Cursor cursorPurchased = myDb.getAllItemPurchased();
    while (cursorPurchased.moveToNext()) {
        String itemId = cursorPurchased.getString(1);
        String itemDescription = cursorPurchased.getString(2);
        Double itemAmt = cursorPurchased.getDouble(3);
        int itemQty = cursorPurchased.getInt(4);
        String itemTimestamp = cursorPurchased.getString(5);

        Product_Item item = new Product_Item();

        item.setItemId(itemId);
        item.setItemDescription(itemDescription);
        item.setItemAmt(itemAmt);
        item.setItemQty(itemQty);
        item.setItemTimestamp(itemTimestamp);

        itemPurchasedList.add(item);
    }
    adapterItemPurchase = new AdapterItemPurchase(ctx, itemPurchasedList);
    rvList.setAdapter(adapterItemPurchase);
    adapterItemPurchase.notifyDataSetChanged();
    myDb.close();
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {

    if (viewHolder instanceof AdapterItemPurchase.ItemPurchaseViewHolder)
    {
        int deletedIndex = viewHolder.getAdapterPosition();
        adapterItemPurchase.removeItem(deletedIndex);
    }
}

}

请帮助我

0 个答案:

没有答案