数据不会插入并显示在我的应用程序上Android Studio

时间:2017-04-27 14:46:54

标签: java android sqlite

我的应用程序没有显示我插入界面的数据,我创建了一个战斗机数据库,我的教练可以将健身房中的战士添加到其中。我使用卡片视图和回收适配器来显示所有数据,但是当我点击提交时它不会显示。

错误日志:ERROR LOG

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private ArrayList<Item>list = new ArrayList<Item>();
    private ItemAdapter adapter;
    private RecyclerView recyclerView;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);


        fetchRecords();

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LayoutInflater inflater = getLayoutInflater();
                View mView1 = inflater.inflate(R.layout.activity_add,null);

                final EditText input_name = (EditText) mView1.findViewById(R.id.Name);
                final EditText input_age = (EditText) mView1.findViewById(R.id.Age);
                final EditText input_weight = (EditText) mView1.findViewById(R.id.Weight);
                final EditText input_height = (EditText) mView1.findViewById(R.id.Height);
                final EditText input_reach = (EditText) mView1.findViewById(R.id.Reach);
                final Button btnSave = (Button) mView1.findViewById(R.id.btnSave);

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setView(mView1).setTitle("Add new Record")
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                dialog.dismiss();
                            }
                        });

                btnSave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String name = input_name.getText().toString();
                        String age = input_age.getText().toString();
                        String weight = input_weight.getText().toString();
                        String height = input_height.getText().toString();
                        String reach = input_reach.getText().toString();

                        if (name.equals("") && age.equals("") && weight.equals("") && height.equals("")&& reach.equals("")){
                            Snackbar.make(view,"Field incomplete",Snackbar.LENGTH_SHORT).show();

                        }else {
                            Save(name,age,name,height,reach);
                            dialog.dismiss();
                            Snackbar.make(view,"Saving",Snackbar.LENGTH_SHORT).show();
                        }
                    }
                });

                dialog = builder.create();
                dialog.show();


            }
        });
    }

    public void fetchRecords() {

        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        adapter = new ItemAdapter(this,list);

        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        list.clear();

        Functions functions = new Functions(MainActivity.this);

        ArrayList<Item>data = functions.getAllRecords();

        if (data.size()>0){
            for (int i = 0; i<data.size(); i++){

                int id = data.get(i).getId();
                String namel = data.get(i).getName();
                String agel = data.get(i).getAge();
                String weightl = data.get(i).getWeight();
                String heightl = data.get(i).getHeight();
                String reachl = data.get(i).getReach();

                Item item = new Item();

                item.setId(id);
                item.setName(namel);
                item.setAge(agel);
                item.setWeight(weightl);
                item.setHeight(heightl);
                item.setReach(reachl);
                list.add(item);

            }adapter.notifyDataSetChanged();

        }else {
            Toast.makeText(MainActivity.this, "No Records found.", Toast.LENGTH_SHORT).show();
        }
    }

    private void Save(String name, String age, String weight, String height, String reach) {


        Functions functions = new Functions(MainActivity.this);
        Item item = new Item();

        item.setName(name);
        item.setAge(age);
        item.setWeight(weight);
        item.setHeight(height);
        item.setReach(reach);
        functions.Insert(item);
        Toast.makeText(MainActivity.this, "Saved...", Toast.LENGTH_SHORT).show();
        fetchRecords();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}
public class Functions extends SQLiteOpenHelper {

    private static final String DB_NAME = "crud.db";
    private static final int DB_VERSION = 1;

    private Fighter fighter = new Fighter();

    public Functions(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(fighter.CREATE_TABLE_PERSON);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + fighter.TABLE_FIGHTER);
    }


    public void Insert(Item item){

        SQLiteDatabase database = getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(fighter.ID,item.getId());
        contentValues.put(fighter.NAME,item.getName());
        contentValues.put(fighter.AGE,item.getAge());
        contentValues.put(fighter.WEIGHT,item.getWeight());
        contentValues.put(fighter.HEIGHT,item.getHeight());
        contentValues.put(fighter.REACH,item.getReach());

        database.insert(fighter.TABLE_FIGHTER,null,contentValues);
    }


    public ArrayList<Item>getAllRecords(){

        ArrayList<Item>list = new ArrayList<Item>();
        SQLiteDatabase database = getReadableDatabase();

        String sql = "SELECT * FROM " + fighter.TABLE_FIGHTER;

        Cursor cursor = database.rawQuery(sql,null);

        if (cursor.moveToFirst()){
            do {
                Item item = new Item();
                item.setId(Integer.parseInt(cursor.getString(0)));
                item.setName(cursor.getString(1));
                item.setAge(cursor.getString(2));
                item.setWeight(cursor.getString(3));
                item.setHeight(cursor.getString(4));
                item.setReach(cursor.getString(5));


                list.add(item);

            }while (cursor.moveToNext());
        }

        cursor.close();
        database.close();
        return list;
    }


    public Item getSingleItem(int id){

        SQLiteDatabase database = getReadableDatabase();

        String sql = "SELECT * FROM " + fighter.TABLE_FIGHTER + " WHERE " + fighter.ID + "=?";
        Cursor cursor = database.rawQuery(sql,new String[]{String.valueOf(id)});

        if (cursor != null)
            cursor.moveToNext();

        Item item = new Item();
        item.setId(Integer.parseInt(cursor.getString(0)));
        item.setName(cursor.getString(1));
        item.setAge(cursor.getString(2));
        item.setWeight(cursor.getString(3));
        item.setHeight(cursor.getString(4));
        item.setReach(cursor.getString(4));

        cursor.close();
        database.close();
        return item;
    }

    public void DeleteItem(int id){

        SQLiteDatabase database = getWritableDatabase();
        database.delete(fighter.TABLE_FIGHTER,fighter.ID + "=?",new String[]{String.valueOf(id)});
        database.close();
    }

    public void Update(Item item){
        SQLiteDatabase database = getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(fighter.NAME,item.getName());
        contentValues.put(fighter.AGE,item.getAge());
        contentValues.put(fighter.WEIGHT,item.getWeight());
        contentValues.put(fighter.HEIGHT,item.getHeight());
        contentValues.put(fighter.REACH,item.getReach());
        database.update(fighter.TABLE_FIGHTER,contentValues,fighter.ID + "=?",new String[]{String.valueOf(item.getId())});

    }


}
public class Fighter {


    public static final String TABLE_FIGHTER = "fighter";



    public static final String ID = "ID";
    public static final String NAME = "Name";
    public static final String AGE = "Age";
    public static final String WEIGHT = "Weight";
    public static final String HEIGHT = "Height";
    public static final String REACH = "Reach";


    public static final String CREATE_TABLE_PERSON = "CREATE TABLE " + TABLE_FIGHTER + "("
            + ID + " INTEGER PRIMARY KEY,"
            + NAME + " TEXT,"
            + AGE + " TEXT,"
            + WEIGHT + " TEXT,"
            + HEIGHT + " TEXT"
            + REACH + " TEXT"+ ")";

}

1 个答案:

答案 0 :(得分:2)

根据您的代码,您在+ HEIGHT + " TEXT"之后错过了逗号。因此,由于SQL语法错误,表未正确创建。请尝试以下。我已经为你添加了逗号。

public static final String CREATE_TABLE_PERSON = "CREATE TABLE " + TABLE_FIGHTER + "("
        + ID + " INTEGER PRIMARY KEY,"
        + NAME + " TEXT,"
        + AGE + " TEXT,"
        + WEIGHT + " TEXT,"
        + HEIGHT + " TEXT,"
        + REACH + " TEXT"+ ")";