如何使用android studio

时间:2016-06-02 15:27:56

标签: sqlite android-fragments

在尝试插入android中的sqlite数据库时出现问题。 我的应用程序有标签(标签一,标签二,标签三),它们链接到片段。我在数据库中使用add按钮将数量和原因的两个编辑文本放入片段。但是当我尝试填写表单并单击数据库时,会出现此错误。我真的需要你的帮助。

MainActivity Code

package package com.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentManager;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity{

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
TextView data;
private EditText modesteinputAmount;
private EditText modesteinputReason;
MyDBHandler dbHandler;



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

     data = (TextView) findViewById(R.id.textView2);
     modesteinputAmount = (EditText) findViewById(R.id.inputAmount);
     modesteinputReason = (EditText)findViewById(R.id.inputReason);
     dbHandler = new MyDBHandler(this, null, null, 1);
    printDatabase();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);

    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

}

public void addbuttonClicked(View view) {
    modesteinputAmount = (EditText) findViewById(R.id.inputAmount);
    modesteinputReason = (EditText)findViewById(R.id.inputReason);
    dbHandler = new MyDBHandler(this, null, null, 1);

    Incomes income = new Incomes(modesteinputAmount.getText().toString());
    Incomes incomes = new Incomes(modesteinputReason.getText().toString());
    dbHandler.addIncomes(income);
    dbHandler.addIncomes(incomes);
    printDatabase();

}
public void printDatabase(){
    String dbString  = dbHandler.databseToString();
    data.setText(dbString);

}

 public void deletebutton(View view) {
     String inputtext = modesteinputReason.getText().toString();
    dbHandler.deleteIncomes(inputtext);
}



private void setupViewPager(ViewPager viewPager) {
    ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
    // ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragmant(), "THREE");
    adapter.addFragment(new FourFragment(), "FOUR");
    viewPager.setAdapter(adapter);
}


public class ViewPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPageAdapter(FragmentManager manager) {
        super(manager);
    }


    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

   @Override
    public CharSequence getPageTitle(int position) {
       return mFragmentTitleList.get(position);
    }

}


}

MyDBHandler类

package com.example;

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

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "personalManagementDB.db";
public static final String TABLE_INCOMES = "incomes";
public static final String COLUMN_ID ="_id";
public static final String COLUMN_INCOME_AMOUNT ="incomeAmount";
public static final String COLUMN_INCOME_REASON ="incomeReason";

public MyDBHandler(Context context, String name,      SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_INCOMES + " ( " +
            COLUMN_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_INCOME_AMOUNT + " INT " +
            COLUMN_INCOME_REASON + " TEXT " +
            ");";
    db.execSQL(query);

}

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

public void addIncomes(Incomes income){
    ContentValues values = new ContentValues();
    values.put(COLUMN_INCOME_AMOUNT,income.get_incomeAmout());
    values.put(COLUMN_INCOME_REASON,income.get_incomeDescription());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_INCOMES,null,values);
    db.close();
}

public void deleteIncomes(String incomeReason){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_INCOMES + " WHERE " +     COLUMN_INCOME_AMOUNT +"=\"" + incomeReason + "\";");


}
public String databseToString(){
    String dbString ="";
    SQLiteDatabase db= getWritableDatabase();
    String query ="SELECT * FROM " + TABLE_INCOMES + "WHERE 1";

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("incomeAmount"))!= null);
            dbString += c.getString(c.getColumnIndex("incomeAmount"));
            dbString +="\n";
        c.moveToNext();
    }
    db.close();
    return dbString;
}
}


**fragment one**
package com.example
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class OneFragment extends Fragment {


public OneFragment() {
    // Required empty public constructor
}


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


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)  {

    return inflater.inflate(R.layout.fragment_one, container, false);
}


  }

Income Class

package com.example;


public class Incomes{

private int _id;
private String _incomeAmout;
private String _incomeDescription;

public Incomes(){}
public Incomes(String incomeAmout) {
    this._incomeAmout = incomeAmout;
}

public void set_id(int _id) {
    this._id = _id;
}

public void set_incomeAmout(String _incomeAmout) {
    this._incomeAmout = _incomeAmout;
}

public void set_incomeDescription(String _incomeDescription) {
    this._incomeDescription = _incomeDescription;
}

public int get_id() {
    return _id;
}

public String get_incomeAmout() {
    return _incomeAmout;
}

public String get_incomeDescription() {
    return _incomeDescription;
}

}

片段化一个xml文件

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Enter Amount"
    android:id="@+id/textView"
    android:layout_marginLeft="48dp"
    android:layout_marginStart="48dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="83dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttonadd"
    android:id="@+id/addbutton"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/inputReason"
    android:layout_alignStart="@+id/inputReason"
    android:onClick="addbuttonClicked" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/inputReason"
    android:width="250dp"
    android:layout_below="@+id/textView3"
    android:layout_alignLeft="@+id/inputAmount"
    android:layout_alignStart="@+id/inputAmount"
    android:inputType="text" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Enter Reason"
    android:id="@+id/textView3"
    android:layout_below="@+id/inputAmount"
    android:layout_alignLeft="@+id/inputAmount"
    android:layout_alignStart="@+id/inputAmount" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/inputAmount"
    android:width="250dp"
    android:layout_below="@+id/textView"
    android:layout_alignLeft="@+id/textView"
    android:layout_alignStart="@+id/textView"
    android:inputType="text" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttondelete"
    android:id="@+id/buttondelete"
    android:layout_alignTop="@+id/addbutton"
    android:layout_alignRight="@+id/inputReason"
    android:layout_alignEnd="@+id/inputReason"
    android:onClick="deletebuttonClicked" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView2"
    android:layout_below="@+id/buttondelete"
    android:layout_toRightOf="@+id/addbutton"
    android:layout_toEndOf="@+id/addbutton"
    android:layout_marginTop="59dp" />

清单文件(文字视图)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>

</manifest>

fragment one (design view)

我想点击按钮添加(属于片段一)并能够插入到sql lite数据库中。非常感谢你

0 个答案:

没有答案