i am trying to insert checkbox value into my mysql database, there is no error in the program...
But i cannot get the value into the database.
In the database it show empty...
public class MainActivity extends AppCompatActivity {
CheckBox box;
Button btn_test;
private static final String REGISTER_URL="http://192.168.0.106/test/checkin.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
box = (CheckBox) findViewById(R.id.checkBox);
btn_test = (Button) findViewById(R.id.button);
btn_test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registerUser();
}
});
}
private void registerUser() {
box.isChecked();
String date = box1.toString().trim().toLowerCase();
register(date);
}
private void register(String date){
String urlSuffix = "?date=" + date;
class RegisterUser extends AsyncTask<String, Void, String> {
how can i get the value from the check box and inserted in the database
Please try this,May be it will help you :)
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
String date = box1.toString().trim().toLowerCase();
register(date); } } } );
Related
Hi I just need a little bit of a helping hand in completing this shared preferences code I have.
Currently I have a class where I stored in the relevant code for SharedPreferences:
public class SharedPreferencesManager {
private static final String APP_PREFS = "AppPrefsFile";
private static final String NUMBER_OF_CLICKS = "numberOfClicks";
private SharedPreferences sharedPrefs;
private static SharedPreferencesManager instance;
private SharedPreferencesManager(Context context) {
sharedPrefs =
context.getApplicationContext().getSharedPreferences(APP_PREFS, MODE_PRIVATE);
}
public static synchronized SharedPreferencesManager getInstance(Context context){
if(instance == null)
instance = new SharedPreferencesManager(context);
return instance;
}
public void storeClicks(int count)
{
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putInt(NUMBER_OF_CLICKS, count);
editor.apply();
}
public int getNumberOfClicks(){
int clicksNumber = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
return clicksNumber;
}
}
I want to count the number of clicks for buttons jokes, poems and funnystories from the MainActivity class and the 'select another' button from the Content class. So if each button was clicked 4 times, the total click count should be 16.
I want to keep the number of clicks count even after the app is closed and re-open. It's just the syntax of the counting of the clicks I am unsure of. I think for main activity it is correct but I am unsure on how to store it for the Content class.
TO recap, count the number of clicks for all of those buttons mentioned and this number should be accessible and updated no matter which page the user is in.
Below is the Main Activity class:
public class MainActivity extends AppCompatActivity{
final Context context = this;
int clicksCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferencesManager.getInstance(context).storeClicks(clicksCount);
Button jokesButton = findViewById(R.id.button_jokes);
Button poemsButton = findViewById(R.id.button_poems);
Button funnyStoriesButton = findViewById(R.id.button_funny_stories);
jokesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicksCount++;
}
});
poemsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicksCount++;
}
});
funnyStoriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicksCount++;
}
});
}
Below is Content class:
public class Content extends AppCompatActivity{
Button backButton;
Button selectAnotherButton;
TextView contentText;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
int clicksCount = SharedPreferencesManager.getInstance(context).getNumberOfClicks();
backButton = findViewById(R.id.button_back);
selectAnotherButton = findViewById(R.id.button_select_another);
selectAnotherButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clicksCount++;
}
});
backButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View v){
finish();
}
});
}
}
Instead of having the two methods getNumberOfClicks() and storeClicks() in your SharedPreferencesManager, you could only have one like this:
public void increaseClickCount() {
int clickCount = sharedPrefs.getInt(NUMBER_OF_CLICKS, 0);
clickCount++;
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putInt(NUMBER_OF_CLICKS, clickCount);
editor.apply();
}
You call this on every click. No need for the activities to know about the clicks count.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferencesManager prefManager = SharedPreferencesManager.getInstance(context);
jokesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prefManager.increaseClickCount();
}
});
...
}
Also there's no need to store your context as a global variable because your activity is already a context itself, you can just use this or MainActivity.this for a context.
I am developing an android application with firebase as my database. I want to insert some data on the press of a button, but some of the objects I want to insert in the database, is placed in different methods in my fragment. How can I insert data to my database across different methods using the same button?
public class mFragment extends Fragment implements
DateFragment.DatePickerEvent {
DatabaseReference database;
FirebaseAuth mAuth;
EditText text1;
EditText text2;
#Override
public void onDateSelected(String date) {
Button buttonDateText = (Button) getView().findViewById(R.id.buttonDate);
buttonDateText.setText(date);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_frag, container, false);
database = FirebaseDatabase.getInstance().getReference("Persons");
mAuth = FirebaseAuth.getInstance();
final FirebaseUser user = mAuth.getCurrentUser();
Button pickDateButton = (Button) rootView.findViewById(R.id.buttonDate);
Button submitButton = (Button) rootView.findViewById(R.id.buttonSubmit);
text1= (EditText) rootView.findViewById(R.id.oddsView);
text2= (EditText) rootView.findViewById(R.id.betAmountView);
pickDateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DateFragment picker = new DateFragment();
picker.setDatePickerEvents(mFragment.this);
picker.show(getFragmentManager(), "datePicker");
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
database.child("Person").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String myText= text1.getText().toString().trim();
String myAnotherText= text2.getText().toString().trim();
String id = database.push().getKey();
database.child(user.getDisplayName()).child(id).child("Adress").setValue(myText);
database.child(user.getDisplayName()).child(id).child("name").setValue(myAnotherText);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
return rootView;
}
}
You can use instance variables. Something like that:
public class MyFrag extends Fragment {
private Object obj1, obj2;
private void method1() {
obj1 = new Object();
}
private void method2() {
obj2 = new Object();
}
private void doSomething() {
// obj1 and obj2 are available here
}
}
im newbie with fragment, and i want to insert some data into database from Fragment with methods from MainActivity
here is my code
LaporanFragment
public class LaporanFragment extends Fragment{
EditText judulL, isiL;
TextView nomor_ktp, ambilNama;
ImageView fotoL;
Button kirim;
private ProgressDialog progressDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_laporan, container, false);
judulL = (EditText) v.findViewById(R.id.judulLaporan);
isiL = (EditText) v.findViewById(R.id.isiLaporan);
nomor_ktp = (TextView) getActivity().findViewById(R.id.nomor_ktp);
final String noktp = nomor_ktp.getText().toString();
//fotoL = (ImageView) v.findViewById(R.id.foto_laporan);
final String jdlLaporan = judulL.getText().toString();
final String isiLaporan = isiL.getText().toString();
kirim = (Button) v.findViewById(R.id.kirim_laporan);
kirim.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).kirim_lapor(jdlLaporan, isiLaporan, noktp);
}
});
return v;
}
and MainActivity with kirim_lapor method
public void kirim_lapor(final String judul, final String isi, final String username){
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.URL_LAPOR,
new Response.Listener<String>(){
#Override
public void onResponse(String response){
//progressDialog.dismiss();
try{
JSONObject jsonObject = new JSONObject(response);
//Toast.makeText(LaporanFragment.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
}catch(JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
//progressDialog.hide();
//Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("judul_laporan", judul);
params.put("isi_laporan", isi);
params.put("no_ktp", username);
return params;
}
};
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
}
the problem is when i press button kirim in fragment, the app will crash
please help me guys, sorry for my bad english.
To localize and hinder further error, instead of calling the MainActivity method via casting the getActivity(), you should make a Listener for telling the Activity about the data.
Create the interface for Listener in Fragment:
public class LaporanFragment extends Fragment {
private LaporanListener mListener;
// Define a Listener to 'speak up' to the main activity
public interface LaporanListener {
public void onSendReportClicked(String title, String content, String idNumber);
}
...
}
When clicking the button sendReport, use the listener:
public class LaporanFragment extends Fragment {
...
...
kirim.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
mListener.onSendReportClicked(jdlLaporan, isiLaporan, noktp);
}
});
...
...
}
Then you need to implement the interface Listener to the MainActivity:
public class MainActivity extends Activity implements LaporanFragment.LaporanListener {
...
#Override
public void onSendReportClicked(String title, String content, String idNumber) {
// MainActivity will receive the data here.
// You need to process here.
}
...
}
For further reading, read Creating Custom Listeners.
Textview value is crashing with null pointer exception
Below is the code which i am using. Is there any issue in the code.
public class MainActivity extends Activity {
Button btn;
int i =0;
SharedPreferences.Editor preferences;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
txt = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
txt.setText("Executed ----------------------- ");
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
After it is coming to onPostExecute the textview value is not setting the value.
Please help me. Thanks in advance.
Textview value is not setting because the value of id may be null. check it.
I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage