列表视图行在进入下一个活动时重复并返回

时间:2014-03-11 05:37:28

标签: android listview

public class abc extends Activity {

    String accountId;
    ListView lv;
    ArrayList<Map<String, String>> lv2; 
    databaseone db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.abc);
            lv=(ListView) this.findViewById(R.id.listCheque);
            lv2 = new ArrayList<Map<String, String>>();
            lv.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View selectedView, int arg2,long arg3) {
            TextView  textAccountId = (TextView) selectedView.findViewById(R.id.textAccNo);
            //Log.d("Accounts", "Selected Account Id : " + textAccountId.getText().toString());
            Intent intent = new Intent(getApplicationContext(), Nextactivity.class);
            intent.putExtra("accountid", lv2 .get(arg2).get("id"));
            startActivity(intent);
        }
    });
    }
    @Override
    protected void onStart() {
            // TODO Auto-generated method stub
    super.onStart();
    db=new databaseone(getApplicationContext());
    Cursor c=null;
    ArrayList<Map<String, String>> lv2 = new ArrayList<Map<String, String>>();
    c=db.viewdata(accountId);
    while (c.moveToNext()) {
        LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>();
        String n=c.getString(c.getColumnIndex("id"));
        String p=c.getString(c.getColumnIndex("cheqgiven"));
        String a=c.getString(c.getColumnIndex("cheqamt"));
        tran.put("id", n);
        tran.put("cheqgiven", p);
        tran.put("cheqamt", a);
        lv2.add(tran);
    }
    c.close();
    db.close();

    SimpleAdapter adapter = new SimpleAdapter(this, lv2,
            R.layout.chequelist, new String[] { "id",
                    "cheqgiven","cheqamt" }, new int[] { R.id.textAccNo, R.id.textName,R.id.textTransType});

    lv.setAdapter(adapter);
} }

Nextactivity.class

     @Override
     protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    accountId = this.getIntent().getStringExtra("accountid");
    Log.v("one",accountId);
    db=new databaseone(getApplicationContext());
    Cursor cur=null;
    cur=db.viewdata(null);
    if (cur.moveToFirst()) {

        // update view
        cheqgiven.setText(cur.getString(cur
                .getColumnIndex("cheqgiven")));
        cheqamt.setText(cur.getString(cur
                .getColumnIndex("cheqamt")));
        cheqdate.setText(cur.getString(cur
                .getColumnIndex("cheqdate")));
        cheqno.setText(cur.getString(cur
                .getColumnIndex("cheqno")));
        remarks.setText(cur.getString(cur
                .getColumnIndex("remarks")));
        }
            cur.close();    
    db.close();
        }}

数据库:

  @Override
  public void onCreate(SQLiteDatabase db) {

     // TODO Auto-generated method stub
      db.execSQL("create table if not exists "+TABLE_NAME+" (id integer primary key   autoincrement,cheqgiven text,cheqamt text,cheqdate text,cheqno text,remarks text)");
   }


  @Override
 public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    // TODO Auto-generated method stub

    db.execSQL("drop table if exists "+TABLE_NAME);

    onCreate(db);
}
 public Cursor viewdata(String acct) {
    // TODO Auto-generated method stub
    sdb = this.getReadableDatabase();
    String where = (acct == null) ? "" : " where accountid = " + acct;
    Cursor c=sdb.rawQuery("select * from " + TABLE_NAME + where, null);
    return c;

} 当我进入下一个活动并返回时,Listview行正在重复,当listview onclick..can任何人帮助时,我想向下一个活动edittext显示特定的id相关细节。

Logcat:

