friends, I am having trouble during insertion.The insert1.setOnClickListener(new View.OnClickListener() shows Error.Anyone having solution suggest me
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
ename=(EditText)findViewById(R.id.editText1);
eid=(EditText)findViewById(R.id.editText2);
insert1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
empname=ename.getText().toString();
empid=Integer.parseInt(eid.getText().toString());
Intent insert=new Intent(Insert.this,DBAdapter.class);
Bundle bundle=new Bundle();
bundle.putString("EMPNAME", empname);
bundle.putInt("EMPID", empid);
insert.putExtras(bundle);
startActivityForResult(insert,1);
//DBAdapter db = null;
//db.insert(empname,empid);
}
});
}
Related
hi i have a menu activity that contains 4 buttons when i press button one the second
activity is open and.i added a back button in second activity to come back to activity
one(menu)how would it perform through code can any one help
private ListView lv;
public static ArrayList<String> your_array_list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
lv = (ListView) findViewById(R.id.listView1);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, your_array_list );
lv.setAdapter(arrayAdapter);
try {
DisplayM.main();
} catch (Exception e) {
e.printStackTrace();
}
// if (ViewClass.theEnd)
// your_array_list.add(ViewClass.methods);
int lst = 0;
for(int i=0; i<lst; i++)
{
}
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
this is my listview activity
Use below code in your back button
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
YourActivity.this.finish();
}
});
Write below code on your activity :-
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
}
or
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
or
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
// if u want to go specific activity then use this code
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
In your second Activity on your Back Button onClick simply finish() the current (second/third ...) activity. This is what the default back button also does
in pause button you have to just call finish() method and then after finishing current activity it return to parent activity
Hi I am working on sliding fragments when user tap on button are draw the view. By using this library SlidingMenu library. This library is working perfectly.But I am facing a problem in Fragment class when I am sliding a view in my fragmentactivity in fragment class there is no methods are calling What I am trying here is my code :
FragmentActivity: this activity extends SlidingFragmentActivity(This activity is in side abvoe mentioned library project)
#Override
#SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
View view1 = getLayoutInflater().inflate(
R.layout.activity_main, null);
View view2 = getLayoutInflater().inflate(R.layout.menu_frame, null);
View view3 = getLayoutInflater().inflate(R.layout.menu_frame_two, null);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSlidingMenu().setMenu(view2);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new Fragment1()).commit();
getSlidingMenu().setSecondaryMenu(view3);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame_two, new Fragment2()).commit();
setContentView(view1);
}
Fragment1.java this activity extends Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println("oncreate view");
View v = inflater.inflate(R.layout.my_fragment_layout, container, false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
System.out.println("on activity created");
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("oncreate");
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
System.out.println("onAttach");
}
#Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
System.out.println("on save instance state");
}
#Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
super.onHiddenChanged(hidden);
System.out.println("onHiddenChanged");
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
}
#Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("onStop");
}
#Override
public void onViewStateRestored(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewStateRestored(savedInstanceState);
System.out.println("onViewstaterestored");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
System.out.println("onViewCreated");
}
When I am slide a view left are right side the above methods are not calling. But when I am click on menu button oncreateview is calling.
Inside on click I am calling getSlidingMenu().showMenu(); this method.
But i want to call any method when I slide on view. What was the problem in my code?
why any method is not calling inside of fragment when sliding?
Any one please help me..Thanks in advance.
Refer this tutorial. I think it's helpful for you.
Implementing Horizontal View Swiping Using ViewPager and FragmentPagerAdapter in Android
This is the code I would like you to go through.
Please spot my mistakes.
This is a simple code for a calculator app.
I am not sure but the mistake might be in button section of the code.
Would be really grateful if you helped me out in this..
Thanks in advance.
package com.iitg.sau_calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Cal extends Activity {
Button one, two, thr, fou, fiv, six, sev, eig, nin, zer, add, sub, mul, div, clr, equ;
TextView scr;
int ans=0,fans=0,ca=0,cs=0,cm=0,cd=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cal);
one=(Button)findViewById(R.id.one);
two=(Button)findViewById(R.id.two);
thr=(Button)findViewById(R.id.thr);
fou=(Button)findViewById(R.id.fou);
fiv=(Button)findViewById(R.id.fiv);
six=(Button)findViewById(R.id.six);
sev=(Button)findViewById(R.id.sev);
eig=(Button)findViewById(R.id.eig);
nin=(Button)findViewById(R.id.nin);
zer=(Button)findViewById(R.id.zer);
add=(Button)findViewById(R.id.add);
sub=(Button)findViewById(R.id.sub);
mul=(Button)findViewById(R.id.mul);
div=(Button)findViewById(R.id.div);
clr=(Button)findViewById(R.id.clr);
equ=(Button)findViewById(R.id.equ);
scr=(TextView)findViewById(R.id.Screen);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+1;
scr.setText(ans);
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+2;
scr.setText(ans);
}
});
thr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+3;
scr.setText(ans);
}
});
fou.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+4;
scr.setText(ans);
}
});
fiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+5;
scr.setText(ans);
}
});
six.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+6;
scr.setText(ans);
}
});
sev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+7;
scr.setText(ans);
}
});
eig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+8;
scr.setText(ans);
}
});
nin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+9;
scr.setText(ans);
}
});
zer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10;
scr.setText(ans);
}
});
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
ca+=1;
scr.setText("+");
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cs+=1;
scr.setText("-");
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cm+=1;
scr.setText("*");
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cd+=1;
scr.setText("/");
}
});
equ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
scr.setText(fans);
}
});
clr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=0;
fans=0;
ca=0;cm=0;cd=0;cs=0;
scr.setText("0");
}
});
}
public void Ans(){
if(ca>0)
{
fans=fans+ans;
ca=0;
}
else if(cs>0)
{
fans=fans-ans;
cs=0;
}
else if(cm>0)
{
fans=fans*ans;
cm=0;
}
else if(cd>0)
{
fans=fans/ans;
cd=0;
}
else
{
fans=ans;
ans=0;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cal, menu);
return true;
}
}
scr is textview and ans is a int. Your app probably crashes.
Replace this
scr.setText(ans);
By
scr.setText(String.ValueOf(ans));
public static String valueOf(int i)
Returns the string representation of the int argument.
Same for all
public final void setText (int resid)
resid is a resource which is an int value.
textView.setText(resid) looks for a resid with that value. If it's not found you will get ResourceNotFoundException.
You should use the below
public final void setText (CharSequence text)
takes a CharSequence as a param
http://developer.android.com/reference/android/widget/TextView.html#setText(int)
There might me other mistakes also (which may lead to crash). If its a crash its better you post the stacktrace.
What is the force close problem of this program?
public class MyActivity extends Activity {
TextView t=(TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
private OnClickListener i=new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
r.setOnClickListener(i);
}
}
You need to get your TextView and Button after inflating the layout.
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here inflate the layout
setContentView(R.layout.main);
//now you can get your widgets
final TextView t= (TextView)findViewById(R.id.textView1);
Button r=(Button)findViewById(R.id.button2);
r.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
t.setText("fghffghfhgf");
}
};
);
}
}
I really recommend you to check this to build your first app.
I'd like my application to show the indefinite progress bar (you know, the cycle) into the Activity's layout, just like the youtube Application. How could I do that? Thanks.
Hi you can use setProgressBarIndeterminateVisibility(boolean) to show indefinite progress bar.Here is the sample code:
private Button buttonUpStartUpload = null;
private boolean mToggleIndeterminate = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.lyt_upload_main);
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
buttonUpStartUpload = (Button) findViewById(R.id.buttonUpStartUpload);
buttonUpStartUpload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UploadingAsyncTask uploadingAsyncTask = new UploadingAsyncTask();
uploadingAsyncTask.execute("temp","doinback","returnparam");
}
});
}//onCreate Ends
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public class UploadingAsyncTask extends AsyncTask<String, String, String>{
Uploader uploader = new Uploader();
ProgressDialog progDialogUpload = null;
int imageIndex = 1;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mToggleIndeterminate = !mToggleIndeterminate;
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
publishProgress("Finish","3");
return "success";
}
#Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
if(values[0] == "Finish"){
imageIndex++;
}
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result.equalsIgnoreCase("success") || result == "success"){
mToggleIndeterminate = !mToggleIndeterminate;
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
}
}
}//UploadingAsyncTask ends