使用行id来检索适当的行值并在edittext中显示

时间:2015-09-03 04:25:34

标签: android android-sqlite

使用行id从两列调用特定行值并显示在edittext中。

为此,我发布了以下我尝试过的代码。

ActivityList.java:

  public class ActivityList extends Activity {

        SQLiteDatabase db = null;
        EditText editId, editCheck, editCity;
        ActionBar actionBar;
        Button search;
        SharedPreferences mPrefs;

        Cursor c1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);  

            getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
            setContentView(R.layout.list_activity);

            editCheck = (EditText) findViewById(R.id.editCheck);
            editId = (EditText) findViewById(R.id.editId);
            editCity = (EditText) findViewById(R.id.editCity);

            search = (Button) findViewById(R.id.search);

            mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
            Boolean welcomeScreenShown = mPrefs.getBoolean("k", false);

            db=openOrCreateDatabase("Check", MODE_PRIVATE, null); 

            db.execSQL("create table if not exists CheckPoint (ID INTEGER PRIMARY KEY AUTOINCREMENT,STATE VARCHAR, PLACE VARCHAR)");

            if (!welcomeScreenShown) {

            db.execSQL("INSERT INTO CheckPoint(STATE ,PLACE) values('Eden Garden','Kolkatta');");

            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putBoolean("k", true);
            editor.commit();
            }

            c1 = db.rawQuery("select * from CheckPoint ", null);

            search.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (editId.getText().toString().equals("")) {

                        show("Enter Id Number");

                    } else if (editId.getText().toString() != null) {

                        if (c1.getCount() > 0) {

                            c1.moveToFirst();
                            do {

                                String cp = c1.getString(1);
                                String cy = c1.getString(2);

                                editCheck.setText(cp);
                                editCity.setText(cy);

                            } while (c1.moveToNext());

                        }
                    }
                }
            });

        }

        public void show(String str) {
            Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
        }

    }

list_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editId"
            android:layout_width="108dp"
            android:layout_height="wrap_content"
            android:ems="10" />

        <Button
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="Button" />
    </LinearLayout>

    <EditText
        android:id="@+id/editCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editCity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:ems="10" />

</LinearLayout>

数据库:

enter image description here

在sqlite浏览器的帮助下,我添加了第二行和第三行数据并存储在我的应用程序中。

修改

我的问题是,如果我搜索id 2,它会显示第1行的id值。我需要显示第2行的id值。顺便说一句,如果我搜索第3行的id,则必须显示该行3编辑文本中的状态和位置。

任何人都可以帮助我。谢谢。

3 个答案:

答案 0 :(得分:1)

我认为你应该使用sqlquery来搜索带有id的数据库。

例如:"select * from CheckPoint WHERE id=2"

    else if (editId.getText().toString() != null) {
        c1 = db.rawQuery("select * from CheckPoint WHERE id=" + editId.getText().toString(), null);
        if (c1.getCount() > 0) {
            if (c1.moveToFirst()) {
                String cp = c1.getString(1);
                String cy = c1.getString(2);

                editCheck.setText(cp);
                editCity.setText(cy);
            }
        }
    }

答案 1 :(得分:1)

从编辑文本中获取id值之后,请执行以下代码片段,以解除原始查询:

String query = "select * from CheckPoint WHERE id =" + yourIdNumber;
Cursor  cursor = database.rawQuery(query,null);

现在你可以执行do-while并获取值。

答案 2 :(得分:0)

<强> MainActivity.java:

btnSearch.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

             if (editId.getText().toString().equals("")) {

                 show("Enter Id Number");

             } else if (editId.getText().toString() != null) {


                 Cursor c1 = db.getData(Integer.parseInt(editId.getText().toString()));

                 if (c1.getCount() > 0) {

                     c1.moveToFirst();
                     do {

                         String cp = c1.getString(1);
                         String cy = c1.getString(2);

                         editName.setText(cp);
                         editNumber.setText(cy);

                     } while (c1.moveToNext());

                 }
             }
         }
     });

<强> DatabaseHandler.java:

public Cursor getData(int id){
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );

    if(res != null && res.moveToFirst()) {
        res.getString(res.getColumnIndex(KEY_NAME));
        res.getString(res.getColumnIndex(KEY_PH_NO));

        return res;
    }


    return res;

}