Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid simple database adapter not working
    text
    copied!<p>i am a new android developer. i have some big problems for this reason i dont continue my project. i want to get contacts info then set these info to the sqlite. this step is working but when i try to list in listview dont work here is my code can u help me ?</p> <p>this is my row layout</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;LinearLayout android:id="@+id/Text" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dip"&gt; &lt;TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FF7F3300" android:textSize="20dip" android:textStyle="italic" /&gt; &lt;TextView android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14dip" android:textColor="#FF267F00" android:paddingLeft="100dip" /&gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout &gt; </code></pre> <p>this is my activity layout </p> <pre><code>&lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /&gt; &lt;ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>this is my sqlite code</p> <pre><code>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; } } </code></pre> <p>and this is my main_activity</p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload