您好我创建了一个列表视图,用户可以将其数据保存在数组适配器中。但问题是当我从db连接它时显示强制关闭错误.......
private void populateAdsFromMetadata() {
String [] titles = getResources().getStringArray(R.array.title);
String [] detailedMessages = getResources().getStringArray(R.array.detailed_message);
String [] ownerName = getResources().getStringArray(R.array.owner_name);
String [] ownerEmail = getResources().getStringArray(R.array.owner_email);
String [] price = getResources().getStringArray(R.array.price);
String [] image = getResources().getStringArray(R.array.images);
String [] id = getResources().getStringArray(R.array.id);
ads.clear();
for(int i = 0; i < titles.length; i++){
Advertisement ad1 = new Advertisement(titles[i],
detailedMessages[i], ownerName[i],
ownerEmail[i], image[i], Integer.parseInt(price[i]), Integer.parseInt(id[i]));
ads.add(ad1);
}
}
这里是我为标题和详细消息创建数据库的类......
Intent intent = getIntent();
Bundle sender = intent.getExtras();
String senderValue = sender.getString("sender");
ListView listView = (ListView) findViewById(R.id.listView1);
// here we are displaying title i.e.detail message
String[] arrayColumns = new String[]{"title","detailed"};
//arrayViewID contains the id of textViews
// you can add more Views as per Requirement
// textViewSMSSender is connected to "address" of arrayColumns
// textViewMessageBody is connected to "body"of arrayColumns
int[] arrayViewIDs = new int[]{R.id.title,R.id.detailed_message};
Cursor cursor;
cursor = getContentResolver().query(Uri.parse("content://title/detailed_message"), null, null, null, null);
// create an Adapter with arguments layoutID, Cursor, Array Of Columns, and Array of ViewIds which is to be Populated
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.single_adds_layout, cursor, arrayColumns, arrayViewIDs);
listView.setAdapter(adapter);
if(senderValue.equals("button2")){
Collections.sort(Button_mak.ads, new Comparator<Advertisement>() {
@Override
public int compare(Advertisement lhs, Advertisement rhs) {
return lhs.getPrice() - rhs.getPrice();
}
});
}
if(senderValue.equals("button3")){
Collections.sort(Button_mak.ads, new Comparator<Advertisement>() {
public int compare(Advertisement lhs, Advertisement rhs) {
return lhs.getTitle() .compareTo(rhs.getTitle()) ;
}
});}
if(senderValue.equals("button1")){
Collections.sort(Button_mak.ads, new Comparator<Advertisement>() {
public int compare(Advertisement lhs, Advertisement rhs) {
return lhs.getId() - rhs.getId();
}
});}
Advertisement[] array = Button_mak.ads.toArray(new Advertisement[Button_mak.ads.size()]);
listView.setAdapter(new MyAdapter(this, array));
}
我知道我做了一些非常愚蠢的事实际上我是android的新手:-(请帮助我.........