Reputation: 1509
Tried so many ways and read huge amounts of text, but I still can not understand how to solve my problem. The idea is that there is HashMap that is filled with a loop "for" with data from the database. Further, this parameter (HashMap) is passed to listViewContent then created adapter and eventually fills the listView. The fact is that every row in the listView has 2 textView and 1 imageView and I need to get text from the second textView of this listView, the data is stored in the database.
Here is my code blocks: HashMap block
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);
Adapter constructor:
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);
Also I tried to get keys of my map and I did this -
map.keySet()
then I've got the following:
[date,image,text]
and I want to know how can I get the first element of "text" key of this map. I need only first text because I want to put this text in the notification body.
Upvotes: 0
Views: 7021
Reputation: 53
hashmap does not preserve the insertion order.So it is better to use a LinkedHashMap to achieve what you are looking for .Please check out this article if you waant to know more http://java.dzone.com/articles/hashmap-vs-treemap-vs
Upvotes: 0
Reputation: 25950
If you use LinkedHashMap
, the insertion order of elements will be preserved.
Map<String, Object> map = new LinkedHashMap<String, Object>();
Then, to retrieve the first inserted entry from your map, you can use:
List<Entry<String, Object>> list =
new ArrayList<Entry<String, Object>>(map.entrySet());
Entry<String, Object> firstInsertedEntry = list.get(0);
Upvotes: 6
Reputation: 1600
I am not understating it wrong you need to get the key name as "text" from your map is so
try
String text = map.get("text");
Upvotes: 0
Reputation: 4462
HashMap
s are unordered, there is no concept of pulling out the first entry.
You may want to use a LinkedHashMap
Upvotes: 0
Reputation: 42176
HashMap doesn't have a concept of "first". If you want to preserve iteration order, use a LinkedHashMap instead.
Upvotes: 1