//这是我在dbcontroller中的代码
package com.example.ansiuser.swipe;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static android.R.id.list;
public class DBController extends SQLiteOpenHelper {
private static final String LOGCAT = null;
public final String TABLE_NAME = "Countsheet";
public DBController(Context applicationcontext) {
super(applicationcontext, "Count.db", null, 1); // creating DATABASE
Log.d(LOGCAT, "Created");
}
@Override
public void onCreate(SQLiteDatabase database) {
String query;
query = "CREATE TABLE IF NOT EXISTS Countsheet ( Id INTEGER PRIMARY KEY AUTOINCREMENT , Category TEXT,Code TEXT,Description TEXT, Unit TEXT, Quantity TEXT)";
database.execSQL(query);
}
public final class FeedReaderContract {
// To prevent someone from accidentally instantiating the contract class,
// make the constructor private.
private FeedReaderContract() {
}
/* Inner class that defines the table contents */
public class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "Countsheet";
public static final String Col_1 = "Id";
public static final String Col_2 = "Category";
public static final String Col_3 = "Code";
public static final String Col_4 = "Description";
public static final String Col_5 = "Unit";
public static final String Col_6 = "Quantity";
}
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old,
int current_version) {
String query;
query = "DROP TABLE IF EXISTS Countsheet";
database.execSQL(query);
onCreate(database);
}
}
//这是主要活动
public class MainActivity extends FragmentActivity{
Button btnimport;
PagerFragment pagerFragment;
public static final int requestcode = 1;
DBController controller = new DBController(this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnimport = (Button) findViewById(R.id.btnupload);
final ArrayList<PageData> data = new ArrayList<PageData>();
data.add(new PageData(1, "Picture13432423423423432423432","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(2, "Picture2","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(3, "Picture3","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(4, "Picture4","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(5, "Picture5","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(6, "Picture6","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(7, "Picture7","Category","Code","Description","Unit","Quantity"));
data.add(new PageData(8, "Picture8","Category","Code","Description","Unit","Quantity"));
ListView lv = (ListView) findViewById(R.id.list_view);
ListViewAdapter lva = new ListViewAdapter(this, R.layout.item_list, data);
lv.setAdapter(lva);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//peace of code that create launch new fragment with swipe view inside
PagerFragment pagerFragment = new PagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("CURRENT_POSITION", position);
bundle.putParcelableArrayList("DATA_LIST", data);
pagerFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, pagerFragment, "swipe_view_fragment").commit();
}
});
btnimport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(MainActivity.this);
alertdialog.setTitle("Confirm Import...");
alertdialog.setMessage("Are you sure you want to Import Data?");
alertdialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// clearApplicationData();
Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
fileintent.setType("gagt/sdf");
try {
startActivityForResult(fileintent, requestcode);
} catch (ActivityNotFoundException e) {
// lbl.setText("No activity can handle picking a file. Showing alternatives.");
}
}
});
alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"Import Canceled" ,Toast.LENGTH_SHORT ).show();
}
});
alertdialog.show();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null)
return;
switch (requestCode) {
case requestcode:
String filepath = data.getData().getPath();
controller = new DBController(getApplicationContext());
SQLiteDatabase db = controller.getWritableDatabase();
String tableName = "Countsheet";
db.execSQL("delete from " + tableName);
try {
if (resultCode == RESULT_OK) {
try {
FileReader file = new FileReader(filepath);
BufferedReader buffer = new BufferedReader(file);
ContentValues contentValues = new ContentValues();
String line = "";
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
String[] str = line.split(",", 5); // defining 5 columns with null or blank field //values acceptance
//Id, category,code,Description,Unit,Quantity
String company = str[0].toString();
String Name = str[1].toString();
String Price = str[2].toString();
String units = str[3].toString();
String quants = str[4].toString();
contentValues.put("Category", company);
contentValues.put("Code", Name);
contentValues.put("Description", Price);
contentValues.put("Unit", units);
contentValues.put("Quantity", quants);
db.insert(tableName, null, contentValues);
// lbl.setText("Successfully Updated Database.");
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (IOException e) {
if (db.inTransaction())
db.endTransaction();
Dialog d = new Dialog(this);
d.setTitle(e.getMessage().toString() + "first");
d.show();
// db.endTransaction();
}
} else {
if (db.inTransaction())
db.endTransaction();
Dialog d = new Dialog(this);
d.setTitle("Only CSV files allowed");
d.show();
}
} catch (Exception ex) {
if (db.inTransaction())
db.endTransaction();
Dialog d = new Dialog(this);
d.setTitle(ex.getMessage().toString() + "second");
d.show();
// db.endTransaction();
}
}
Toast.makeText(MainActivity.this,"data Imported",Toast.LENGTH_SHORT).show();
}
}
//这里是parcel class
package com.example.ansiuser.swipe;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by ansiuser on 11/01/2017.
*/
public class PageData implements Parcelable{
private int picture;
private String title ;
private String Category ;
private String Code ;
private String Description ;
private String Unit ;
private String Quantity ;
public PageData(){
}
public PageData(Parcel in){
picture = in.readInt();
title = in.readString();
Category = in.readString();
Code = in.readString();
Description = in.readString();
Unit = in.readString();
Quantity = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(picture);
dest.writeString(title);
dest.writeString(Category);
dest.writeString(Code);
dest.writeString(Description);
dest.writeString(Unit);
dest.writeString(Quantity);
}
public PageData(int picture, String title , String cat,String code,String desk, String unit, String quan){
this.picture = picture;
this.title = title;
this.Category = cat;
this.Code = code;
this.Description = desk;
this.Unit = unit;
this.Quantity = quan;
}
public int getPicture() {
return picture;
}
public String getTitle() {
return title;
}
public String getCategory() {
return Category;
}
public String getCode() {
return Code;
}
public String getDesk() {
return Description;
}
public String getUnit() {
return Unit;
}
public String getQuantity() {
return Quantity;
}
public static final Creator<PageData> CREATOR = new Creator<PageData>() {
@Override
public PageData createFromParcel(Parcel in) {
return new PageData(in);
}
@Override
public PageData[] newArray(int size) {
return new PageData[size];
}
};
}