Android Studio如何从SQLlite获取数据并将其放在EditText上

时间:2018-09-23 20:27:20

标签: android sqlite android-studio listview

我是编码的初学者,我真的希望你能帮助我。 所以我的问题是,我创建了一个SQLite数据库,并将信息从编辑文本保存到ListView中,但是现在我希望当生病单击列表中的特定项目时,它将打开EditList活动,其中包含用户键入的所有信息并保存在这里,请帮助我,这是我的代码(我已经检查了所有信息都已保存,我只想单击列表中的项目时就对其进行编辑):

这是我的数据库助手:

 package com.example.lian.atlist;

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

/**
 * Created by Lian on 18-Sep-18.
 */

public class DataBaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "List.db";
    public static final String TABLE_NAME = "Notes_table";
    public static final String ID = "ID";
    public static final String TITLE = "TITLE";
    public static final String DESCRIPTION = "DESCRIPTION";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    //    SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,TITLE TEXT,DESCRIPTION TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
    }

    public boolean insertData(String title,String description){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put(TITLE,title);
        contentValues.put(DESCRIPTION,description);
        long result =db.insert(TABLE_NAME,null,contentValues);
        if(result==-1){
            return false;
        }else
        return true;
    }
    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " +TABLE_NAME,null);
        return  res;
    }
    public boolean updateData(String id,String title,String description){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put(ID,id);
        contentValues.put(TITLE,title);
        contentValues.put(DESCRIPTION,description);
        db.update(TABLE_NAME,contentValues,"ID = ?",new String[]{id});
        return true;
    }
}

这是我的主要活动:

package com.example.lian.atlist;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Debug;
import android.os.Parcelable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.EventLogTags;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import static com.example.lian.atlist.EditList.Description;
import static com.example.lian.atlist.EditList.Title;
import static com.example.lian.atlist.EditList.id;
public class MainActivity extends AppCompatActivity {

    DataBaseHelper myDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        myDB= new DataBaseHelper(this);
        ListView listView =(ListView)  findViewById(R.id.LV);
        ArrayList<String> TheList = new ArrayList<>();
        final Cursor data =myDB.getAllData();
        ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,TheList);
        listView.setAdapter(listAdapter);

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

          Intent EditNote = new Intent(MainActivity.this, EditList.class);
         startActivity(EditNote);
                                           }
       });

        if (data.getCount()==0){
            Toast.makeText(MainActivity.this, "The list is empty", Toast.LENGTH_LONG).show();
        }else{
            while(data.moveToNext()){
                TheList.add(data.getString(1));


            }
        }

        Intent SaveNote = getIntent();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               Intent addNote = new Intent(MainActivity.this, EditList.class);
               startActivity(addNote);
            }
        });
    }




}

这是我的EditList活动:

package com.example.lian.atlist;

import android.content.Intent;
import android.database.Cursor;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.security.PublicKey;

public class EditList extends AppCompatActivity {
    static TextView id;
    static EditText Title;
    static EditText Description;
    Button SaveButton;
    DataBaseHelper myDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_list);
        myDB = new DataBaseHelper(this);
        id=(TextView)findViewById(R.id.id);
        Title=(EditText)findViewById(R.id.editText);
        Description=(EditText)findViewById(R.id.editText2);
        SaveButton=(Button) findViewById(R.id.SaveButton);
        AddData();
        viewAll();
        UpdateData();

    }
    Intent AddNote = getIntent();
    Intent EditNote = getIntent();



    public void UpdateData(){
        boolean isUpdated = myDB.updateData(id.getText().toString()
                ,Title.getText().toString()
                ,Description.getText().toString());
        if (isUpdated==true){
                Toast.makeText(EditList.this,"Data Updated",Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(EditList.this, "Data is not Updated", Toast.LENGTH_LONG).show();
            }

        }



    public void viewAll(){
        Cursor res = myDB.getAllData();
        if(res.getCount()==0){
            //show message
            showMessage("Error","Nothing found");
            return;
        }
    }

    public void showMessage(String title,String Message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
    }
        public void AddData(){
        SaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               boolean isInserted= myDB.insertData(Title.getText().toString()
                        ,Description.getText().toString());


                if (isInserted == true){
                    Toast.makeText(EditList.this,"Data Inserted",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(EditList.this, "Data is not Inserted", Toast.LENGTH_LONG).show();
                }
                Intent SaveNote = new Intent(EditList.this, MainActivity.class);
                startActivity(SaveNote);
            }
        });
        }



}

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。其中之一是使用附加元素传递您单击的元素的ID

Intent i = new Intent(this, EditActivity.class);
i.putExtra("id", id); //Find id using the position in the click listener
startActivity(i);

然后在EditActivity中执行

Intent i= getIntent();
int id = i.getIntExtra("id", 0);

获得ID后,只需进行数据库查询并显示详细信息即可。