I used the answer of this question: Android CheckBoxPreference title color
But when i deployed to my application, i click to "account setting" section => Settings stoped
.What is wrong here?
This is my Custom CheckBoxPreference
package vn.com.myapp.view;
import android.content.Context;
import android.graphics.Color;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
/**
* Created by anubis on 5/16/15.
*/
public class MyCheckBoxPreferences extends CheckBoxPreference {
public MyCheckBoxPreferences(Context context) {
super(context);
}
public MyCheckBoxPreferences(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCheckBoxPreferences(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
TextView title = (TextView) view.findViewById(android.R.id.title);
title.setTextColor(Color.BLACK);
}
}
And using it
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Options" />
<vn.com.myapp.view.MyCheckBoxPreferences
android:checked="true"
android:clickable="false"
android:defaultValue="true"
android:focusable="false"
android:key="contactSync"
android:summary="Sum1"
android:textColorHighlight="#color/textColorSecondary"
android:title="tit1" />
<vn.com.myapp.view.MyCheckBoxPreferences
android:editable="false"
android:checked="true"
android:clickable="false"
android:defaultValue="true"
android:focusable="false"
android:key="smsSync"
android:summary="Sum2"
android:textColorHighlight="#color/textColorSecondary"
android:title="tit2" />
<vn.com.myapp.view.MyCheckBoxPreferences
android:checked="true"
android:clickable="false"
android:defaultValue="true"
android:focusable="false"
android:key="callLogSync"
android:summary="Sum3"
android:textColorHighlight="#color/textColorSecondary"
android:title="tit3" />
</PreferenceScreen>
Related
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.
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.
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);
}
}
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 5;
}
}
}
I am getting a type mismatch error. Basically it says it cannot convert the activity to a fragment. from what I have read it has something to do with my imports. Can anyone help me out?
I know its late but am submitting this answer because it happens with a lot of developers.
This error appears due to library import conflict. Check your imports in FirstFragment, SecondFragment and ThirdFragment and make sure that you are using the same fragment library.
if you are using Fragments for older APIs you need to import android.support.v4.app.Fragment;
But if you are using Fragments for newer APIs use
android.app.Fragment;
The problem comes when you use auto-complete to complete class name you may not notice what library you are importing.
Please look at the code below. I show you and I expand you how to create pager view:
MainActivity class:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
}
activity_main.xml
<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">
<!-- it is important to use ViewPager from support library -->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MyPagerAdapter.class
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1:
return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2:
return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3:
return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4:
return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default:
return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 5;
}
}
fragment_layout.xml
I created common layout for all fragments. I will change background colour and set text in source code below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
FirstFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.RED);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static FirstFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
FirstFragment result = new FirstFragment();
result.setArguments(args);
return result;
}
}
SecondFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.GRAY);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static SecondFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
SecondFragment result = new SecondFragment();
result.setArguments(args);
return result;
}
}
ThirdFragment.class
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ThirdFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
v.setBackgroundColor(Color.BLUE);
TextView textView = (TextView) v.findViewById(R.id.text_view);
textView.setText(getArguments().getString("value"));
return v;
}
public static ThirdFragment newInstance(String s) {
Bundle args = new Bundle();
args.putString("value", s);
ThirdFragment result = new ThirdFragment();
result.setArguments(args);
return result;
}
}
Here is complete program witch display 5 fragment with text which is passed as parameter. Please try it. If you have one more question or something in my solution is not clear or my solution is not helpful for you. Go ahead, just ask. :)
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();
}
}