您的表中必须有 _id 列。这是其背后的主要原因。
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.clientview);
cdb=new ClientDatabase(this);
Bundle bundle = getIntent().getExtras();
stt = bundle.getString("stuff");
a=stt.substring(0,(stt.indexOf("@")));
a1=stt.substring((stt.indexOf("@")+1), (stt.length()));
cdb.open();
clientviewlist();
}
private void clientviewlist() {
// TODO Auto-generated method stub
Cursor cur=cdb.getallclient(a,a1);
String[] columnn=new String[]{ClientDatabase.CLIENT_CODE,ClientDatabase.CLIENT_NAME};
int[] to=new int[]{R.id.client1,R.id.client2};
dataaDapter=new SimpleCursorAdapter(this,R.layout.clintlist,cur,columnn,to,0);
final ListView listview=(ListView)findViewById(R.id.listView1);
listview.setAdapter(dataaDapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int positions,
long id) {
tv = (TextView)arg1.findViewById(R.id.client1);
st=tv.getText().toString();
System.out.println(st);
Intent intent =new Intent(Client.this,Server.class);
// System.out.println(selectedFromList+"------------444444444444444444");
}
});
和数据库代码是
public class ClientDatabase {
//public static final String CLIENT_ID="_id";
public static final String CLIENT_CODE="_id";
public static final String DISTT_CODE="_distcode";
public static final String CLIENT_NAME="_name";
public static final String TOWN_CODE="_townid";
private static final String DATABASE_NAME="combinedclient.db";
private static final String TABLE_NAME="clientdata";
private static final int DATABASE_VERSION=1;
public static SQLiteDatabase db;
private final Context context;
private DatabaseHelper dbheLper;
private static final String TAG = "ClientsDbAdapter";
public ClientDatabase(Context context){
this.context=context;
dbheLper=new DatabaseHelper(context);
}
public class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null,DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME +"(" + CLIENT_CODE + " TEXT NOT NULL," + CLIENT_NAME + " TEXT NOT NULL," + DISTT_CODE + " INTEGER NOT NULL," + TOWN_CODE + " INTEGER NOT NULL)");
}// "CREATE TABLE " + TABLE_NAME +"(" + TOWN_ID + " INTEGER ," + TOWN_CODE + " INTEGER NOT NULL," + TOWN_NAME + " TEXT NOT NULL," + DISTT_CODE + " TEXT NOT NULL," + TOWNDISTT_CODE + " INTEGER PRIMARY KEY)";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE If EXISTS" +TABLE_NAME);
}
}
public ClientDatabase open() throws SQLException{
db=dbheLper.getWritableDatabase();
return this;
// TODO Auto-generated method stub
}
public Cursor getallclient(String diss, String tiw) {
// TODO Auto-generated method stub
Cursor nCursor=db.query(TABLE_NAME, new String[] {CLIENT_CODE,CLIENT_NAME},DISTT_CODE + "='" + diss + "' AND " + TOWN_CODE +"='" + tiw + "'", null, null, null, null);
if (nCursor != null) {
nCursor.moveToFirst();
}
return nCursor;
}
public void close() {
dbheLper.close();
}
public void insertvalues(String id, String namee, String dist,
String townv) {
try {
ContentValues initial=new ContentValues();
initial.put(DISTT_CODE,dist);
initial.put(CLIENT_CODE,id);
initial.put(CLIENT_NAME,namee);
initial.put(TOWN_CODE,townv);
db.insert(TABLE_NAME, null, initial);
} catch (Exception e) {
// TODO: handle exception
Log.d("Error is===", e.toString());
System.out.println("error in insertinggggggggggggg");
}
}
public void delete() {
// TODO Auto-generated method stub
db.delete(TABLE_NAME,null, null);
}
public String getclient(String clientcode) {
// TODO Auto-generated method stub
Cursor cursor=db.query(TABLE_NAME, null,CLIENT_CODE + "='" + clientcode + "'", null, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "not exist";
}
cursor.moveToFirst();
String name= cursor.getString(cursor.getColumnIndex(CLIENT_NAME));
cursor.close();
System.out.println(name);
return name;
}