Call an activity using Alarm Manager & Broadcast Receiver - broadcastreceiver

I'm trying to create an app that automatically make phone calls after regular intervals to a specified number. I'm using Alarm Manager and Broadcast Receiver for this purpose. Alarm Manager can't initiate PHONE CALL activity and application terminates giving an error.
Here's my code. I am new in this Dev this.
*MainActivity.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView label;
private EditText phoneNum;
private PendingIntent pendingIntent;
private AlarmManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label=(TextView)findViewById(R.id.entertextlabel);
phoneNum=(EditText)findViewById(R.id.phonenofield);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
//pendingIntent = PendingIntent.getActivity(this,1,alarmIntent,0);
}
public void startAlarm(View view) {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancelAlarm(View view) {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
}
*AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
context.startActivity(in);
}
}
I'm getting error "Call requires user permission...."
at
context.startActivity(in);
*mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter the Mobile number"
android:id="#+id/entertextlabel"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Alarm"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/callBtn"
android:layout_marginBottom="81dp"
android:onClick="startAlarm"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel Alarm"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/callBtn"
android:onClick="cancelAlarm"/>
</RelativeLayout>
*Mantifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saud.nexustelecom" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

The issue is resolved.
I used the Flag "FLAG_ACTIVITY_NEW_TASK" with with the intent in AlarmReceiver class.
New code is as follows
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
/**
* Created by Saud on 05/11/2015.
*/
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
// Intent in = new Intent(Intent.ACTION_CALL);
//in.setData(Uri.parse("tel:03324310929"));
Intent in = new Intent(Intent.ACTION_CALL);
in.setData(Uri.parse("tel:03324310929"));
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
}

Related

Load Webview in second Fragment when Button is clicked from First Fragment

I have two Fragments. First Fragment is named as Fragment1 and Second Fragment named as physics. First Fragment have Two Buttons. I would Like to show a local html page ( say A.html when Button1 is pressed and B.html when Button2 is Pressed ) in Second Fragment i.e in physics.java
My Fragment.java contains following code
package com.nepalpolice.mnemonics;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by Sagar on 2017/09/23.
*/
public class Fragment1 extends Fragment implements View.OnClickListener{
public Button button;
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
button = (Button) view.findViewById(R.id.button1);
button = (Button) view.findViewById(R.id.button2);
return view;
}
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity().getApplicationContext(), physics.class);
switch (v.getId()) {
case R.id.button1:
intent.putExtra("url", "file:///android_asset/B.html");
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("url", "file:///android_asset/A.html");
startActivity(intent);
break;
default:
break;
}
}
}
and My SecondFragment contains following code
package com.nepalpolice.mnemonics;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Sagar on 2017/09/23.
*/
public class physics extends Fragment {
WebView myWebView;
public physics() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment2, container, false);
String url = getActivity().getIntent().getStringExtra("url");
myWebView=(WebView)rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
return rootView;
}
}
and main fragment_fragment1.xml contains
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nepalpolice.mnemonics.Fragment1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:text="button01"
android:onClick="onClick"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentTop="true"
android:text="button02"
android:onClick="onClick"/>
</LinearLayout>
</FrameLayout>
and Secondfragment.xml has
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="#+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
But my is Crashing with following Error.
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button1'
I'm almost Done with my project. If it could be done...It would mean a lot for me.

How to change widget view in background service

