SQLite数据库为每个String赋予一个int组

时间:2018-02-24 17:19:16

标签: java android sqlite android-sqlite

我是编码的初学者,所以我有一个问题。我有一个词汇应用程序,用于测试用户的词汇量。我的想法是将每个新单词放入一个组(进入1,2,3,4,5或6组)。因此,当只有美国人在几个词汇表上进行测试时,他可以使用其中一个组。因为我也是sqlite的初学者,我不知道怎么做这个。我的代码:

voc_liste:

package com.example.calebseeling.frenchlearning.lists;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.calebseeling.frenchlearning.Databases.DatabaseHelper_voc;
import com.example.calebseeling.frenchlearning.Databases.DatabaseHelper_voc_german;
import com.example.calebseeling.frenchlearning.Activitys.ListVocDataLayout;
import com.example.calebseeling.frenchlearning.Databases.Database_for_numbers;
import com.example.calebseeling.frenchlearning.R;

/**
 * Created by calebseeling on 27.01.18.
 */

public class voc_liste extends AppCompatActivity {


    private static final String TAG = "MainActivity";


    public DatabaseHelper_voc mDatabaseHelper;
    public DatabaseHelper_voc_german mDatabaseHelper_2;
    public Database_for_numbers mDatabaseHelper_3;
    private Button btnAdd, btnViewData;
    private EditText editText , editText_2;
    private static int number = 0;
    private static String newEntry;
    private static String newEntry_2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voc_liste);
        editText = (EditText) findViewById(R.id.editText_1);
        btnAdd = (Button) findViewById(R.id.btnAdd_1_1);
        btnViewData = (Button) findViewById(R.id.btnView_1);
        mDatabaseHelper = new DatabaseHelper_voc(this);
        mDatabaseHelper_2 = new DatabaseHelper_voc_german(this);
        editText_2 = (EditText) findViewById(R.id.editText_2);
        Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);


        String[] items = new String[]{"1", "2", "3","4", "5", "6"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
        spinner.setAdapter(adapter);
        String spinner_text = spinner.getSelectedItem().toString();


        if (spinner_text.equals(1)) {

number = 1;

        }
        if (spinner_text.equals(2)){
            number = 2;

        }
        if (spinner_text.equals(3)){
            number = 3;

        }if (spinner_text.equals(4)){
            number = 4;

        }if (spinner_text.equals(5)){
            number = 5;

        }if (spinner_text.equals(6)){
            number = 6;
        }









        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 String newEntry = editText.getText().toString();
                  String newEntry_2 = editText_2.getText().toString();

                if (editText.length() != 0 && editText_2.length() != 0) {
                    AddData(newEntry , number);
                    AddData_2(newEntry_2);
                    editText.setText("");
                    editText_2.setText("");
                } else {
                    toastMessage("Sie müssen eine Vokabel ins Feld schreiben");
                }

            }
        });

        btnViewData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(voc_liste.this, ListVocDataLayout.class);
                startActivity(intent);
            }
        });

    }


    public void AddData(String newEntry , int number) {
        boolean insertData = mDatabaseHelper.addData(newEntry , number);


        if (insertData) {
            toastMessage("Super. Neuer Schüler hinzugefügt");
        } else {
            toastMessage("Error 404");
        }
    }
    public void AddData_2(String newEntry_2) {
        boolean insertData_2 = mDatabaseHelper_2.addData(newEntry_2);

        if (insertData_2) {
            toastMessage("Super. Neuer Schüler hinzugefügt");
        } else {
            toastMessage("Error 404");
        }
    }





    /**
     * customizable toast
     * @param message
     */
    private void toastMessage(String message){
        Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
    }
    public static int getNumber(){
        return number;
    }
    public static String getString(){
        return newEntry;
    }
    public DatabaseHelper_voc getmDatabaseHelper(){
        return mDatabaseHelper;
    }
}

ListVocDataLayout:

package com.example.calebseeling.frenchlearning.Activitys;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by calebseeling on 27.01.18.
 */

import com.example.calebseeling.frenchlearning.Databases.DatabaseHelper_voc;
import com.example.calebseeling.frenchlearning.Databases.DatabaseHelper_voc_german;
import com.example.calebseeling.frenchlearning.Databases.Database_for_numbers;
import com.example.calebseeling.frenchlearning.R;
import com.example.calebseeling.frenchlearning.lists.voc_liste;

/**
 * Created by User on 2/28/2017.
 */

public class ListVocDataLayout extends AppCompatActivity {

    private static final String TAG = "ListDataActivity";
    private static ListAdapter adapter;

    public DatabaseHelper_voc mDatabaseHelper;
    public DatabaseHelper_voc_german mDatabaseHelper_2;
    public Database_for_numbers mDatabaseHelper_3;

    private static ListView mListView_1;
    public static ArrayList<String> listData;
    public static ArrayList<String> listData_2;
    private static ListView mListView_2;
    private static int long_1;





