Am new in android programming and am making an application to save contacts in a sqlite database. So my problem is producing a pop up menu when the list is clicked. In my coresponding xml layout file I have the android:onClick="Show_popup_menu". I want it to be such in a way that when you click on an saved item in the database list, it should show a popup menu with 3 options 1. Call selected number 2. SMS selected number 3. Email selected address
I tried all I can but failed, this is my database.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#c7c7c7" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#C7C7C7"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:id="@+id/add_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:text="Add Colleague"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:background="@+drawable/green_btn"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:onClick="show_popup_menu"
android:background="#c7c7c7">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
</LinearLayout>
The Database.java class looks like this
package com.Benz_Narankz.MyContactDbApp;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.net.Uri;
public class Database extends Activity {
Button add_btn;
ListView Contact_listview;
ArrayList<Contact> contact_data = new ArrayList<Contact>();
Contact_Adapter cAdapter;
DatabaseHandler db;
String Toast_msg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
try {
Contact_listview = (ListView) findViewById(R.id.list);
Contact_listview.setItemsCanFocus(false);
add_btn = (Button) findViewById(R.id.add_btn);
Set_Referash_Data();
} catch (Exception e) {
// TODO: handle exception
Log.e("some error", "" + e);
}
add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent add_user = new Intent(Database.this,
Add_Update_User.class);
add_user.putExtra("called", "add");
add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(add_user);
finish();
}
});
}
public void show_popup_menu(View v)
{
/*I think something i dont know should be here */
}
public void Set_Referash_Data() {
contact_data.clear();
db = new DatabaseHandler(this);
ArrayList<Contact> contact_array_from_db = db.Get_Contacts();
for (int i = 0; i < contact_array_from_db.size(); i++) {
int tidno = contact_array_from_db.get(i).getID();
String name = contact_array_from_db.get(i).getName();
String mobile = contact_array_from_db.get(i).getPhoneNumber();
String email = contact_array_from_db.get(i).getEmail();
Contact cnt = new Contact();
cnt.setID(tidno);
cnt.setName(name);
cnt.setEmail(email);
cnt.setPhoneNumber(mobile);
contact_data.add(cnt);
}
db.close();
cAdapter = new Contact_Adapter(Database.this, R.layout.listview_row,
contact_data);
Contact_listview.setAdapter(cAdapter);
cAdapter.notifyDataSetChanged();
}
public void Show_Toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Set_Referash_Data();
}
public class Contact_Adapter extends ArrayAdapter<Contact> {
Activity activity;
int layoutResourceId;
Contact user;
ArrayList<Contact> data = new ArrayList<Contact>();
public Contact_Adapter(Activity act, int layoutResourceId,
ArrayList<Contact> data) {
super(act, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.data = data;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.name = (TextView) row.findViewById(R.id.user_name_txt);
holder.email = (TextView) row.findViewById(R.id.user_email_txt);
holder.number = (TextView) row.findViewById(R.id.user_mob_txt);
holder.edit = (Button) row.findViewById(R.id.btn_update);
holder.delete = (Button) row.findViewById(R.id.btn_delete);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
user = data.get(position);
holder.edit.setTag(user.getID());
holder.delete.setTag(user.getID());
holder.name.setText(user.getName());
holder.email.setText(user.getEmail());
holder.number.setText(user.getPhoneNumber());
holder.edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Edit Button Clicked", "**********");
Intent update_user = new Intent(activity,
Add_Update_User.class);
update_user.putExtra("called", "update");
update_user.putExtra("USER_ID", v.getTag().toString());
activity.startActivity(update_user);
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
// show a message while loader is loading
AlertDialog.Builder adb = new AlertDialog.Builder(activity);
adb.setTitle("Delete?");
adb.setIcon(android.R.drawable.ic_menu_delete);
adb.setMessage("Are you sure you want to delete?");
final int user_id = Integer.parseInt(v.getTag().toString());
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Yes, delete",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// MyDataObject.remove(positionToRemove);
DatabaseHandler dBHandler = new DatabaseHandler(
activity.getApplicationContext());
dBHandler.Delete_Contact(user_id);
Database.this.onResume();
}
});
adb.show();
}
});
return row;
}
class UserHolder {
TextView name;
TextView email;
TextView number;
Button edit;
Button delete;
}
}
}
Any help will be appreciated. Thanks
No comments:
Post a Comment