Sqlite Next button in android - android

I am trying to code a "next" button for my Sqlite db. The way I want it to work is upon entering a number it one of the entries from the db if pressed again it brings up the entry on the list for the same given number entered. However I cant get it to bring up the next entry. So far my button code looks like this. However I have tried countless way to configure it with no luck. Please help. Thanks in advance!!!
public void onClick(View view) {
if (view == btnNext) {
Cursor c = db.rawQuery("SELECT * FROM number WHERE meter='" + editText6.getText() + "'", null);
if (editText6.getText().toString().trim().length() >= 0) {
c.getPosition();
c.moveToPosition(+1);
idNumber.setText(c.getString(0));
editText5.setText(c.getString(6));
editText4.setText(c.getString(5));
editText3.setText(c.getString(4));
editText2.setText(c.getString(3));
editText.setText(c.getString(2));
textView4.setText(c.getString(7));
status.setText(c.getString(8));
while (c.moveToNext()) ;
}
}

I had to add this separate method to keep cursor open while scrolling to the next entry:
final Button nextBtn = (Button) findViewById(R.id.btnNext);
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (editText6.getText().toString().trim().length() == 0) {
Toast ToastMessage1 = Toast.makeText(getApplicationContext(), "Please" +
" Enter Meter Number\nThen Press Lookup Meter", Toast.LENGTH_LONG);
ToastMessage1.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
View toastView1 = ToastMessage1.getView();
toastView1.setBackgroundResource(R.drawable.shape9);
ToastMessage1.show();
} else {
if (c.getPosition() >= 1) {
c.moveToPrevious();
idNumber.setText(c.getString(0));
//Must maintain this order listed below in order for clear field on focus to work properly.
editText6.setText(c.getString(1));
editText5.setText(c.getString(6));
editText4.setText(c.getString(5));
editText3.setText(c.getString(4));
editText2.setText(c.getString(3));
editText.setText(c.getString(2));
//To here-list order.
date.setText(c.getString(7));
status.setText(c.getString(8));
etAddress.setText(c.getString(9));
}
if (c.getPosition() == 0) {
c.close();
Toast ToastMessage = Toast.makeText(getApplicationContext(), " Last Entry Recorded ", Toast.LENGTH_LONG);
ToastMessage.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
View toastView = ToastMessage.getView();
toastView.setBackgroundResource(R.drawable.shape9);
ToastMessage.show();
}
final Button clearBtn = (Button) findViewById(R.id.btnClear);
clearBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v2) {
if (v2 == clearBtn) {
c.close();
clearText();
}
}
});
}
}
});

Related

in my calculator app i have introduced four buttons but when i click them before entering values app crashes down. how to solve this problem

Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float firstValue , secondValue , ans ;
firstValue = Float.parseFloat( etFirstValue.getText().toString());
secondValue = Float.parseFloat( etSecondValue.getText().toString());
ans = firstValue + secondValue;
tvAnswer.setText("Ans = " + ans);
}
});
this is the coding for my addition button click, all buttons are working fine after entering values. but the problem occurs when I click the button before entering values my app suddenly stops working. solve this issue, make it something like nothing happens when I click the button before entering values.
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float firstValue=0 , secondValue=0 , ans=0 ;
if (etFirstValue.length()>0&& (etSecondValue.length()>0)
{
firstValue = Float.parseFloat(etFirstValue.getText().toString());
secondValue = Float.parseFloat(etSecondValue.getText().toString());
tvAnswer.setText("Ans = " + ans);
} else {
Toast.makeText(MainActivity.this, "Enter the Values First", Toast.LENGTH_SHORT).show();
}
ans = firstValue + secondValue;
tvAnswer.setText("Ans = " + ans);
}
});
I have solved this on my own.
Now it is not crashing just giving a warning message "Enter the values First"
Thanks to all for giving your precious time.
Just Do Like This
First Convert Your Values To String And Then Do This:
if(value1.matches("")){
Toast(context, "Please Fill First Value", LENGTH_SHORT).show();
return;
}
else if(value2.matches("")){
Toast(context, "Please Fill First Value", LENGTH_SHORT).show();
return;
}
else{
//Continue With Your Code
//Do Calculations Or Anything
}
Note: Change Value1, Value2 With Your Own Values Which Came From EditText
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float firstValue=null , secondValue=null, ans=0; //initialise the value here
firstValue = Float.parseFloat( etFirstValue.getText().toString());
secondValue = Float.parseFloat( etSecondValue.getText().toString());
if(firstValue==null&&secondvValue==null)
{
tvAnswer.setText("Ans = " + ans);
} else{
ans = firstValue + secondValue;
tvAnswer.setText("Ans = " + ans);
}
}
});
If the above doesnt work initialise all above value to zero instead of null.

