Show immediate changes when data deleted from database - android

View Database XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/wood_bg" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Daily Fruit Log"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:layout_width="110dp"
android:layout_height="fill_parent"
android:text="Name of fruit"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="No Of Fruit"
android:layout_weight="1"
android:textStyle="bold"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Total Calories"
android:layout_weight="1"
android:textStyle="bold"/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TableRow>
<TextView
android:id="#+id/view"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="food"
android:layout_weight="1" />
<TextView
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1"
android:layout_weight="1"/>
<TextView
android:id="#+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="20"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
<Button
android:id="#+id/bdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Log"
android:layout_gravity="center"
android:layout_marginTop="30dp" />
Java class of View Database page:
public class FruitLog extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fruitlog);
TextView tv = (TextView) findViewById(R.id.view);
TextView tv1 = (TextView) findViewById(R.id.view1);
TextView tv2 = (TextView) findViewById(R.id.view2);
FruitDB info = new FruitDB(this);
info.open();
String data = info.getName();
String data1 = info.getNum();
String data2 = info.getCal();
info.close();
tv.setText(data);
tv1.setText(data1);
tv2.setText(data2);
Button save = (Button) findViewById(R.id.bdelete);
save.setTextColor(Color.BLUE);
save.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
FruitDB info = new FruitDB(FruitLog.this);
info.open();
info.deleteAll();
info.close();
}
});
}
I have edited the code, now I can delete all the data in my page, but the issue is that, I have to navigate back and enter this FruitLog page to see the changes (all rows deleted).
I want to see a immediate result when the user click 'Clear Log' Button without navigating back and front.

This method in your db class:
public boolean deleteAll() {
// Returns true if number of deleted rows is larger than 0;
return mDb.delete(DATABASE_TABLE, null, 1) > 0;
}
In the onClickListener:
public void onClick(View v) {
FruitDB db = new FruitDB(YourClass.this);
db.open();
db.deleteAll();
db.close();
}

To delete entire table:
db.delete(DATABASE_TABLE, null, null);
To delete particular records in a table:
db.delete(DATABASE_TABLE, whereCondition, null);
for eg:
db.delete(DATABASE_TABLE, "KEY_NAME='mango'", null);

Button save = (Button) findViewById(R.id.bdelete);
save.setTextColor(Color.BLUE);
save.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
DbHelper myDbHelper =new DbHelper(Activity.this);
try{
myDbHelper.open();
myDbHelper.delete();
}catch(Exception e){
e.printStackTrace();
}finally{
myDbHelper.close();
}
}
});
create a method in your DBHelper class
public void delete() {
ourDatabase.execSQL("delete from "+ DATABASE_TABLE);
ourDatabase.execSQL("UPDATE sqlite_sequence set "+ KEY_ROWID + " =0 where name= '"+DATABASE_TABLE+"'");
}

It will delete all records
public void deleteAll() {
final SQLiteDatabase db = getWritableDatabase();
db.delete(DATABASE_TABLE, null, null);
db.close();
}