03-11 12:13:47.184: D/dalvikvm(423): GC_EXTERNAL_ALLOC freed 739 objects / 56600 bytes in 78ms
03-11 12:13:51.094: V/one(423): 1
03-11 12:13:51.114: I/Database(423): sqlite returned: error code = 1, msg = no such column: accountid
03-11 12:13:51.124: D/AndroidRuntime(423): Shutting down VM
03-11 12:13:51.124: W/dalvikvm(423): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-11 12:13:51.154: E/AndroidRuntime(423): FATAL EXCEPTION: main
03-11 12:13:51.154: E/AndroidRuntime(423): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.campusrain.chequemanager/com.campusrain.chequemanager.Marking}: android.database.sqlite.SQLiteException: no such column: accountid: , while compiling: select * from cheque where accountid = 1
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.os.Looper.loop(Looper.java:123)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 12:13:51.154: E/AndroidRuntime(423):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:13:51.154: E/AndroidRuntime(423):  at java.lang.reflect.Method.invoke(Method.java:521)
03-11 12:13:51.154: E/AndroidRuntime(423):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-11 12:13:51.154: E/AndroidRuntime(423):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-11 12:13:51.154: E/AndroidRuntime(423):  at dalvik.system.NativeStart.main(Native Method)
03-11 12:13:51.154: E/AndroidRuntime(423): Caused by: android.database.sqlite.SQLiteException: no such column: accountid: , while compiling: select * from cheque where accountid = 1
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
03-11 12:13:51.154: E/AndroidRuntime(423):  at com.campusrain.chequemanager.databaseone.viewdata(databaseone.java:62)
03-11 12:13:51.154: E/AndroidRuntime(423):  at com.campusrain.chequemanager.Marking.onStart(Marking.java:43)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.Activity.performStart(Activity.java:3781)
03-11 12:13:51.154: E/AndroidRuntime(423):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
03-11 12:13:51.154: E/AndroidRuntime(423):  ... 11 more

1 个答案:

答案 0 :(得分:1)

清除第一个Activity onStart()

上的ArrayList
@Override
    protected void onStart() {
    super.onStart();
    lv2.removeAll(lv2);
    db=new databaseone(getApplicationContext());
    Cursor c=null;
    c=db.viewdata(accountId);
    while (c.moveToNext()) {
        LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>();
        String n=c.getString(c.getColumnIndex("id"));
        String p=c.getString(c.getColumnIndex("cheqgiven"));
        String a=c.getString(c.getColumnIndex("cheqamt"));
        tran.put("id", n);
        tran.put("cheqgiven", p);
        tran.put("cheqamt", a);
        lv2.add(tran);
    }
    c.close();
    db.close();

    SimpleAdapter adapter = new SimpleAdapter(this, lv2,
            R.layout.chequelist, new String[] { "id",
                    "cheqgiven","cheqamt" }, new int[] { R.id.textAccNo, R.id.textName,R.id.textTransType});

    lv.setAdapter(adapter);
} }

第二项活动:

@Override
     protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    accountId = this.getIntent().getStringExtra("accountid");
    Log.v("one",accountId);
    db=new databaseone(getApplicationContext());
    Cursor cur=null;
    cur=db.viewdata(accountId ); //Change the line
    if (cur.moveToFirst()) {

        // update view
        cheqgiven.setText(cur.getString(cur
                .getColumnIndex("cheqgiven")));
        cheqamt.setText(cur.getString(cur
                .getColumnIndex("cheqamt")));
        cheqdate.setText(cur.getString(cur
                .getColumnIndex("cheqdate")));
        cheqno.setText(cur.getString(cur
                .getColumnIndex("cheqno")));
        remarks.setText(cur.getString(cur
                .getColumnIndex("remarks")));
        }
            cur.close();    
    db.close();
        }}

然后在数据库中

@Override
  public void onCreate(SQLiteDatabase db) {

     // TODO Auto-generated method stub
      db.execSQL("create table if not exists "+TABLE_NAME+" (id integer primary key     autoincrement,cheqgiven text,cheqamt text,cheqdate text,cheqno text,remarks text)");
   }


  @Override
 public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    // TODO Auto-generated method stub

    db.execSQL("drop table if exists "+TABLE_NAME);

    onCreate(db);
}
 public Cursor viewdata(String acct) {
    // TODO Auto-generated method stub
    sdb = this.getReadableDatabase();

    //Change bellow line to id
    String where = (acct == null) ? "" : " where id = " + acct;

    Cursor c=sdb.rawQuery("select * from " + TABLE_NAME + where, null);
    return c;