我在android stodio中创建了名为stats.csv的CSV文件,因此我创建了3个类:
第一
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.ListView;
import java.io.InputStream;
import java.util.List;
public class MyDatabase extends Activity {
private ListView listView;
private ItemArrayAdapter itemArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_database);
listView = (ListView) findViewById(R.id.list_view);
itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.single_list_item);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(itemArrayAdapter);
listView.onRestoreInstanceState(state);
InputStream inputStream = getResources().openRawResource(R.raw.stats);
CSVReader csv = new CSVReader(inputStream);
List<String[]> scoreList = csv.read();
for(String [] scoreData : scoreList)
{
itemArrayAdapter.add(scoreData);
}
}
}
第二
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ItemArrayAdapter extends ArrayAdapter<String[]>{
private List<String[]> scoreList = new ArrayList<String[]>();
static class ItemViewHolder {
TextView namee;
TextView score;
}
public ItemArrayAdapter(Context context, int resource) {
super(context, resource);
}
public void add(String[] object) {
scoreList.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.scoreList.size();
}
@Override
public String[] getItem(int position) {
return this.scoreList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemViewHolder viewHolder;
if(row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_list_item, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.namee = (TextView) row.findViewById(R.id.namee);
viewHolder.score = (TextView) row.findViewById(R.id.score);
row.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder) row.getTag();
}
String[] stat = getItem(position);
viewHolder.namee.setText(stat[0]);
viewHolder.score.setText(stat[1]);
return row;
}
}
第三
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
InputStream inputStream;
public CSVReader (InputStream is)
{
this.inputStream = is;
}
public List<String[]> read () {
List<String[]> resultList = new ArrayList<String[]>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(",");
resultList.add(row);
}
} catch (IOException ex) {
throw new RuntimeException("Error iv reading CSV file:" + ex);
}
finally {
try {
inputStream.close();
} catch (IOException e) {
throw new RuntimeException("Error while closing input strem:" + e);
}
}
return resultList;
}
}
^这些代码显示了我的csv文件的列表视图。 但是当我尝试插入SQLite时它不起作用。 这里我正在尝试第三课:
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
InputStream inputStream;
public CSVReader (InputStream is)
{
this.inputStream = is;
}
public List<String[]> read () {
List<String[]> resultList = new ArrayList<String[]>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
SQLiteDatabase db =this.getWritableDatabase(); /* THE ERROR SHOWN HERE : " " */
String csvLine = "";
db.beginTransaction();
try {
while((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(",");
if (row.length != 2) {
Log.d("CSVParser", "Skipping Bad CSV Row");
continue;
}
ContentValues cv = new ContentValues(2);
cv.put("namee", row[0].trim());
cv.put("score", row[1].trim());
db.insert("TABLE", null, cv);
resultList.add(row);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
return resultList;
}
}
错误消息:" Error:(26, 32) error: cannot find symbol method getWritableDatabase() , "
答案 0 :(得分:0)
public class YourDatabaseName extends SQLiteOpenHelper {
private static final String LOGCAT = null;
public YourDatabaseName(Context applicationcontext) {
super(applicationcontext, "androidsqlite.db", null, 12);
Log.d(LOGCAT, "Created the Database");
}
@Override
public void onCreate(SQLiteDatabase database) {
String query1;
query1 = "CREATE TABLE SomeTableName"
+ "(_id INTEGER PRIMARY KEY, " + // 1
"names TEXT, " + //2
"scores TEXT)"; //3 This should probably be a float
database.execSQL(query1);
Log.d(LOGCAT, "SomeTableName Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
String query1;
query1 = "DROP TABLE IF EXISTS SomeTableName";
database.execSQL(query1);
onCreate(database);
}
public void insertdata(HashMap<String, String> inserting) {
Log.d(LOGCAT, "SomeTableName inserted");
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("names", inserting.get("names"));
values.put("scores", inserting.get("score"));
database.insert("SomeTableName", null, values);
database.close();
} }
然后在您的活动中
YourDateBaseName controller = new YourDateBaseName(this);//or (YourActivity.this)
Context context = this;
HashMap<String, String> datavalues = new HashMap<String, String>();
datavalues.put("names", names);
datavalues.put("scores", scores);
controller.insertdata(datavalues);