During the moment of alertdialog prompt up, edittext value is defined as blank value.Then, I needed to enter second number so that textview will display the first number I entered before. This means that whatever I entered at first time, it must be blank. How to let the edittext not be blank when I clicked on amount listview.
public class MainActivity extends Activity {
String[] listItems;
int total = 0;
boolean correctCategory = true;
int amount = 0;
String v;
TextView tv;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems = getResources().getStringArray(R.array.budgetlist);
tv = (TextView) findViewById(R.id.tv3);
btn = (Button) findViewById(R.id.bt1);
final ListView listview = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
listview.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Income.class);
startActivity(i);
}
});
final EditText et = new EditText(MainActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Amount");
builder.setMessage("Please enter your amount");
et.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(et);
builder.setCancelable(false);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
tv.setText(total);
Toast.makeText(getApplicationContext(), et.getText(),
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
alert.show();
String s = listview.getItemAtPosition(position).toString();
if (s.equals("Amount")) {
v = et.getText().toString();
try
{
amount = Integer.parseInt(v);
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
totalbudget = totalbudget + food;
correctCategory = true;
} else {
correctCategory = false;
}
}
});
}
}
I expect the edittext no parsed blank value into integer at first time.
The problem is that that you are trying to assign an empty string to the variable amount because the method in which you put it will be invoked first before the PositivButton onClick method which will always lead to NumberFormatException which will result in your try block to never execute
So try this solution.
First, make the variable s global.
Secondly, move all the variables assignment to the following method:
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (s.equals("Amount")){
v = et.getText().toString();
amount = Integer.parseInt(v);
tv.setText(String.valueOf(amount));
}
}
});
and then lastly the final method
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
alert.show();
s = listView.getItemAtPosition(position).toString();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.