I have created widget. I am trying to update the widget in background each 5 seconds (For testing purpose 5 second). But it is not getting update.Is any mistake on my code.
Provider class
package com.widget.widgetapplication;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.util.Random;
public class MyWidget extends AppWidgetProvider {
private int randomNumber = 0;
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "Receive", Toast.LENGTH_SHORT).show();
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "Update", Toast.LENGTH_SHORT).show();
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// create some random data
randomNumber = new Random().nextInt(100);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(randomNumber));
// Set the text
remoteViews.setTextViewText(R.id.time, String.valueOf(randomNumber));
appWidgetManager.notifyAppWidgetViewDataChanged(widgetId, R.id.time);
// Register an onClickListener
Intent intent = new Intent(context, MyWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.watch, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
widget info xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="100dp"
android:minWidth="100dp"
android:previewImage="#drawable/analog_watch"
android:initialLayout="#layout/widget_layout"
android:updatePeriodMillis="5000">
</appwidget-provide
Registered receiver in manifest
<receiver android:name=".MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_info"/>
</receiver>
Widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<AnalogClock
android:id="#+id/watch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textStyle="bold"
android:text="Time is : "
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"/>
</LinearLayout>
YES. you are doing it wrong.
the minimum available value for updatePeriodMillis is 30min (1800000 millis).
official document :
Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.

App crashes saying 'Unfortunately, app has stopped' while using asmack

Here is my code for connecting to OpenFire through aSmack API. I am getting an error saying 'unfortunately app has stopped' when trying to run the app on my phone. When i remove the code of aSmack, the app is working fine with creating one button. I also have added the jars into the classpath. Please help me.
package com.example.demo;
import java.io.File;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
String username = "swapnil", password = "swapnil", host = "192.168.0.4", service = "mirana";
int port = 5222;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
final EditText et =(EditText)findViewById(R.id.editText1);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// et.setText("Test");
XMPPConnection xmpp = null;
ConnectionConfiguration xmppConfig = new ConnectionConfiguration("192.168.0.4", 5222,"mirana");
xmppConfig.setSASLAuthenticationEnabled(true);
xmppConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
if (xmppConfig == null)
throw new NullPointerException("XMPPService must be configured before it can connect!");
try {
if (xmpp == null) {
xmpp = new XMPPConnection(xmppConfig);
}
xmpp.connect();
} catch (XMPPException ex) {
et.setText("ERROR !");
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And here is the manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.demo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Without log, after watching your code,what I can guess is that, you are running smack methods on Main Thread.. The exception should be of NetworkOnMainThreadException.
You should do the task, in your button click, on background threads. You might want to create AsyncTask for this.
Also, you dont have Internet permission. So please fix that too..
While implementing Smack, you need to take care of maintaining sessions of Smack Connections and related ones.. For this, you can refer the following the GitHub demo app: Smack Android Demo
You have to add Internet permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
And you need to add below code in onCreate() method.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

NewActivity not resolving in base class - should/how do I get the base class to recognize it?

Using Eclipse on OSX:
I'm trying to start my first new activity from an Intent. The error seems to be here:
Intent newButton = new Intent(v.getContext(),RandomActivity.class);
startActivity(RandomActivity);
RandomActivity isn't getting resolved. I think I have it correct in my Manifest file. I think my imports are correct. Do I need to declare it somewhere in my base Activity? That doesn't seem to work. It's like the code doesn't recognize the existence of the new class, but I can't see where to fix that.
The rest of my code:
package course.examples.UI.Button;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import course.examples.UI.Button.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import course.examples.UI.Button.RandomActivity;
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent newButton = new Intent(v.getContext(),RandomActivity.class);
startActivity(RandomActivity);
^Multiple markers at this line
brandNewButton cannot be resolved to a type
RandomActivity cannot be resolved to a variable
Note: brandNewButton WAS a class, but I deleted it, then went File->New->Class to create RandomActivity.java.
}
});
}
}
My XML code is below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:text="Press Me!"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
>
</Button>
</RelativeLayout>
And here's my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.examples.UI.Button"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:allowBackup="false"
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".ButtonActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RandomActivity">
</activity>
</application>
</manifest>
Thanks in advance everyone, it's great to have such a community of support here for us noobies.
You don't really need to do :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent newButton = new Intent(v.getContext(),RandomActivity.class);
Here's the issue: You are trying to start activity with different intent name ??
"startActivity(RandomActivity);" // Wrong intent name
Check your intent!
Instead, just try using this :
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent newButton = new Intent(ButtonActivity.this, RandomActivity.class);
startActivity(newButtom);//This is right
Also.. if your RandomActivity is not from your same package, register it with full path like:
<activity android:name="your packagename where random activity is.RandomActivity" />
Hope this helps.

pass values of clicked item in item list to a new activity

I am trying to get the values clicked on my listactivity to display on the third.xml. it has 4 editText fields. I want to display the 4 columns on the 4 ET fields. HELP!!!
HEre is a code for getting a specific record in my DBAdapter:
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {ROW_ID,
KEY_FIRSTNAME, KEY_LASTNAME, KEY_CITY, KEY_STATE},
ROW_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Then here is my Second.java list acitivity
package com.dtan.testcontacts;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Second extends ListActivity {
public final static String ID_EXTRA="com.dtan.testcontacts._ID";
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db.open();
Cursor c = db.getAllContacts();
ArrayList<String> columns = new ArrayList<String>();
int iFirstName = c.getColumnIndex(DBAdapter.KEY_FIRSTNAME);
int iLastName = c.getColumnIndex(DBAdapter.KEY_LASTNAME);
for(c.move(0); c.moveToNext(); c.isAfterLast()){
String columnstring = c.getString(iFirstName)+ " " + c.getString(iLastName)+"\n";
columns.add(columnstring);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, columns);
setListAdapter(adapter);
final ListView lv = getListView();
registerForContextMenu(lv);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case R.id.delete:
AlertDialog.Builder abuilder = new AlertDialog.Builder(Second.this);
abuilder.setMessage("Are you sure you want to delete?");
abuilder.setCancelable(false);
abuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Cursor c = db.getAllContacts();
c.moveToPosition(info.position);
String id = c.getString(c.getColumnIndex(DBAdapter.ROW_ID));
db.open();
db.deleteContact(Long.parseLong(id));
c.close();
Toast toast = Toast.makeText(Second.this, "Contact Deleted Successfully", 5000);
toast.show();
Intent i = new Intent(Second.this, Second.class);
startActivity(i);
}
});
abuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = abuilder.create();
alert.show();
return true;
case R.id.edit:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> lv, View cell,
int position, long id) {
Intent i = new Intent(Second.this, Third.class);
i.putExtra("entry_id", position);
startActivity(i);
}
});
}
return super.onContextItemSelected(item);
}
}
Finally, here is the Third activity where i want to display the value clicked on the listactivity to display on each edittext. xml first then java code:
<EditText
android:id="#+id/editText1Third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:clickable="false"
android:ems="10"
android:hint="Firstname"
android:inputType="text"
android:text="#string/none" />
<TextView
android:id="#+id/textView1"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="#string/fname" />
<EditText
android:id="#+id/editText2Third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Lastname"
android:inputType="text"
android:text="#string/none" />
<TextView
android:id="#+id/textView2"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:text="#string/lastname" />
<EditText
android:id="#+id/editText3Third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="City"
android:inputType="text"
android:text="#string/none" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city" />
<EditText
android:id="#+id/editText4Third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="State"
android:inputType="text"
android:text="#string/none" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/state" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="64dp" >
<Button
android:id="#+id/ButtonUpdateThird"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/update" />
</RelativeLayout>
</LinearLayout>
Third.java
package com.dtan.testcontacts;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.EditText;
public class Third extends Activity {
private DBAdapter db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
int b = this.getIntent().getExtras().getInt("entry_id");
EditText firstname = (EditText) findViewById(R.id.editText1Third);
db.open();
Cursor c = db.getRecord(b);
firstname.setText(c.getString(1));
db.close();
}
}