I want to make a program that read data from contact list and add them to the database and then update a listview that contain selected data. but every time I click add button and contact list open and i select one item, listview doesn't change. I also don't know where the database is stored. main activity:
public class BlockActivity extends Activity {
ArrayList<String> contactnumber= new ArrayList<String>();
ArrayList<String> contactname= new ArrayList<String>();
public static SQLiteDatabase database;
SQLiteDatabase myDB= null;
CustomAdapter adapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pickContact = (Button) findViewById(R.id.button1);
pickContact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
null, null, null);
if (c != null && c.moveToFirst()) {
Log.i("error", "salalalalal");
String number = c.getString(0);
String name = c.getString(1);
manageDatabase(name, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public void manageDatabase(String name, String number) {
String TableName = "contact";
/* Create a Database. */
try {
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
/* Create a Table in the Database. */
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (" +
"name TEXT, " +
"number TEXT" +
");"
);
/* Insert data to a Table*/
myDB.execSQL("INSERT INTO "
+ TableName
+ "(name,number)"
+ " VALUES ('name','number');");
// retrieve data from database
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
// Check if our result was valid.
c.moveToFirst();
if(c.moveToNext()){
while(!c.isAfterLast())
{
Log.i("error",c.getString(0));
Log.i("error",c.getString(1) );
contactname.add(c.getString(0));
contactnumber.add(c.getString(1));
c.moveToNext();
}}
//fill the list view with results from database
ListView lv=(ListView) findViewById(R.id.listView1);
adapter = new CustomAdapter(this, contactname, contactnumber);
lv.setAdapter(adapter);
}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
}
}
here is custom adapter:
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> nameList;
private final ArrayList<String> phoneList;
public CustomAdapter(Activity context,ArrayList<String> nameList, ArrayList<String> phoneList) {
super(context, R.layout.detail, nameList);
this.context = context;
this.nameList = nameList;
this.phoneList =phoneList;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.detail, null, true);
TextView word1 = (TextView) rowView.findViewById(R.id.one);
TextView word2 = (TextView) rowView.findViewById(R.id.two);
word1.setText(nameList.get(position));
word2.setText(phoneList.get(position));
return rowView;
}
}
main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
detail.xml file:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:id="@+id/one"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="30dip" />
<TextView
android:id="@+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="30dip" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="2dip" android:background="#ff7f7f"/>
Aucun commentaire:
Enregistrer un commentaire