Move this code to save.setOnClickListener(new View.OnClickListener(){..} after deleting
or move it to onResume()
FruitDB info = new FruitDB(this);
info.open();
String data = info.getName();
String data1 = info.getNum();
String data2 = info.getCal();
info.close();
tv.setText(data);
tv1.setText(data1);
tv2.setText(data2);

Related

Display corresponding details of user when selected from list

I have developed a screen where a no. of people from the database are displayed in a list view. I want to display the profile page of the selected person. So my question is how to bind each detail of the selected person like name, contact, etc. to the profile page which I have created? Will I have to call the getById API in the onItemClickListener?
Here's the edited code:-
public class Test extends AppCompatActivity {
List<Genie> genieList;
GenieAdapter genieAdapter;
TextView responseView;
ProgressBar progressBar;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
responseView = (TextView) findViewById(R.id.responseView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
button = (Button) findViewById(R.id.test);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Test.this, "Blahblah", Toast.LENGTH_LONG).show();
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<Void, Void, List<Genie>> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected List<Genie> doInBackground(Void... urls) {
GenieService genieService = new GenieService();
return genieService.getAll();
}
protected void onPostExecute(List<Genie> genies) {
if (genies == null) {
new ArrayList<Genie>(); // "THERE WAS AN ERROR"
} else {
progressBar.setVisibility(View.GONE);
Log.i("INFO", genies.get(0).name);
List<String> rows = genies.stream().map(genie -> getRow(genie)).collect(Collectors.toList());
genieAdapter=new GenieAdapter(getApplicationContext(),R.layout.genie_list, genies);
ListView list=(ListView)findViewById(R.id.listViewMain);
list.setAdapter(genieAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Test.this, "" + position, Toast.LENGTH_SHORT).show();
// if (position == 1) {
// startActivity(new Intent(Test.this, viewGenie1.class));
// }
}
});
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("name", "%s");
intent.putExtra("add", "%s");
intent.putExtra("phn", "%s");
intent.putExtra("sal", "%s");
intent.putExtra("lea", "%s");
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private String getRow(Genie g) {
return String.format("%s, %s, %s, %s, %s", g.name, g.salary, g.contact, g.paid_leaves, g.address);
}
}
}
Here's the viewGenie1.class:-
public class viewGenie1 extends AppCompatActivity implements View.OnClickListener {
TextView name;
EditText address, contact, salary, leaves;
Button attendance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_genie1);
name = (TextView) findViewById(R.id.txName);
address = (EditText) findViewById(R.id.txAddress);
contact = (EditText) findViewById(R.id.txContact);
salary = (EditText) findViewById(R.id.txSalary);
leaves = (EditText) findViewById(R.id.txLeaves);
Button update=(Button)findViewById(R.id.btUpdate);
update.setOnClickListener(this);
Button delete=(Button)findViewById(R.id.delete);
delete.setOnClickListener(this);
Button attendance = (Button) findViewById(R.id.attendance);
attendance.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAtt();
}
});
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
}
#Override
public void onClick(View view) {
final AlertDialog.Builder builder=new AlertDialog.Builder(viewGenie1.this);
builder.setMessage("Are you sure you want to delete records?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
new deleteTask().execute();
Toast.makeText(viewGenie1.this, "Genie deleted..!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(viewGenie1.this, navDrawer.class));
// GenieService genieService=new GenieService();
// genieService.delete(2);
// Log.d("Information", String.valueOf(genieService.delete(2)));
// Log.i("INFO", genies.get(0).name);
// startActivity(new Intent(viewGenie1.this,Test.class));
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private class deleteTask extends AsyncTask {
#Override
protected Object doInBackground(Object[] objects) {
GenieService genieService = new GenieService();
return genieService.delete(6);
}
}
public void showAtt() {
Intent intent = new Intent(this, viewAbsentee.class);
startActivity(intent);
}
}
Here's the xml file of the profile page I have created with hard coded values but want to display the actual values from the local mysql database using an API call:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bcak"
tools:context="com.codionics.geniem.AddGenie"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="313dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradientbackground"
android:orientation="vertical">
<ImageView
android:layout_width="117dp"
android:layout_height="117dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:src="#drawable/genie" />
<TextView
android:id="#+id/txName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Abc"
android:textColor="#ffffff"
android:textSize="21sp"
android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="115dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact"
android:textColor="#f000"
android:textStyle="bold"
android:textSize="20sp" />
<EditText
android:id="#+id/txContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="123456789"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#f000"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="#+id/txAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Pune"
android:textColor="#3F51B5"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<LinearLayout
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_attach_money_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Paid leaves : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txLeaves"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:layout_weight="1"
android:text=" 5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="#drawable/ic_money" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp"
android:text="Salary : "
android:textColor="#303F9F"
android:textSize="27dp"
android:textStyle="bold" />
<EditText
android:id="#+id/txSalary"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:text=" 5000"
android:textColor="#123" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="30dp"
android:background="#drawable/buttonstylegradient"
android:text="Update Genie"
android:textColor="#fff" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:layout_marginTop="-50dp"
android:background="#drawable/buttonstylegradient"
android:text="Delete Genie"
android:textColor="#fff" />
<Button
android:id="#+id/attendance"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstylegradient"
android:textColor="#fff"
android:text="Attendance" />
</LinearLayout>
I want to display the details in the a profile page like this:-
profile page
Please pass the value in Intent using putExtra()
Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("key","Value"); //Key must be unique and value should be the value which you want to pass to viewGenie1 class.
startActivity(intent);
In viewGenie1 class you can get the value like this
String value="";
if(getIntent().hasExtra("key")) {
value = getIntent().getExtras().getString("key");
}
Please replace
String value = "";
if (getIntent().hasExtra("name")) {
String name = getIntent().getExtras().getString("name");
String add = getIntent().getExtras().getString("add");
String phn = getIntent().getExtras().getString("phn");
String sal = getIntent().getExtras().getString("sal");
String lea = getIntent().getExtras().getString("lea");
}
name.setText(value);
address.setText(value);
contact.setText(value);
salary.setText(value);
leaves.setText(value);
To
String mName = "",mAdd="",mPhn="",mSal="",mLea="";
if (getIntent().hasExtra("name")) {
mName = getIntent().getExtras().getString("name");
mAdd = getIntent().getExtras().getString("add");
mPhn = getIntent().getExtras().getString("phn");
mSal = getIntent().getExtras().getString("sal");
mLea = getIntent().getExtras().getString("lea");
}
name.setText(mName);
address.setText(mAdd);
contact.setText(mPhn);
salary.setText(mSal);
leaves.setText(mLea);

How to add dynamic column to my table?

I have one form in my app,i am creating textview and edittext programmatically,as per user requirement,user can create number of textviews and edittext,on click of add button,now issue is i am not able to store all the values of dynamically created textviews and edittexts to my table...the values nameofevent,date,time,duration is storing properly in my table..following is image of my form..can any one tell what is the mistake?
Mainactivity.java
public class MainActivity extends Activity {
private Button addnewdata;
private Button submit;
private EditText edtnmofevent;
private EditText edtdtofevent;
private EditText edttmofevent;
private EditText edtdurationofevent;
private LinearLayout lnr;
private TextView valueTV;
private EditText edtvalues;
int totalFields = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addnewdata = (Button) findViewById(R.id.addnewdata);
submit = (Button) findViewById(R.id.btnsubmit);
edtnmofevent = (EditText) findViewById(R.id.edtnameofevent);
edtdtofevent = (EditText) findViewById(R.id.edtdateofevent);
edttmofevent = (EditText) findViewById(R.id.edttimeofevent);
edtdurationofevent = (EditText) findViewById(R.id.edtdurationofevent);
addnewdata.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
lnr = (LinearLayout) findViewById(R.id.addnewlinear);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(25, 0, 0, 0);
valueTV = new TextView(MainActivity.this);
// valueTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
valueTV.setText(userInput.getText());
valueTV.setLayoutParams(lp);
valueTV.setTextSize(18);
valueTV.setTextColor(Color.parseColor("#2d6cae"));
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp1.setMargins(25, 0, 25, 0);
lp1.height = 50;
edtvalues = new EditText(MainActivity.this);
edtvalues.setBackgroundResource(R.drawable.rect_edt);
edtvalues.setLayoutParams(lp1);
lnr.addView(valueTV);
lnr.addView(edtvalues);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String edcity=edtnmofevent.getText().toString();
String dtofevent = edtdtofevent.getText().toString();
String timeofevent = edttmofevent.getText().toString();
String duration = edtdurationofevent.getText().toString();
queries q=new queries(MainActivity.this);
// q.insert(edcity, dtofevent, timeofevent, duration);
String dlabl=valueTV.getText().toString();
String dedt=edtvalues.getText().toString();
q.insertevnt(dlabl,dedt);
System.out.print("values of textand edit"+valueTV.getText().toString());
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
queries.java
public class queries {
SQLiteDatabase sd;
public queries(Context c)
{
db_handler dh=new db_handler(c);
sd=dh.getWritableDatabase();
}
public void insert(String city,String dateofevent,String timeofevent,String durationofevent)
{
String query="insert into tbl_spin(city,dateofevent,timeofevent,durationofevent)values('"+city+"','"+dateofevent+"','"+timeofevent+"','"+durationofevent+"')";
sd.execSQL(query);
}
public Cursor select()
{
// String query="select tbl_spin.city from tbl_spin INNER JOIN tbl_event ON tbl_spin.id=tbl_event.id";
String query="select * from tbl_event";
//String query="select city from tbl_spin CROSS JOIN tbl_event";
Cursor c= sd.rawQuery(query, null);
return c;
}
public void insertevnt(String txtofevent,String edtofevent)
{
String querys="insert into tbl_event(txtofevent,edtofevent)values('"+txtofevent+"','"+edtofevent+"')";
sd.execSQL(querys);
}
/*public Cursor selectevent()
{
String querys="select * from tbl_event";
Cursor c= sd.rawQuery(querys, null);
return c;
}*/
}
activity_main
<?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"
android:background="#android:color/white"
>
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#000000"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="IT ADD$"
android:id="#+id/itaddestxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="UP"
android:textColor="#ffffff"
android:layout_toRightOf="#+id/itaddestxt"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="#+id/addnewlinear"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD NEW EVENT"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:id="#+id/txtaddnewevent"
android:textColor="#73b5fa"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtaddnewevent"
android:layout_marginTop="10dp"
android:id="#+id/bluelines"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of Event:*"
android:layout_below="#+id/bluelines"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtnameofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtnameofevent"
android:layout_below="#+id/txtnameofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Event:*"
android:layout_below="#+id/edtnameofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdateofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdateofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledtdate"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edtdateofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edtdateofevent"
android:layout_marginLeft="10dp"
android:id="#+id/calndrdat"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time of Event:*"
android:layout_below="#+id/reledtdate"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txttimeofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txttimeofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledttime"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edttimeofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edttimeofevent"
android:layout_marginLeft="10dp"
android:id="#+id/timepickrs"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duration of Event:*"
android:layout_below="#+id/edttimeofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdurationofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtdurationofevent"
android:layout_below="#+id/txtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/addnewdata"
android:text="Add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="#android:color/white"
android:layout_gravity="center"
android:id="#+id/btnsubmit"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
db_handler
public class db_handler extends SQLiteOpenHelper{
public db_handler(Context context
) {
super(context, "databs.db", null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tbl_spin(id integer primary key autoincrement,city text,dateofevent text,timeofevent text,durationofevent text)");
db.execSQL("create table tbl_event(id integer primary key autoincrement,txtofevent text,edtofevent text)");
// db.execSQL("CREATE TABLE COMPANY(ID INT PRIMARY KEY NOT NULL,NAME CHAR(50) NOT NULL,AGE CHAR(50) NOT NULL,ADDRESS CHAR(50) NOT NULL,SALARY CHAR(50) NOT NULL);");
// db.execSQL("CREATE TABLE DEPARTMENT(ID INT PRIMARY KEY NOT NULL,DEPT CHAR(50) NOT NULL,EMP_ID INT NOT NULL);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

How to overcome the situation when content of spinner is hidden?

I created an android application that takes some data using editText field such as Name, Contact, one radiogroup button, two buttons for save and view stored data and a spinner and stores these information in database when save button is pressed. Datas are displayed at the bottom of the screen when view button is pressed.Usually the spinner shows value 18 as by default.Everything is fine,when I open the application and press save button after entering the fields it saves and when once view button is pressed datas are displayed but I can not insert more data by opening the application once because the content of spinner is hidden now. To do that I need to close the application and then open it once again. How to overcome this problem. Anyone plz run the codes and see what is the problem
.xml file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textContcat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="#string/name"
android:textSize="20sp" />
<EditText
android:id="#+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/namehint"
android:inputType="textPersonName"
android:paddingRight="5dp"
android:textSize="20sp" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textAge"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="20dp"
android:text="#string/age"
android:textSize="20sp" />
<Spinner
android:id="#+id/spAge"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_weight="0.00"
android:clickable="true"
android:gravity="top|center"
android:paddingRight="20dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textContcat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="#string/contact"
android:textSize="20sp" />
<EditText
android:id="#+id/editContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/contacthint"
android:inputType="number"
android:paddingRight="5dp"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/sLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left|center"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:text="#string/sx"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/malebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/mal"
android:textSize="20sp" />
<RadioButton
android:id="#+id/femalebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/femal"
android:textSize="20sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/savebutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/save"
android:textSize="20sp" />
<Button
android:id="#+id/viewbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/view"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
</TableRow>
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/tvSQLinfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="#string/nullvalue"
android:textColor="#color/text_color"
android:textSize="20sp" >
</TextView>
</LinearLayout>
.java file:
public class SqlLiteExample extends Activity implements OnClickListener,
OnItemSelectedListener {
Button sqlUpdate, sqlView;
EditText etName, etContact;
RadioGroup myRadioGrp;
RadioButton rbtn1, rbtn2;
Spinner sp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
sqlUpdate = (Button) findViewById(R.id.savebutton);
etName = (EditText) findViewById(R.id.editName);
etContact = (EditText) findViewById(R.id.editContact);
rbtn1 = (RadioButton) findViewById(R.id.malebutton);
rbtn2 = (RadioButton) findViewById(R.id.femalebutton);
sqlView = (Button) findViewById(R.id.viewbutton);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
uI();
spElements();
}
private void spElements() {
// TODO Auto-generated method stub
List<String> ages = new ArrayList<String>();
for (int i = 18; i <= 99; i++) {
ages.add(String.valueOf(i));
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ages);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp.setAdapter(dataAdapter);
}
private void uI() {
// TODO Auto-generated method stub
sp = (Spinner) findViewById(R.id.spAge);
sp.setOnItemSelectedListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.savebutton:
boolean didWork = true;
try {
String name = etName.getText().toString();
String age = (String) sp.getSelectedItem();
String contact = etContact.getText().toString();
myRadioGrp = (RadioGroup) findViewById(R.id.RadioGroup01);
String sex = ((RadioButton) this.findViewById(myRadioGrp
.getCheckedRadioButtonId())).getText().toString();
MyDB entry = new MyDB(SqlLiteExample.this);
entry.open();
entry.createEntry(name, age, contact, sex);
entry.close();
} catch (Exception e) {
didWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
} finally {
if (didWork) {
Dialog d = new Dialog(this);
d.setTitle("Updated");
TextView tv = new TextView(this);
tv.setText("Succesfully");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.viewbutton:
Intent i = new Intent("com.bysakiralam.mydatabase.DISPLAYRECORDS");
startActivity(i);
break;
}
}
#Override
public void onItemSelected(AdapterView<?> main, View arg1, int position,
long Id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Displayrecords.java :
public class DisplayRecords extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
MyDB info = new MyDB(this);
try {
info.open();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String data = info.getData();
info.close();
tv.setText(data);
}
}
MyDB.java :
public class MyDB {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_AGE = "persons_age";
public static final String KEY_CONTACT = "persons_contact";
public static final String KEY_SEX = "gender";
private static final String DATABASE_NAME = "MyDatabase";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_AGE + " TEXT NOT NULL, "
+ KEY_CONTACT + " TEXT NOT NULL, " + KEY_SEX
+ " TEXT NOT NULL)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public MyDB(Context c) {
ourContext = c;
}
public MyDB open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String name, String age, String contact, String sex) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_AGE, age);
cv.put(KEY_CONTACT, contact);
cv.put(KEY_SEX, sex);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_AGE,
KEY_CONTACT, KEY_SEX };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iAGE = c.getColumnIndex(KEY_AGE);
int iCONTACT = c.getColumnIndex(KEY_CONTACT);
int iSEX = c.getColumnIndex(KEY_SEX);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + "\t\t" + c.getString(iName)
+ "\t" + c.getString(iAGE) + "\t\t" + c.getString(iCONTACT)
+ "\t" + c.getString(iSEX) + "\n";
}
return result;
}
}
Maybe you have to reset the spinner once you save or view an entry? (Perhaps in the onClick methods of the save/view buttons)
Also the xml file doesn't seem right as you have a Linear Layout within a ScrollView.

Android how to create clickable menu from ListView and SimpleMenuAdapter

I have problem about SimpleCursorAdapter and ListView. When I want to create dynamic list menu by query from database. The problem is the listview cannot set onClickListener to do something when user click. These are my code.
In file "menu_header.xml"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:background="#drawable/menu_background">
<Button
android:id="#+id/bt_back"
android:layout_width="70dip"
android:layout_height="40dip"
android:layout_centerVertical="true"
android:text="#string/menu_back" />
<ImageButton
android:id="#+id/bt_search"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="#null"
android:src="#android:drawable/ic_menu_search"
android:layout_alignParentRight="true"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
</RelativeLayout>
file "menu_wrapper.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/menu_header" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_below="#id/menu_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
In file "menu_choice.xml"
<?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="wrap_content"
android:background="#drawable/menu_selector"
android:clickable="true"
android:padding="10dip">
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="10dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="60dip"
android:layout_height="60dip"
android:src="#drawable/building" />
</LinearLayout>
<TextView
android:id="#+id/eng_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Building"
android:textColor="#040404"
android:typeface="sans"
android:paddingTop="5dip"
android:textSize="20dip"
android:textStyle="bold" />
<TextView
android:id="#+id/thai_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/eng_name"
android:textColor="#343434"
android:textSize="15dip"
android:layout_marginTop="3dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Test" />
<ImageView android:layout_width="15dip"
android:layout_height="15dip"
android:src="#drawable/next_arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
and the last one is the file that I use to create the menu
public class Menu extends ListActivity {
private ListView listView;
private ImageButton imageButtonSearch;
private Button buttonBack;
private Constants constants = Constants.getInstance();
private Database database = new Database(this);
private Beans beans = Beans.getInstance();
private Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_wrapper);
buttonBack = (Button)findViewById(R.id.bt_back);
imageButtonSearch = (ImageButton)findViewById(R.id.bt_search);
buttonBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
imageButtonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(new Intent(context, Search.class), beans.REQUEST_CODE);
}
});
listView = this.getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.d("Item Click","item click");
}
});
database.open();
Cursor cursorPlaceType = database.getPlaceType();
startManagingCursor(cursorPlaceType);
if(cursorPlaceType != null){
String[] columns = new String[]{ constants.PLACE_TYPE_IMAGE, constants.PLACE_TYPE_ENAME, constants.PLACE_TYPE_TNAME };
int[] to = new int[]{ R.id.list_image, R.id.eng_name, R.id.thai_name };
SimpleCursorAdapter menuAdapter = new SimpleCursorAdapter(this, R.layout.menu_choice, cursorPlaceType, columns, to);
menuAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view.getId() == R.id.list_image){
try {
String imageFile = constants.IMAGE_ASSETS + cursor.getString(columnIndex) + ".png";
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(imageFile));
((ImageView)view).setImageBitmap(bitmap);
} catch (IOException e) { e.printStackTrace(); }
return true;
}//end if
return false;
}//end setViewValue
});
this.setListAdapter(menuAdapter);
}else{
new AlertDialog.Builder(this)
.setMessage("Try again")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
}).show();
}
}//end onCreate()
Thank you indeed for your help.
Use protected void onListItemClick(ListView l, View v, int position, long id) this method of ListActivity instead of your listView.setOnItemClickListener method.

