我想制作一个程序,从联系人列表中读取数据并将它们添加到数据库中,然后更新包含所选数据的列表视图。但每次我点击添加按钮和联系人列表打开,我选择一个项目,列表视图不会更改。我也不知道数据库的存储位置。 主要活动:
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();
}
}
}
这是自定义适配器:
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文件:
<?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="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文件:
<?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" >
<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"/>
答案 0 :(得分:0)
要在View中更新数据,您应该调用他的适配器。
((ArrayAdapter) mMyListView.getAdapter()).notifyDataSetChanged();