单击listview中的项目时不会进入下一个活动。

时间:2012-08-10 12:55:17

标签: android listviewitem sqlite

我是android新手,使用sqlite在数据库中插入数据,并在自定义列表视图中使用这些数据。当我在下一个活动中使用编辑按钮编辑数据时出现问题,显示行字符串但是,在每行编辑但显示第1行字符串的数据。  通过图像你可以理解: enter image description here

我希望在点击编辑时新活动页面中的行名称。 当我按下删除时删除行..帮帮我..

editlist java file

public class EditLIST extends Activity {


    private AndroidSQLite mySQLiteAdapter ;
final static String EXTRA_MESSAGE = "edit.list.message";
 public void onClick (View view) {


     Intent intent = new Intent (this,display.class);
     TextView textView=(TextView) findViewById(R.id.textView1);
     String message=textView.getText().toString();


     //data.putString("thetext",text);
     intent.putExtra(EXTRA_MESSAGE,message);
     startActivity(intent);
}
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_list);
        ListView listContent = (ListView)findViewById(R.id.listView1);



        mySQLiteAdapter = new AndroidSQLite(this);
        mySQLiteAdapter.openToWrite();
      mySQLiteAdapter.deleteAll();

       mySQLiteAdapter.insert("umesh");
       mySQLiteAdapter.insert("kalpesh");
       mySQLiteAdapter.insert("manish");


        mySQLiteAdapter.close();

        mySQLiteAdapter = new AndroidSQLite(this);
        mySQLiteAdapter.openToRead();

        Cursor cursor = mySQLiteAdapter.queueAll();
        startManagingCursor(cursor);

        String[] from = new String[]{AndroidSQLite.KEY_FNAME};
        int[] to = new int[]{R.id.textView1};




        SimpleCursorAdapter cursorAdapter =
         new SimpleCursorAdapter(this, R.layout.button, cursor, from, to);


                  listContent.setAdapter(cursorAdapter);



    }


    }

AndroidSQLite.java文件

package edit.list;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class AndroidSQLite {

public static final String MYDATABASE_NAME = "abncd";
public static final String MYDATABASE_TABLE = "zxc";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_FNAME = "FNAME";



//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_FNAME + " text not null);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public AndroidSQLite(Context c){
context = c;
}

public AndroidSQLite openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}

public AndroidSQLite openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}

public void close(){
sqLiteHelper.close();
}

public long insert(String fname){

ContentValues contentValues = new ContentValues();

contentValues.put(KEY_FNAME, fname);

return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}

public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}

public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_FNAME};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
  null, null, null, null, null);

return cursor;
}

public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
  CursorFactory factory, int version) {
 super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
 // TODO Auto-generated method stub
 db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 // TODO Auto-generated method stub

}

}

}

display.java

public class display extends Activity {
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);


                // Get the message from the intent
                Intent intent = getIntent();
                String message = intent.getStringExtra(EditLIST.EXTRA_MESSAGE);

                // Create the text view
                EditText editText = new EditText(this);
                editText.setTextSize(40);
                editText.setText(message);

                // Set the text view as the activity layout
                setContentView(editText);
                }
     public boolean onCreateOptionsMenu (Menu menu)
        {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_edit_list,menu);
            return true;
        }

}

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确。据我所知,当您点击编辑时,无论您实际点击哪个项目,您都会从第一个项目获取数据?在这种情况下,我认为问题在于:

TextView textView=(TextView) findViewById(R.id.textView1);

您正在整个列表中搜索R.id.textView1,并且只返回第一个项目。您只需要在单击的项目中进行搜索:

TextView textView=(TextView) view.findViewById(R.id.textView1);

其中view是onClick函数的参数。

正如我所说,我不确定这是不是问题,但它看起来像是一个问题。

答案 1 :(得分:0)

我认为您的删除代码存在问题 它会像..

public int delete_byID(int id)         {
             sqLiteDatabase.delete(MYDATABASE_TABLE,KEY_ROWID +“=”+ 1,null);              返回-1;         }

将删除表中的id号1