我是一个新的安卓开发者。我有一些大问题,因此我不继续我的项目。我想获取联系人信息,然后将这些信息设置为 sqlite。此步骤有效,但是当我尝试在 listview 中列出时,这里不起作用是我的代码,你能帮帮我吗?
这是我的行布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFDAFF7F"
android:padding="8dp">
<LinearLayout android:id="@+id/Text"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F3300"
android:textSize="20dip"
android:textStyle="italic"
/>
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#FF267F00"
android:paddingLeft="100dip"
/>
</LinearLayout>
</RelativeLayout >
这是我的活动布局
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这是我的 sqlite 代码
package com.example.myprojects;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class contacts_nick {
//private static final String TAG = contacts_nick.class.getSimpleName();
public static final String KEY_ID="id";
public static final String KEY_NAME="name";
public static final String KEY_PHONE="phone";
public static final String KEY_NICK="nick";
private static final String DATABASE_NAME="mydatabase";
private static final String DATABASE_TABLE="Contacts";
private static final int DATABASE_VERSION=1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
//To create db
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " +DATABASE_TABLE+ " (" +KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" TEXT, "+KEY_PHONE+" TEXT, "+KEY_NICK+" TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public contacts_nick(Context c){
ourContext=c;
}
public contacts_nick open() throws SQLException{
ourHelper=new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long insert (String displayName, String phoneNumber) throws SQLException {
ContentValues cv=new ContentValues();
cv.put(KEY_NAME, displayName);
cv.put(KEY_PHONE, phoneNumber);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() throws SQLException {
// TODO Auto-generated method stub
String[] columns =new String[]{KEY_ID,KEY_NAME,KEY_PHONE};
Cursor c=ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
int id=c.getColumnIndex(KEY_ID);
int name=c.getColumnIndex(KEY_NAME);
int phone=c.getColumnIndex(KEY_PHONE);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result=result+c.getString(id)+","+c.getString(name)+" "+c.getString(phone)+"\n";
}
return result;
}
public String search(int l) throws SQLException{
String[] columns =new String[]{KEY_ID,KEY_NAME,KEY_PHONE};
Cursor c=ourDatabase.query(DATABASE_TABLE, columns,KEY_ID +"="+l, null, null, null, null);
if(c!=null){
c.moveToFirst();
String name=c.getString(1);
return name;
}
return null;
}
public int getrowcount(String name,String phone){
int result;
String[] columns =new String[]{KEY_ID,KEY_NAME,KEY_PHONE};
Cursor c=ourDatabase.query(DATABASE_TABLE, columns,KEY_NAME +"="+"'"+name+"'"+" AND "+KEY_PHONE+"="+ "'"+phone+"'", null, null, null, null);
result=c.getCount();
return result;
}
public void update(long id, String name, String phone) throws SQLException{
ContentValues cvUpdate=new ContentValues();
cvUpdate.put(KEY_ID,id);
cvUpdate.put(KEY_NAME,name);
cvUpdate.put(KEY_PHONE,phone);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ID+"="+id, null);
}
public void delete(long l) throws SQLException{
ourDatabase.delete(DATABASE_TABLE, KEY_ID+"="+l, null);
}
public Cursor getAllData () {
ourDatabase=ourHelper.getReadableDatabase();
String[] columns =new String[]{KEY_ID,KEY_NAME,KEY_PHONE};
Cursor cur=ourDatabase.query(DATABASE_TABLE,columns, null,null, null, null, null);
return cur;
}
}
这是我的 main_activity
package com.example.myprojects;
import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class edit_contacts extends ListActivity {
cursor customadapter;
TextView contactView;
TextView s1;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
contactView = (TextView) findViewById(R.id.textView1);
s1=(TextView)findViewById(R.id.textView2);
list=(ListView)findViewById(android.R.id.list);
boolean diditwork=true;
try{
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts_nick entry=new contacts_nick(this);
entry.open();
if(entry.getrowcount(displayName,phoneNumber)==0){
entry.insert(displayName,phoneNumber);
entry.close();
}
else{
Dialog d=new Dialog(this);
d.setTitle("Error");
TextView tv=new TextView(this);
tv.setText("KAYIT VAR");
d.setContentView(tv);
d.show();
entry.close();
}
}
}
catch(Exception e){
diditwork=false;
String error =e.toString();
Dialog d=new Dialog(this);
d.setTitle("Sorun Var");
TextView tv=new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
finally{
if(diditwork){
Dialog d=new Dialog(this);
d.setTitle("Tebrikler");
TextView tv=new TextView(this);
tv.setText("Başaralı");
d.setContentView(tv);
d.show();
}
}
contacts_nick info=new contacts_nick(this);
Cursor c = info.getAllData();
String[] columns = new String[] { contacts_nick.KEY_ID, contacts_nick.KEY_NAME,contacts_nick.KEY_PHONE };
int[] to = new int[] { R.id.name, R.id.phone };
//@SuppressWarnings("deprecation")
ListAdapter simpCurAdap = new SimpleCursorAdapter(this, R.layout.activity_row, c, columns, to,0);
setListAdapter(simpCurAdap);
}
}