Java 将SQLiteDatabase导出到CSV Android Studio
我已经使用这段代码好几天了。我在Java方面没有太多经验,但我可以拼凑我在网上找到的代码并从中学习。我一直在使用这个网站作为我当前活动的模板。我已经对提供的代码做了很多修改,但都是一个接一个的错误。现在使用我的代码,我得到了sqldb(第211行左右…无法解析符号。我只是觉得我没有正确地声明sqldb是我的全部问题。您可能会在网上的其他地方注意到代码的其他部分。只是尝试学习,然后在完成后将代码更改为我需要的代码。这里是一些文件。谢谢你的帮助!(如果你看到任何其他的变化,那将是伟大的,因为我一直在做的就是使用Android Studio的建议)。不确定您需要查看哪些文件,但这里有一些。我的活动是我的主要爱好。在MyActivity.java中选择“send”时,它应导出csv文件 MyActivity.javaJava 将SQLiteDatabase导出到CSV Android Studio,java,android,sqlite,csv,Java,Android,Sqlite,Csv,我已经使用这段代码好几天了。我在Java方面没有太多经验,但我可以拼凑我在网上找到的代码并从中学习。我一直在使用这个网站作为我当前活动的模板。我已经对提供的代码做了很多修改,但都是一个接一个的错误。现在使用我的代码,我得到了sqldb(第211行左右…无法解析符号。我只是觉得我没有正确地声明sqldb是我的全部问题。您可能会在网上的其他地方注意到代码的其他部分。只是尝试学习,然后在完成后将代码更改为我需要的代码。这里是一些文件。谢谢你的帮助!(如果你看到任何其他的变化,那将是伟大的,因为我一直在
package com.nate.nash.howtoloveme;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatCallback;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class MyActivity extends ListActivity implements AppCompatCallback {
private DBHelper dbHelper;
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
private AppCompatDelegate delegate;
TextView student_Id;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO remove comment code
// TODO change wording from student to feeling
// TODO change when list is removed or added, spinner changes
// TODO start new activity after MainActivity.java
//let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
delegate = AppCompatDelegate.create(this, this);
//we need to call the onCreate() of the AppCompatDelegate
delegate.onCreate(savedInstanceState);
//we use the delegate to inflate the layout
delegate.setContentView(R.layout.activity_my);
//TODO custom added
//Finally, let's add the Toolbar
Toolbar toolbar= (Toolbar) findViewById(R.id.my_awesome_toolbar);
delegate.setSupportActionBar(toolbar);
super.onCreate(savedInstanceState);
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if (studentList.size() != 0) {
repo = new StudentRepo(this);
studentList = repo.getStudentList();
if(studentList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra("student_Id", Integer.parseInt( studentId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MyActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);
}else{
Toast.makeText(this, "No student!", Toast.LENGTH_SHORT).show();
}
} else {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_null);
Button button = (Button) findViewById(R.id.angry_btn);
}
String versionName = "";
try {
versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
TextView versionname = (TextView) findViewById(R.id.versionName);
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
}
@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_my, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MyActivity.this, SettingsActivity.class);
startActivity(intent);
//finish();
return true;
}
if (id == R.id.action_add) {
Intent intent = new Intent(MyActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.test) {
Intent intent = new Intent(MyActivity.this, HelloGridView.class);
startActivity(intent);
return true;
}
if (id == R.id.send) {
//main code begins here
try {
SQLiteDatabase sqldb = dbHelper.getReadableDatabase();
Cursor c = null;
c = sqldb.rawQuery("select * from crud.db", null);
int rowcount = 0;
int colcount = 0;
File sdCardDir = Environment.getExternalStorageDirectory();
String filename = "MyBackUp.csv";
// the name of the file to export with
File saveFile = new File(sdCardDir, filename);
FileWriter fw = new FileWriter(saveFile);
BufferedWriter bw = new BufferedWriter(fw);
rowcount = c.getCount();
colcount = c.getColumnCount();
if (rowcount > 0) {
c.moveToFirst();
for (int i = 0; i < colcount; i++) {
if (i != colcount - 1) {
bw.write(c.getColumnName(i) + ",");
} else {
bw.write(c.getColumnName(i));
}
}
bw.newLine();
for (int i = 0; i < rowcount; i++) {
c.moveToPosition(i);
for (int j = 0; j < colcount; j++) {
if (j != colcount - 1)
bw.write(c.getString(j) + ",");
else
bw.write(c.getString(j));
}
bw.newLine();
}
bw.flush();
Toast.makeText(this, "Exported Successfully.", Toast.LENGTH_SHORT).show();
//infotext.setText("Exported Successfully.");
}
} catch (Exception ex) {
if (sqldb.isOpen()) {
try {
sqldb.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, ex.getMessage().toString(), Toast.LENGTH_SHORT).show();
//infotext.setText(ex.getMessage().toString());
}
} finally {
}
return true;
}
if (id == R.id.array_add) {
Intent intent = new Intent(MyActivity.this, ArraySave.class);
startActivity(intent);
return true;
}
if (id == R.id.action_quit) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
switch(item.getItemId())
{
case R.id.preferences:
{
Intent intent = new Intent();
intent.setClassName(this, "MyPreferenceActivity");
startActivity(intent);
return true;
}
}
return super.onOptionsItemSelected(item);
}
public void ButtonOnClick(View v) {
switch (v.getId() /*to get clicked view id**/) {
case R.id.angry_btn:
Intent intent = new Intent(MyActivity.this, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
}
package com.nate.nash.howtoloveme;
/**
* Created by Nash on 1/13/2016.
*/
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends ListActivity implements android.view.View.OnClickListener{
Button btnAdd,btnGetAll;
TextView student_Id;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent = new Intent(MainActivity.this, MyActivity.class);
startActivity(intent);
finish();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View view) {
if (view== findViewById(R.id.btnAdd)){
Intent intent = new Intent(this,StudentDetail.class);
intent.putExtra("student_Id",0);
startActivity(intent);
}else {
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if(studentList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra("student_Id", Integer.parseInt( studentId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);
}else{
Toast.makeText(this,"No student!",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if(studentList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra("student_Id", Integer.parseInt( studentId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);
}else{
Toast.makeText(this,"No student!",Toast.LENGTH_SHORT).show();
}
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnGetAll = (Button) findViewById(R.id.btnGetAll);
btnGetAll.setOnClickListener(this);
}
}
MainActivity.java
package com.nate.nash.howtoloveme;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatCallback;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class MyActivity extends ListActivity implements AppCompatCallback {
private DBHelper dbHelper;
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
private AppCompatDelegate delegate;
TextView student_Id;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO remove comment code
// TODO change wording from student to feeling
// TODO change when list is removed or added, spinner changes
// TODO start new activity after MainActivity.java
//let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
delegate = AppCompatDelegate.create(this, this);
//we need to call the onCreate() of the AppCompatDelegate
delegate.onCreate(savedInstanceState);
//we use the delegate to inflate the layout
delegate.setContentView(R.layout.activity_my);
//TODO custom added
//Finally, let's add the Toolbar
Toolbar toolbar= (Toolbar) findViewById(R.id.my_awesome_toolbar);
delegate.setSupportActionBar(toolbar);
super.onCreate(savedInstanceState);
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if (studentList.size() != 0) {
repo = new StudentRepo(this);
studentList = repo.getStudentList();
if(studentList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra("student_Id", Integer.parseInt( studentId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MyActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);
}else{
Toast.makeText(this, "No student!", Toast.LENGTH_SHORT).show();
}
} else {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_null);
Button button = (Button) findViewById(R.id.angry_btn);
}
String versionName = "";
try {
versionName = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
TextView versionname = (TextView) findViewById(R.id.versionName);
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
}
@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_my, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MyActivity.this, SettingsActivity.class);
startActivity(intent);
//finish();
return true;
}
if (id == R.id.action_add) {
Intent intent = new Intent(MyActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.test) {
Intent intent = new Intent(MyActivity.this, HelloGridView.class);
startActivity(intent);
return true;
}
if (id == R.id.send) {
//main code begins here
try {
SQLiteDatabase sqldb = dbHelper.getReadableDatabase();
Cursor c = null;
c = sqldb.rawQuery("select * from crud.db", null);
int rowcount = 0;
int colcount = 0;
File sdCardDir = Environment.getExternalStorageDirectory();
String filename = "MyBackUp.csv";
// the name of the file to export with
File saveFile = new File(sdCardDir, filename);
FileWriter fw = new FileWriter(saveFile);
BufferedWriter bw = new BufferedWriter(fw);
rowcount = c.getCount();
colcount = c.getColumnCount();
if (rowcount > 0) {
c.moveToFirst();
for (int i = 0; i < colcount; i++) {
if (i != colcount - 1) {
bw.write(c.getColumnName(i) + ",");
} else {
bw.write(c.getColumnName(i));
}
}
bw.newLine();
for (int i = 0; i < rowcount; i++) {
c.moveToPosition(i);
for (int j = 0; j < colcount; j++) {
if (j != colcount - 1)
bw.write(c.getString(j) + ",");
else
bw.write(c.getString(j));
}
bw.newLine();
}
bw.flush();
Toast.makeText(this, "Exported Successfully.", Toast.LENGTH_SHORT).show();
//infotext.setText("Exported Successfully.");
}
} catch (Exception ex) {
if (sqldb.isOpen()) {
try {
sqldb.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(this, ex.getMessage().toString(), Toast.LENGTH_SHORT).show();
//infotext.setText(ex.getMessage().toString());
}
} finally {
}
return true;
}
if (id == R.id.array_add) {
Intent intent = new Intent(MyActivity.this, ArraySave.class);
startActivity(intent);
return true;
}
if (id == R.id.action_quit) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing Activity")
.setMessage("Are you sure you want to close this activity?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
switch(item.getItemId())
{
case R.id.preferences:
{
Intent intent = new Intent();
intent.setClassName(this, "MyPreferenceActivity");
startActivity(intent);
return true;
}
}
return super.onOptionsItemSelected(item);
}
public void ButtonOnClick(View v) {
switch (v.getId() /*to get clicked view id**/) {
case R.id.angry_btn:
Intent intent = new Intent(MyActivity.this, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
}
package com.nate.nash.howtoloveme;
/**
* Created by Nash on 1/13/2016.
*/
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends ListActivity implements android.view.View.OnClickListener{
Button btnAdd,btnGetAll;
TextView student_Id;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent = new Intent(MainActivity.this, MyActivity.class);
startActivity(intent);
finish();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View view) {
if (view== findViewById(R.id.btnAdd)){
Intent intent = new Intent(this,StudentDetail.class);
intent.putExtra("student_Id",0);
startActivity(intent);
}else {
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if(studentList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra("student_Id", Integer.parseInt( studentId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);
}else{
Toast.makeText(this,"No student!",Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StudentRepo repo = new StudentRepo(this);
ArrayList<HashMap<String, String>> studentList = repo.getStudentList();
if(studentList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
student_Id = (TextView) view.findViewById(R.id.student_Id);
String studentId = student_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra("student_Id", Integer.parseInt( studentId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_name});
setListAdapter(adapter);
}else{
Toast.makeText(this,"No student!",Toast.LENGTH_SHORT).show();
}
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnGetAll = (Button) findViewById(R.id.btnGetAll);
btnGetAll.setOnClickListener(this);
}
}
package com.nate.nash.howtoloveme;
/**
*由Nash于2016年1月13日创建。
*/
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.KeyEvent;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类MainActivity扩展ListActivity实现android.view.view.OnClickListener{
按钮btnAdd,BTN高度;
TextView学生Id;
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
if((keyCode==KeyEvent.keyCode\u BACK)){
意向意向=新意向(MainActivity.this、MyActivity.class);
星触觉(意向);
完成();
}
返回super.onKeyDown(keyCode,event);
}
@凌驾
公共void onClick(视图){
if(视图==findViewById(R.id.btnAdd)){
意向意向=新意向(此,StudentDetail.class);
intent.putExtra(“学生Id”,0);
星触觉(意向);
}否则{
StudentRepo=新StudentRepo(本);
ArrayList studentList=repo.getStudentList();
如果(studentList.size()!=0){
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
student\u Id=(TextView)view.findViewById(R.Id.student\u Id);
String studentId=student_Id.getText().toString();
Intent objIndent=newintent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra(“student_Id”,Integer.parseInt(studentId));
星形触觉(明显);
}
});
ListAdapter=new SimpleAdapter(MainActivity.this,studentList,R.layout.view_student_条目,新字符串[]{“id”,“name”},新int[]{R.id.student_id,R.id.student_name});
setListAdapter(适配器);
}否则{
Toast.makeText(这个“没有学生!”,Toast.LENGTH_SHORT.show();
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StudentRepo=新StudentRepo(本);
ArrayList studentList=repo.getStudentList();
如果(studentList.size()!=0){
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
student\u Id=(TextView)view.findViewById(R.Id.student\u Id);
String studentId=student_Id.getText().toString();
Intent objIndent=newintent(getApplicationContext(),StudentDetail.class);
objIndent.putExtra(“student_Id”,Integer.parseInt(studentId));
星形触觉(明显);
}
});
ListAdapter=new SimpleAdapter(MainActivity.this,studentList,R.layout.view_student_条目,新字符串[]{“id”,“name”},新int[]{R.id.student_id,R.id.student_name});
setListAdapter(适配器);
}否则{
Toast.makeText(这个“没有学生!”,Toast.LENGTH_SHORT.show();
}
btnAdd=(按钮)findviewbyd(R.id.btnAdd);
btnAdd.setOnClickListener(此);
btngeall=(按钮)findViewById(R.id.btngeall);
btnGetAll.setOnClickListener(此);
}
}
尝试使用此功能
private void exportDB() {
File dbFile=getDatabasePath("MyDBName.db");
DBHelper dbhelper = new DBHelper(getApplicationContext());
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists())
{
exportDir.mkdirs();
}
File file = new File(exportDir, "csvname.csv");
try
{
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM contacts",null);
csvWrite.writeNext(curCSV.getColumnNames());
while(curCSV.moveToNext())
{
//Which column you want to exprort
String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
}
catch(Exception sqlEx)
{
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
}
}
我使用了你的建议,实际上我以前也尝试过,但在CSVwriter上遇到了问题。事实证明,这是一个需要添加的依赖项。这对我来说是新鲜事!但到目前为止没有错误……所以希望这能让我走上正确的方向!非常感谢。