Calling on click listener multiple times for updating text views?

In my activity i am showing user a question and 3 options to choose from, these options are text-views.
On clicking the text-view On click listener is performed which updates the text-views according to the option selected.
Now user again after reading the question performs on click listener on options. But this is working only twice then it stops clicking and updating.
Debug is not working either i am beginner so i am confused.
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.optionA) {
Answer = optionA;
LogAnswer = "a : " + optionA;
Log.w(TAG, LogAnswer);
conditions(questionnumber);
}
else if (id == R.id.optionB) {
Answer = optionB;
LogAnswer = "b : " + optionB;
Log.w(TAG, LogAnswer);
conditions(questionnumber);
}
else if (id == R.id.optionC) {
Answer = optionC;
LogAnswer = "c : " + optionC;
Log.w(TAG, LogAnswer);
conditions(questionnumber);
}
}
conditions method change text-views according to option.
public void conditions(int i){
if(Answer.equals(optionA) && i == 1 )
{
questionnumber = 2;
MartialStatus = Single;
GetQuestion(2);
}
else if(Answer.equals(optionB) && i == 1 )
{
questionnumber = 2;
GetQuestion(2);
}
}
It may be that my text-view onclicklistener is calling same text-view listener which breaks.

Toast not working,App stops when toast is to be displayed

I have created a small application to check whether the password entered by user is valid or not. it is being able to check , but its not displaying the toast and as soon as i click the button , it shows "Unfortunately , your app has stopped working". I am using my device for deployment. Please help me find out , why the toast is not working. I have used a command which sets the value of variable a,b,c in the edit text field , to check whether it is coming correct. And yes it was coming correct. So the problem lies in the toast as per i think.
public class second extends AppCompatActivity {
public EditText fname ;
public EditText lname ;
public EditText email ;
public EditText pass ;
public EditText blood;
public EditText cpass;
public EditText add ;
public EditText mob ;
public Toast t ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
fname = (EditText) findViewById(R.id.fname);
lname = (EditText) findViewById(R.id.lname);
email = (EditText) findViewById(R.id.email);
pass = (EditText) findViewById(R.id.pass);
add = (EditText) findViewById(R.id.add);
cpass = (EditText) findViewById(R.id.cpass);
mob = (EditText) findViewById(R.id.mob);
blood = (EditText) findViewById(R.id.blood);
Button sign = (Button) findViewById(R.id.sign);
sign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sfname = fname.getText().toString();
String spass = pass.getText().toString();
String scpass = cpass.getText().toString();
validate(spass, scpass);
}
});
}
public void validate(String spass ,String scpass){
int a =0;
int b =0;
int c =0;
t = new Toast(this);
int len = spass.length();
for(int i =0;i<len;i++){
char d = spass.charAt(i);
if(d>=48 && d<=57){
a++;
}
if(d>=65 && d<=90){
b++;
}
if(d>=33 && d<=47){
c++;
}
}
email.setText(a+" "+b+" "+c);
if(a==0 || b==0 || c==0){
t.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG);
t.show();
} else {
if(spass.equals(scpass)){
t.makeText(this,"login succesful",Toast.LENGTH_SHORT);
t.show();
} else {
t.makeText(this,"passwords dont match",Toast.LENGTH_SHORT).show();
}
}
}
}
try this
Toast toast = Toast.makeText(context, text, duration);
toast.show()
or
Toast.makeText(context, text, duration).show();
From the Docs, it says
Toast (Context context) Construct an empty Toast object. You must call
setView(View) before you can call show().
So when you create Toast object from its constructor, it is considered that you are trying to create Custom Toast
If you are not creating any, then use like:
public Toast t; // Global variable
Now inside your validate method:
t = Toast.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG);
t.show();
I have made some changes to you method , try using this one .
You can find more details on https://stackoverflow.com/a/21963343
public void validate(String spass ,String scpass){
int a =0;
int b =0;
int c =0;
// Toast t = new Toast(this);
int len = spass.length();
for(int i =0;i<len;i++){
char d = spass.charAt(i);
if(d>=48 && d<=57){
a++;
}
if(d>=65 && d<=90){
b++;
}
if(d>=33 && d<=47){
c++;
}
}
email.setText(a+" "+b+" "+c);
if(a==0 || b==0 || c==0){
/*
* updated
* */
Toast.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG).show();
} else {
if(spass.equals(scpass)){
/*Updated*/
Toast.makeText(this,"login succesful",Toast.LENGTH_SHORT).show();
} else {
/*Updated*/
Toast.makeText(this,"passwords dont match",Toast.LENGTH_SHORT).show();
}
}
}

