嘗試了很多方法並閱讀大量文本,但我仍然無法理解如何解決我的問題。這個想法是有一個HashMap,用來自數據庫的數據填充循環「for」。此外,這個參數(HashMap)被傳遞給listViewContent,然後創建適配器並最終填充listView。事實是,listView中的每一行都有2個textView和1個imageView,我需要從這個listView的第二個textView中獲取文本,數據存儲在數據庫中。如何獲得HashMap的第一個元素
這是我的代碼塊: HashMap中塊
if (posts != null) {
for (WallPostItem post : posts) {
//create new map for a post
Map<String, Object> map = new HashMap<String, Object>();
map.put(ATTRIBUTE_NAME_TEXT, post.text);
PictureItem postPicture = new PictureItem();
map.put(ATTRIBUTE_NAME_IMAGE, postPicture);
map.put(ATTRIBUTE_NAME_DATE, post.date);
if ((post.pictures != null) && (post.pictures.size() != 0)) {
//start downloading of pictures
String pictureLink = post.pictures.get(0);
if (dbHandler.hasPicture(pictureLink)) {
Bitmap image = dbHandler.getPicture(pictureLink);
int width = image.getWidth();
int height = image.getHeight();
if (width>800 || height>800){
int threeWidth = width/2;
int threeHeight = height/2;
Bitmap bmThree = Bitmap.createScaledBitmap(image, threeWidth,
threeHeight, false);
postPicture.picture = bmThree;}
else if (width>500 && width <800 || height>500 && height<800) {
int halfWidth = width;
int halfHeight = height;
Bitmap bmHalf = Bitmap.createScaledBitmap(image, halfWidth, halfHeight, false);
postPicture.picture = bmHalf;
} else postPicture.picture = image;
//Log.d("mytest", " HAS PICTURE " + pictureLink);
} else {
DownloadImageTask downloadImageTask = new DownloadImageTask(postPicture){
@Override
public void onResponseReceived(){
//Log.d("mytest", "adding picture to db: " + urldisplay);
if (bmImage.picture != null)
dbHandler.addPicture(bmImage.picture, urldisplay);
sAdapter.notifyDataSetChanged();
};
};
downloadImageTask.execute(pictureLink);
}
}
listViewContent.add(map);
適配器構造:
String[] from = {ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE, ATTRIBUTE_NAME_DATE};
int[] to = {R.id.tvText, R.id.ivImg, R.id.tvDate};
sAdapter = new SimpleAdapter(this, listViewContent, R.layout.news_item, from, to) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if ((position % 2) == 0) {
view.setBackgroundColor(Color.parseColor("#00E9E9E9"));
} else {
view.setBackgroundColor(Color.parseColor("#00F5F5F5"));
}
return view;
}
};
lvSimple.setAdapter(sAdapter);
此外,我試圖讓我的地圖的鑰匙和我這樣做 -
map.keySet()
然後我有以下幾點:
[date,image,text]
,我想知道我怎麼可以得到該地圖的「文本」鍵的第一個元素。 我只需要第一個文本,因爲我想將此文本放在通知正文中。
是的,這是我真正想要的!但現在我有另一個問題:我的地圖在方法中,但我需要獲取第一個元素,並將其放入onCreate方法中的String。我怎樣才能做到這一點?我試圖創建一個變量並將其分配給地圖,但後來我得到NullPointer – vendettacore
@vendettacore我無法遠程調試您的代碼。檢索第一個條目並將其存儲在全局變量中,然後在任何需要的地方使用它。你的代碼太混亂了,對不起,這是我的建議。 – Juvanis