我有一个列表视图。当我点击每个项目时,我想显示带有编辑文本的警告对话框,然后将输入值保存到整数变量中。但是,它显示错误。当我不转换输入文本然后它工作正常,但当我使用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)
答案 0 :(得分:1)
试试这个:
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
strQuantity = quantityInput.getText().toString();
quantity= Integer.parseInt(strQuantity); //int
Toast.makeText(getActivity(),String.valueOf(quantity),Toast.LENGTH_LONG).show(); //error here
Toast.makeText(getActivity(),"Enter the correct quantity",Toast.LENGTH_LONG).show();
}
});