该应用程序用于存储用户的联系人,并在打开ViewContacts页面(我在下面附带的java文件中)时显示它。 我想将检索到的值转换为字符串,以便每个值都可以作为参数传递给ArrayAdapter,以在ListView中显示值。
目前,我正在使用此代码段来执行此任务,但是,我的应用程序正在崩溃此声明。
if(c.moveToFirst())
{
do{
str.add(c.getString(0));
}while(c.moveToNext());
}
请建议我执行此转换的正确方法。我的目的只是打印一列,即" name"来自页面上的数据库。
package app.shriram;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class ViewContacts extends AppCompatActivity {
ListView contactList;
SQLiteDatabase db;
ArrayList<String> str = new ArrayList<String>();
String sql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_contacts);
contactList = (ListView)findViewById(R.id.contact_list_view);
db = openOrCreateDatabase("db_addressBook", MODE_PRIVATE, null);
SharedPreferences pref = getSharedPreferences("ShriRam", MODE_PRIVATE);
String emailId = pref.getString("Email", "");
db.execSQL("CREATE TABLE IF NOT EXISTS tbl_contacts (cname varchar(100)," +
"cmobile varchar(20), cemail varchar(100), uemail varchar(100))");
sql = "SELECT name FROM tbl_contacts WHERE uemail = '" + emailId + "'";
Cursor c = db.rawQuery(sql, null);
if(c.moveToFirst())
{
do{
str.add(c.getString(0));
}while(c.moveToNext());
}
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, str);
contactList.setAdapter(adpt);
}
public void back_arrow_fn2(View view) {
startActivity(new Intent(this, HomePage.class));
}
//need to have a delete option
//need to dynamically created markers for contacts to delete them selectively.
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".ViewContacts">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:onClick="back_arrow_fn2"
android:layout_width="52dp"
android:layout_height="match_parent"
android:src="@drawable/ic_arrow_back_black_24dp"
android:layout_weight="0.02" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY ADDRESS BOOK"
android:gravity="center"
android:textSize="20dp"
android:textStyle="italic"
android:padding="15dp"
android:layout_weight="0.96"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.02"
android:src="@drawable/ic_exit_to_app_black_24dp"
android:background="#EBF5FB"/>
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/contact_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>