Check if EditText is empty

I am making a math app. I have two arrays with two sets of numbers. I make the app choose a random number from each array. The app displays that two numbers in a TextView. It asks the user to type in a EditText what the addition of the numbers is. Then when the user presses "Submit", the app will then tell the user if they are right or wrong.
The app works, but I have one main problem. If the user does not type anything in the EditText and presses "Submit", the app force closes. I have looked at other questions asking how to check if a EditText is empty. I tried different methods but it still does not work.
public class MainActivity extends Activity {
Button submitb, startb, idkb;
EditText et;
TextView tv, rig, wron;
int arrone[] = { 24, 54, 78, 96, 48, 65, 32, 45, 95, 13, 41, 55, 33, 22,
71, 5, 22, 17, 13, 29, 63, 72, 14, 81, 7 }; // One Set of Numbers//
int arrtwo[] = { 121, 132, 147, 79, 82, 723, 324, 751, 423, 454, 448,
524, 512, 852, 845, 485, 775, 186, 99 }; // Another set of Numbers//
int random1, random2, add;
int wrong = 0;
int right = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list();
startb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = (int) (Math.random() * 25);
int mun = (int) (Math.random() * 19);
random1 = arrone[num];
random2 = arrtwo[mun];
String yu = (random1 + " + " + random2 + " =");
tv.setText(yu);
et.setHint("");
idkb.setVisibility(0);
startb.setText("Next Question");
}
});
startb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int bread2 = 0;
if (bread2 == 0) {
Toast.makeText(getApplicationContext(),
"You didn't answer the question!",
Toast.LENGTH_LONG).show();
}
int swag = random1 + random2;
String bread = et.getText().toString();
bread2 = Integer.parseInt(bread);
if (bread2 == swag) {
startb.setText("Next Question");
tv.setText("Correct");
et.setText("");
et.setHint("Click Next Question");
right++;
rig.setText("Right:" + right);
rig.setTextColor(Color.parseColor("#14df11"));
bread2 = 0;
}
else if (bread2 == 0) {
tv.setText("Wrong, the correct answer is " + swag + ".");
startb.setText("Next Question");
et.setText("");
tv.setHint("Click Next Question");
wrong++;
wron.setText("Wrong:" + wrong);
wron.setTextColor(Color.parseColor("#ff0000"));
bread2 = 0;
}
else {
tv.setText("Wrong, the correct answer is " + swag + ".");
startb.setText("Next Question");
et.setText("");
et.setHint("Click Next Question");
wrong++;
wron.setText("Wrong:" + wrong);
wron.setTextColor(Color.parseColor("#ff0000"));
bread2 = 0;
}
}
});
idkb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int swag = random1 + random2;
tv.setText("The Correct Answer is " + swag + ".");
et.setText("");
tv.setHint("Click Next Question");
wrong++;
wron.setText("Wrong:" + wrong);
wron.setTextColor(Color.parseColor("#ff0000"));
}
});
}
private void list() {
submitb = (Button) findViewById(R.id.b1);
startb = (Button) findViewById(R.id.b2);
et = (EditText) findViewById(R.id.et1);
tv = (TextView) findViewById(R.id.tv1);
rig = (TextView) findViewById(R.id.rtv);
wron = (TextView) findViewById(R.id.wtv);
idkb = (Button) findViewById(R.id.idk);
}
Few points to consider:
you are setting OnClickListener on startb twice, is it just a typo?
in the second onClick(), you have a condition:
int bread2 = 0;
if (bread2 == 0) {
//rest of code
}
This condition will ALWAYS be satisfied, is that what you want?
you can check if there's nothing entered by use with the following:
String bread = et.getText().toString();
// check if there is any text entered by user
if (bread.length() > 0) {
bread2 = Integer.parseInt(bread);
if (bread2 == swag) {
startb.setText("Next Question");
//the rest of the code
}
Not sure I'm understanding you correctly, but it should be easy as:
et = (EditText) findViewById(R.id.et1);
String bread = et.getText().toString();
if (bread.length() == 0){
// raise error dialogue
} else {
// continue to check the parsed integer
}
This should all be done within your click listener.
You need to use findViewById on the et before gets its value.
By the way, you are setting bread2 value to 0, and on the next line verifying if it's 0, i suppose it's just a test, isn't it?
#Override
public void onClick(View v) {
int bread2 = 0;
if (bread2 == 0) {
Toast.makeText(getApplicationContext(),
"You didn't answer the question!",
Toast.LENGTH_LONG).show();
}
int swag = random1 + random2;
et = findViewById(R.id.yourEditText);
String bread = et.getText().toString();
bread2 = Integer.parseInt(bread);
//...
Hope it helps!
I think it good practice to check text length, for example this function can help you..
public boolean isEditTextEmpty(EditText input){
if(input.getText().length() == 0)
return true;
else
return false;
}
if (et.getText().length == 0 || et.gettext == null ){
// do nothing
} else {
// do whatever here
}
this should works fine

How to display Textview for a array item

I'm very new to android development. Hopefully you all can help me.
I'm doing a modified version of asqlmanager from sourceforge. In the code, it can only display 1 data in toast at a time. However, I want to display more than one piece of data at a time. The data is something like name, identification no., age. Whenever the user clicks at the row, it will display name, identification no., age in the toast.
the code as copied is as below:-
private void appendRows(TableLayout table, String[][] data) {
if (data == null)
return;
int rowSize=data.length;
int colSize=(data.length>0)?data[0].length:0;
for(int i=0; i<rowSize; i++){
TableRow row = new TableRow(this);
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// button 1 was clicked!
Utils.logD("OnClick: " + v.getId());
}
});
if (i%2 == 1)
row.setBackgroundColor(Color.BLUE);
for(int j=0; j<colSize; j++){
TextView c = new TextView(this);
c.setText(data[i][j]);
c.setPadding(3, 3, 11, 11);
if (j%2 == 1)
// if (i%2 == 1)
// c.setBackgroundColor(Color.BLUE);
// else
c.setBackgroundColor(Color.LTGRAY & Color.MAGENTA);
c.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// button 1 was clicked!
Utils.logD("OnClick: " + v.getId());
String text = (String)((TextView)v).getText();
EditText Displayname;
Displayname=(EditText)findViewById(R.id.SQLStm);
Displayname.setText(text);
Utils.toastMsg(_cont, "Text copied to clip board" + Displayname.getText()+ v.getId());
}
});
row.addView(c);
}
table.addView(row, new TableLayout.LayoutParams());
}
}
end of the code
Thank you.

Categories

Resources