Populate android contacts

i want to choose the contacts from a list view (which seems to work) - and populate the chosen contact in another ui (and then send a generated message to the chosen contact by sms). The populateField method doesn't work.
Here snippets from my code:
the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/yellow"
android:orientation="vertical" >
<TextView
android:id="#+id/label_sms_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_sms_number"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/display_sendSms_name_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/display_sendSms_contact_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/label_sms_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_sms_message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<EditText
android:id="#+id/input_sms_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_sms_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_sms_button_send" />
</LinearLayout>
my java code:
public class SendSMS extends Activity{
private IShoppingListDao shoppingManager;
private TextView mContact;
private TextView mMessage;
private Long mRowId;
private static final String TAG = "SendSMS";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_list_by_sms);
shoppingManager = new ShoppingListDao(this);
shoppingManager.open();
mContact = (TextView) findViewById(R.id.display_sendSms_name_contact);
mRowId = null;
Bundle extras = getIntent().getExtras();
mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
.getSerializable(ContactsContract.Contacts.DISPLAY_NAME);
Log.d(TAG, "Row-id: "+mRowId);
if (extras != null) {
mRowId = extras.getLong(ContactsContract.Contacts.DISPLAY_NAME);
}
mMessage = (TextView) findViewById(R.id.input_sms_message);
mMessage.setText(generateMessage());
populateFields();
}
private void populateFields() {
Log.d(TAG,"Entering populateFields");
Log.d(TAG,"Row-id: "+mRowId);
if (mRowId != null) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Contacts._ID + " = " + mRowId;
Log.d(TAG,"String where: "+where);
//selecting the name
Cursor listItem = cr.query(
ContactsContract.Contacts.CONTENT_URI,
null,
where,
null, null);
startManagingCursor(listItem);
mContact.setText(listItem.getString(
listItem.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
}
}

Categories

Resources