Insert data to EditText - android

I'm making a simple app which has two buttons and one edittext. When the buttons are clicked the edittext wil display values of my database. I tried this code but it did not work
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == bnt1) {
SharedPreferences sharename = PreferenceManager.getDefaultSharedPreferences(this);
String na = sharename.getString("thename", "null");
edit.setText(na);
} else if (v == bnt2) {
SharedPreferences sharedescribed = PreferenceManager.getDefaultSharedPreferences(this);
String dess = sharedescribed.getString("thedescribed", "null");
edit.setText(dess);
}
}
Please show me the problems.

It looks like you probably don't have a Button attached to your onClick() method which would result in a null pointer exception when you try to reference v inside the method. There are different ways of implementing this (programmatically or through xml). With what you show (not overriding the onClick() method) it looks like you would want to use the xml way which means you need to declare the function for your Buttons
<Button
android:id="#+id/btn1"
android:onClick="onClick"
then do the same for btn2. Then v will reference whichever Button was clicked. You can also do this programmatically but setting an onClickListener
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
// some code
}
});
But then you have to do the same thing for btn2 so if you want to reuse the same function for both Buttons then the first method would be preferred.
Here is a link to a previous answer of mine convering the same thing

Use the below code:
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharename = PreferenceManager.getDefaultSharedPreferences(this);
String na = sharename.getString("thename", "null");
edit.setText(na);
}
}
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedescribed = PreferenceManager.getDefaultSharedPreferences(this);
String dess = sharedescribed.getString("thedescribed", "null");
edit.setText(dess);
}
}

Try this code :
SharedPreferences sharename = this.getSharedPreferences("sharename", MODE_WORLD_READABLE);
String prefName = sharename.getString("thename", 0);
edit.setText(prefName);

I guess the Exception is NullPointerException. Please check your share preference not null before call getString() method.

Related

Non-final variable on a click listener

I am making an android application. I have a TextField and a button. Based on the value of the textfield, as soon as the user clicks the button I want to make something. I have the code:
EditText et = (EditText)findViewById(R.id.editText1);
String value = et.getText().toString();
ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (value = "a" ) {
//do something }
}
});
This however doesn't compile, saying "Cannot refer to a non-final variable value inside an inner class defined in a different method". Is there any way to fix this? Thanks a lot
Use
final String value = et.getText().toString();
and then use,
if(value.equals("a") {
}
If you want to compare the value of a string you should use the "equals" method instead of "=".
The code will look like this:
EditText et = (EditText)findViewById(R.id.editText1);
final String value = et.getText().toString();
ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (value.equals("a") ) {
//do something }
}
});
final String value = et.getText().toString();

User setup activity of android app

I want to create a simple app to upload my location .I have two activities and in first activity the user can input parameters url for upload with editbox , a checkbox if user wish upload location save preferences button and start button for go to get location activity.I try this but no work...How i call my function start and save?Any help?I have errors when debug...after click button
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences preferences = getSharedPreferences("gpstracker" , MODE_PRIVATE);
String strValue = preferences.getString("Url",strValued);
edittxtUrl = (EditText)findViewById(R.id.txtUrl);
edittxtUrl.setText(strValue);
Button buttonStart = (Button)findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(startListener);
Button buttonSave = (Button)findViewById(R.id.buttonSave);
buttonSave.setOnClickListener(saveListener);
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Start();
}
};
private OnClickListener saveListener = new OnClickListener() {
public void onClick(View v) {
Save();
}
};
public void Save() {
SharedPreferences preferences = getSharedPreferences("gpstracker" , MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
edittxtUrl = (EditText)findViewById(R.id.txtUrl);
String strUrl = edittxtUrl.getText().toString();
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
boolean blnTandC = chkTandC.isChecked();
editor.putString("Url", strUrl); // value to store
editor.putBoolean("TandC", blnTandC); // value to store
// Commit to storage
editor.commit();
}
public void Start() {
startActivity(new Intent(this, LocTracker.class));
}
Without your log cat it is somewhat hard to tell what your problem is, but what I think is happening is that you are passing a null view to the start method, and this is a problem because you are then trying to get a context. Effectively what you have written is
null.getContext()
which doesn't work. You can fix this by replacing view.getContext() with getApplicationContext()

Android - How to increase EditText value?

I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}

XML inflation and EditText

I've posted another question on this, but im asking another as the problem has been narrowed down some what. My problem is that I'm getting null pointer exception from inside the inner onClick at the line where the first String strTime is created. It has been suggested the reason for this is the poptest.xml not inflating properly. Can anyone see why this is happening?
I have this method:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.newappt:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.poptest);
dialog.setTitle("Create New Appointment");
dialog.setCancelable(true);
Button buttoncancel = (Button) dialog.findViewById(R.id.Button01);
buttoncancel.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button buttonsave = (Button) dialog.findViewById(R.id.Button02);
buttonsave.setOnClickListener(new OnClickListener() {
// on click for save button
#Override
public void onClick(View v) {
String strTime = ((EditText) findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) findViewById(R.id.evntdet)).getText().toString();
cursor = getAppts();
addAppt(strTime, strTitle, strDet);
showAppts(cursor);
dialog.dismiss();
}
});
dialog.show();
break;
case R.id.delappt:
rmvall();
break;
}
}
Try this if your EditText are in the Dialog:
String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText().toString();
String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText().toString();
my first guess would be that the findViewById inside the onClick listener is searching for R.id.evnttime as a child of buttonsave.
the findViewById method belongs to the View class and is therefore availlale to every sub class like Buttons, Layouts...
It is searching up to the last children of the view you use it on.
Possible solutions here:
find your views outside of the onClick method or use the findViewById function of the main view (dialog).

Android onClick method

I have two onclick method in android project
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
});
And i have onther onClick method in which i have to call that method directly
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(new OnClickListener() {
----- I have to call that method---
}
});
Is there Any Solution for this?
You want to call the first onClick from the second? Just extract the contents of your first onClick in a separate method and call that method from each onClick.
Edit: As per st0le's comment, you can do what you want by calling clr.performClick(). (Didn't know that.) Still, extracting it into a separate method seems cleaner.
you can do something like this in the XML file
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="some_function" />
and put this function in the Java file
public void some_function(View view) {
// stuff...
}
and put the some_function in both "onClick"s
You should turn to use the simplest way that I always do as below:
#Override
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
}
}
};
I would recommend to use the same OnClickListener for both buttons if both buttons really have to do the same thing:
OnClickListener l=new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
};
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(l);
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(l);
or if its not exactly the same you could access the listener method by l.onClick(null); manually..

Categories

Resources