检索数据表单数据库后列表视图不更新
Posted
技术标签:
【中文标题】检索数据表单数据库后列表视图不更新【英文标题】:listview doesn't update after retrieving data form database 【发布时间】:2015-12-17 04:53:16 【问题描述】:我想制作一个程序,从联系人列表中读取数据并将它们添加到数据库中,然后更新包含所选数据的列表视图。但是每次我单击添加按钮并打开联系人列表并选择一项时,列表视图都不会改变。我也不知道数据库存储在哪里。 主要活动:
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_
android:layout_
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:text="add" />
<ListView
android:id="@+id/listView1"
android:layout_
android:layout_ >
</ListView>
detail.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1" >
<TextView
android:id="@+id/one"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="30dip" />
<TextView
android:id="@+id/two"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="center"
android:text="name"
android:textSize="30dip" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_
android:layout_ android:background="#ff7f7f"/>
【问题讨论】:
【参考方案1】:要更新 View 中的数据,你应该调用他的适配器。
((ArrayAdapter) mMyListView.getAdapter()).notifyDataSetChanged();
【讨论】:
我应该在哪里使用它? 数据下载后。我在您的代码中找不到这一点。 我不下载数据库。它只使用查询。 那我不明白你的问题。什么时候升级到 View? 列表视图更新错误。此行contactname.add(c.getString(0));只给第一行。 c.moveToNext();没有任何效果。【参考方案2】:如果数据不稳定,请使用 RecyclerView!
【讨论】:
以上是关于检索数据表单数据库后列表视图不更新的主要内容,如果未能解决你的问题,请参考以下文章