我有一個列表視圖。當我點擊每個項目時,我想用編輯文本顯示一個警報對話框,然後將輸入值保存到一個整數變量中。但是,它顯示錯誤。當我不轉換輸入文本,然後它工作正常,但是當我使用Integer.parseInt()時,應用程序崩潰。這裏是我的代碼和logcat。
// CODE
/**
* A simple {@link Fragment} subclass.
*/
public class All extends Fragment {
//EXPERIMENTAL CODE STARTS
private VolleySingleton volleySingleton;
private RequestQueue requestQueue;
private RecyclerView allItemList;
private ArrayList<Items> listItems=new ArrayList<>();
private AdapterItemAll adapterItemAll;
public static int ticketNum=5;
DatabaseHelper myDb;
public int quantity=0;
public float totalPrice=0;
private String title;
private boolean update;
private String name;
String strQuantity;
EditText quantityInput;
//SETTING IMAGE TEST CODE STARTS
//public static final String URL_API="http://www.delaroystudios.com/images/teams.json";
//ENDS
public All() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_all, container, false);
// allItemList=(RecyclerView) view.findViewById(R.id.allItemsList);
// allItemList.setLayoutManager(new LinearLayoutManager(getActivity()));
// adapterItemAll=new AdapterItemAll(getActivity());
// allItemList.setAdapter(adapterItemAll);
//****LISTVIEW EXPERIMENT STARTS
ListView listView=(ListView) view.findViewById(R.id.allItemsList);
myDb=new DatabaseHelper(getContext());
ArrayList<String> theList=new ArrayList<>();
final Cursor data=myDb.getItemContents();
if(data.getCount()==0){
Toast.makeText(getActivity(),"List is Empty",Toast.LENGTH_SHORT).show();
}else{
while(data.moveToNext()){
theList.add(0,data.getString(1));
ListAdapter listAdapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
//LIST ITEM ONCLICK ADD QUANTITY TO TICKET STARTS
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = (String) parent.getItemAtPosition(position);
//ALERT DECLARE
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setTitle("Item Quantity");
builder.setIcon(R.mipmap.ic_receipt_black_24dp);
builder.setMessage("Enter Item Quantity");
quantityInput=new EditText(getContext());
builder.setView(quantityInput);
//SET POSITIVE
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
strQuantity = quantityInput.getText().toString();
quantity=Integer.valueOf(strQuantity);
Toast.makeText(getActivity(),quantity,Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(),"Enter the correct quantity",Toast.LENGTH_LONG).show();
}
});
AlertDialog ad=builder.create();
ad.show();
//ENDS
//updating quantity
update=myDb.updateTicketQuantity(name,quantity);
Toast.makeText(getActivity(),"Total "+quantity+ "Items Added to the Ticket",Toast.LENGTH_LONG).show();
}
});
//ENDS
//LISTVIEW EXPERIMENT ENDS
return view;
}
public int quantityIncrease(int quantity){
this.quantity=quantity+1;
int qt=this.quantity;
return qt;
}
}
//錯誤日誌
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x42
at android.content.res.Resources.getText(Resources.java:230)
at android.widget.Toast.makeText(Toast.java:265)
at com.soumya.possystem.All$1$1.onClick(All.java:142)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
'Toast.makeText(getActivity(),將String.valueOf(數量), Toast.LENGTH_LONG).show();' –
好的。但我需要將該值轉換爲整數並將其保存到我的數量變量。將它們展示在烤麪包上不是我的目標。 –
OOPS!我的壞兄弟。我錯過了你的觀點。現在它運行良好。謝謝。 –