    @Override
    protected   void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout_voc);
        mListView_1 = (ListView) findViewById(R.id.listView_1);
        mListView_2 = (ListView) findViewById(R.id.listView_2);
        mDatabaseHelper = new DatabaseHelper_voc(this);
        mDatabaseHelper_2 = new DatabaseHelper_voc_german(this);
        listData = null;
        populateListView();
        populateListView_2();



    }
    public void populateListView() {
        Log.d(TAG, "populateListView: Displaying data in the ListView.");
        //get the data and append to a list
        Cursor data = mDatabaseHelper.getData();
        listData = new ArrayList<>();


        while(data.moveToNext()){
            //get the value from the database in column 1

            listData.add(data.getString(1));

        }



        //create the list adapter and set the adapter
        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
        mListView_1.setAdapter(adapter);
        //set an onItemClickListener to the ListView
        Toast.makeText(this, "ALso?" + listData , Toast.LENGTH_SHORT).show();

        mListView_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {




            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = adapterView.getItemAtPosition(i).toString();
                Log.d(TAG, "onItemClick: You Clicked on " + name);



                Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
                int itemID = -1;
                while(data.moveToNext()  ){
                    itemID = data.getInt(0);

                }

                if(itemID > -1 ){
                    Log.d(TAG, "onItemClick: The ID is: " + itemID );
                    Intent editScreenIntent = new   

                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                }
                else{
                    Log.d(TAG,"else problem");
                }




            }


        });

    }
    public void populateListView_2() {
        Log.d(TAG, "populateListView: Displaying data in the ListView.");
        //get the data and append to a list
        Cursor data = mDatabaseHelper_2.getData();
        listData_2 = new ArrayList<>();
        while(data.moveToNext()){
            //get the value from the database in column 1

            listData_2.add(data.getString(1));

        }
        Log.d(TAG, "???" + data);
        Log.d(TAG, "BITTEE MIT SMACKES" + listData_2);


        //create the list adapter and set the adapter
         adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData_2);
        mListView_2.setAdapter(adapter);
         long_1 = mListView_1.getAdapter().getCount();
        Log.d(TAG, "ALSO WIE LANG?"+long_1);

        //set an onItemClickListener to the ListView


        mListView_2.setOnItemClickListener(new AdapterView.OnItemClickListener() {




            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = adapterView.getItemAtPosition(i).toString();
                Log.d(TAG, "onItemClick: You Clicked on " + name);



                Cursor data = mDatabaseHelper_2.getItemID(name); //get the id associated with that name
                int itemID = -1;
                while(data.moveToNext()  ){
                    itemID = data.getInt(0);

                }

                if(itemID > -1 ){
                    Log.d(TAG, "onItemClick: The ID is: " + itemID );
                    Intent editScreenIntent = new Intent(ListVocDataLayout.this, Edit_Voc_Data_German_Activity.class);
                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                }
                else{
                    Log.d(TAG,"else problem");
                }




            }


        });

    }

        //set an onItemClickListener to the ListView



    /**
     * customizable toast
     * @param message
     */
    private void toastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }



}

DatabaseHelper_voc:

package com.example.calebseeling.frenchlearning.Databases;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
import android.util.Log;

/**
 * Created by User on 2/28/2017.
 */

public class DatabaseHelper_voc extends SQLiteOpenHelper {

    private static final String TAG = "DatabaseHelper";

    public static final String TABLE_NAME = "Voc_table";
    public static final String COL1 = "ID";
    private static final String COL1_1 = "ID_1";
    private static final String COL2 = "French";
    private static final String COL3 = "German";
    private static final String COL4 = "Product";
    public static SQLiteDatabase db;







    public DatabaseHelper_voc(Context context) {
        super(context, TABLE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +

                COL2 +" TEXT" + COL4 + " INTEGER";
        db.execSQL(createTable);
    }

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

    public boolean addData(String item , int item2) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues French = new ContentValues();
        French.put(COL2, item);
        French.put(COL4, item2);



        Log.d(TAG, "addData: Adding " + item + item2 + " to " + TABLE_NAME);

        long result = db.insert(TABLE_NAME, null, French);

        //if data as inserted incorrectly it will return -1
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * Returns all the data from database
     * @return
     */
    public Cursor getData(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_NAME;
        Cursor data = db.rawQuery(query, null);
        return data ;
    }

    /**
     * Returns only the ID that matches the name passed in
     * @param name
     * @return
     */
    public Cursor getItemID(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
                " WHERE " + COL2 + " Where" + COL4 + " = '" + name + "'";
        Cursor data = db.rawQuery(query, null);
        Log.d(TAG, "OKAYY" + data);
        return data;
    }

    /**
     * Updates the name field
     * @param newName
     * @param id
     * @param oldName
     */
    public void updateName(String newName, int id, String oldName){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
                " = '" + newName + "' WHERE " + COL1 +  " = '" + id + "'" +
                " AND " + COL2 + " AND " + COL4 + " = '" + oldName + "'";
        Log.d(TAG, "updateName: query: " + query);
        Log.d(TAG, "updateName: Setting name to " + newName);
        db.execSQL(query);
    }

    /**
     * Delete from database
     * @param id
     * @param name
     */
    public void deleteName(int id, String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "DELETE FROM " + TABLE_NAME + " WHERE "
                + COL1 + " = '" + id + "'" +
                " AND " + COL2 + " AND " + COL4 + " = '" + name + "'";
        Log.d(TAG, "deleteName: query: " + query);
        Log.d(TAG, "deleteName: Deleting " + name + " from database.");
        db.execSQL(query);
    }
    public static SQLiteDatabase getdb(){

        return db;
    }
    public static String getTableName() {
        return TABLE_NAME;
    }
    public static SQLiteDatabase getDb() {
        return db;
    }

}

0 个答案:

没有答案