和美好的一天。我的项目有两个活动,第一个是将数据插入到数据库中运行良好,但后来我想在另一个活动中显示数据库中数据的列表视图。
当我尝试使用数据库时,我已尝试过本地数据,例如fred,george数组,但列表视图效果很好。活动崩溃了。看来我无法使用“database.open”
public class MyListActivity extends Activity{
DatabaseAdapter database;
//Creates item_details based on the ListItemDetails class
ListItemDetails item_details;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
//Uses the arraylist and set the result
ArrayList<ListItemDetails> result = GetSearchResults();
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new CustomListAdapter(getApplicationContext(),result));
}
private ArrayList<ListItemDetails> GetSearchResults() {
ArrayList<ListItemDetails> results = new ArrayList<ListItemDetails>();
item_details = new ListItemDetails();
/*
database.open();
Cursor c = database.getAllContacts();
if (c.moveToFirst())
{
do {
item_details.setFirstName(c.getString(1));
results.add(item_details);
} while (c.moveToNext());
}
*/
return results;
}
}
这是主要活动。当我使用buttonShow时,数据库打开正常,所以我尝试了buttonTest并将它们存储到列表中,但它对我来说有点混乱。
public class FamousPersonActivity extends Activity {
EditText editFirstName, editLastName;
Button buttonAdd, buttonShow, buttonTest;
ListView lv;
ListItemDetails item_details;
DatabaseAdapter database;
int request_Code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_famous_person);
database = new DatabaseAdapter(this);
lv = (ListView)findViewById(R.id.listView1);
//ArrayList<ListItemDetails> result = GetSearchResults();
//lv.setAdapter(new CustomListAdapter(getApplicationContext(),result));
editFirstName = (EditText)findViewById(R.id.editFirstName);
editLastName = (EditText)findViewById(R.id.editLastName);
buttonAdd = (Button)findViewById(R.id.buttonAdd);
buttonShow = (Button)findViewById(R.id.buttonShow);
buttonTest = (Button)findViewById(R.id.buttonTest);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "added to database", Toast.LENGTH_SHORT).show();
database.open();
long id = database.insertContact("Test Contact", "Test Email") ;
database.close();
}
});
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent("com.id11020260.exercise6part2.MyListActivity");
startActivityForResult(i, request_Code);
}
});
buttonShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
String destPath = "/data/data/" + getPackageName() +
"/databases";
File f = new File(destPath);
if (!f.exists()) {
f.mkdirs();
f.createNewFile();
//---copy the db from the assets folder into
// the databases folder---
CopyDB(getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath + "/MyDB"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//---get all contacts---
database.open();
Cursor c = database.getAllContacts();
if (c.moveToFirst())
{
do {
DisplayContact(c);
} while (c.moveToNext());
}
database.close();
}
});
}
private ArrayList<ListItemDetails> GetSearchResults() {
ArrayList<ListItemDetails> results = new ArrayList<ListItemDetails>();
item_details = new ListItemDetails();
database.open();
Cursor c = database.getAllContacts();
if (c.moveToFirst())
{
do {
item_details.setFirstName(c.getString(1));
results.add(item_details);
} while (c.moveToNext());
database.close();
}
return results;
}
public void CopyDB(InputStream inputStream,
OutputStream outputStream) throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.famous_person, menu);
return true;
}
public void DisplayContact(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Name: " + c.getString(1) + "\n" +
"Email: " + c.getString(2),
Toast.LENGTH_LONG).show();
}
}
这是堆栈跟踪,我不确定这是否完全正确
答案 0 :(得分:0)
试试这个: -
lv.setListAdapter(new ArrayAdapter(MyListActivity.this, android.R.layout.simple_list_item_1,结果));