簡體   English   中英

將值從光標存儲到Android sqlite中的arraylist

[英]storing values from cursor to arraylist in android sqlite

如何將游標對象中的字符串值放入數組列表對象中,然后如何將該數組列表對象返回給調用方法?

public class MySqlite extends SQLiteOpenHelper {

private static int DATABASE_VERSION = 1;
private static String DATABASE_NAME = "student_info";
ArrayList<string> arraylist;

public MySqlite(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase sqlite) {
    // TODO Auto-generated method stub
    String query = "CREATE TABLE student_datatable(first_name Text, last_name Text, gender Text, street Text, city Text, contact Text)";
    sqlite.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase sqlite, int arg1, int arg2) {
    // TODO Auto-generated method stub
    sqlite.execSQL("DROP TABLE IF EXISTS student_datatable");
    onCreate(sqlite);
}

public void addrecord(String firstname, String lastname,String radiovalue,String street,String city,String contact) {
    SQLiteDatabase sqlite = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("first_name", firstname);
    cv.put("last_name", lastname);
    cv.put("gender", radiovalue);
    cv.put("street", street);
    cv.put("city", city);
    cv.put("contact", contact);

    sqlite.insert("student_datatable", null, cv);
    sqlite.close();
}

public ArrayList<String> searchrecord(String firstname)
{
    arraylist=new ArrayList<string>();
    SQLiteDatabase sql=this.getReadableDatabase();
    String lastnamefromtable = null;

    String param[]=new String[1];
    param[0]=firstname;
    Cursor c=sql.rawQuery("Select * from student_datatable where first_name=?", param);
    if(c.moveToFirst())
    {
        do
        {

            arraylist.add(c.getString(c.getColumnIndex("last_name")));


        }while(c.moveToNext());
    }
    return arraylist;
    }

public void deleterecord(String firstname)
{
    String Table_name="student_info";
    String column_name="first_name";
    SQLiteDatabase sql=this.getWritableDatabase();
    String lastnamefromtable=null;
    String param[]=new String[1];
    param[0]=firstname;
    sql.execSQL("DELETE FROM student_info where first_name=?",param);
}

}

我的活動如下:

public class Searchrecord extends Activity {

Intent i;
EditText firstname;
TextView getfirstname,getlastname,getgender,getstreet,getcity,getcontact;
MySqlite mysql;
ArrayList<String> arraylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchrecord);
    firstname=(EditText) findViewById(R.id.editText1);
    getfirstname=(TextView) findViewById(R.id.textView2);
    getlastname=(TextView) findViewById(R.id.textView3);
    getgender=(TextView) findViewById(R.id.textView4);
    getstreet=(TextView) findViewById(R.id.textView5);
    getcity=(TextView) findViewById(R.id.textView6);
    getcontact=(TextView) findViewById(R.id.textView7);
    mysql=new MySqlite(getApplicationContext());
    arraylist=new ArrayList<String>();
}

public void search(View v)
{
    if(firstname.getText().toString().equals(""))
    {
        Toast.makeText(getApplicationContext(), "Please Provide value", Toast.LENGTH_SHORT).show();
    }
    else
    {
        arraylist=mysql.searchrecord(firstname.getText().toString());


    }

}
public void back(View v)
{
    i = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(i);
}
}

在您的searchrecord方法中嘗試以下代碼,

public List<String> searchrecord(String firstname)
{
    List<String> arraylist = new ArrayList<String>();
    SQLiteDatabase sql=this.getReadableDatabase();

    String query = "SELECT last_name FROM student_datatable WHERE first_name = '"+firstname+"';

    Cursor c = sql.rawQuery(query, null);
    while(c.moveToNext()) {
        arraylist.add(c.getString(0);
    }
    c.close();
    return arraylist;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM