How to put image in HashMap by giving a specific url - java

private static final String picpic = "picpic";
private ArrayList < HashMap < String, Object>> myBooks;
myBooks = new ArrayList<HashMap<String,Object>>();
HashMap < String, Object> hm;
hm = new HashMap<String, Object>();
drawable=LoadImage("http://www.wauhaha.com/smart/company/album/pic.jpg");
hm.put(picpic, drawable);
myBooks.add(hm);
final ListView listView = (ListView)findViewById(R.id.list);
SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox, new String[]{picpic}, new int[]{R.id.image1});
listView.setAdapter(adapter);

See this Example Listview With Custom Adapter.....And see CustomizedListView Activity
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

If you want the Key to be Url then use this code.
//Declarations
public Map<String, Object> ImageMap = new WeakHashMap<String, Object>();
//Inserting into HashMap
public void addToHash(String url)
{
ImageMap.put(url,LoadImage(url));
}
// Get from HashMap
public Object getImageFromHash(String url)
{
ImageMap.get(url);
}
EDITED
You can use the Solution given by Samir,
you need to use ImageLoader.java, ImageCache.java, FileCache.java and Utils.java
For your adapter
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> urls;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<String> u) {
activity = a;
urls=u;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listbox, null);
ImageView thumb_image=(ImageView)vi.findViewById(your.imageview.id); // thumb image
imageLoader.DisplayImage(urls.get(position), thumb_image);
return vi;
}
}

Related

ExpandableListView doesn't appear in the right order

I am wriring an Android app, and I decided to use expandable list view. I have everything I need, list is working fine, but when I populate it, groups appear to be in different order than they should, here's an example:
As you can see, child values are fine, only parent values appear in random order. Here's the full code I have:
My fragment code:
public class BuildingsFragment extends Fragment {
public BuildingsFragment() {
// Required empty public constructor
}
public static BuildingsFragment newInstance(Context context) {
BuildingsFragment fragment = new BuildingsFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_buildings, container, false);
final ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListViewBuildings);
HashMap<String, List<String>> expandableListDetail = ExpandableListDataPump.getData();
List<String> expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
ExpandableListAdapter expandableListAdapter = new CustomExpandableListAdapter(getContext(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Fragment fragment = null;
Class fragmentClass = BuildingDetailsFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction trans = getFragmentManager().beginTransaction();
Bundle args = new Bundle();
args.putString("BUILDING_NAME", "test");
fragment.setArguments(args);
trans.replace(R.id.frameLayoutForFragments, fragment);
trans.addToBackStack(null);
trans.commit();
return false;
}
});
return view;
}
}
My custom adapter:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_buildings_row_layout, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_buildings_group_layout, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listGroup);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
The ExpandableListDataPump I am using:
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
List<String> abuildings = new ArrayList<String>();
abuildings.add("A-1");
abuildings.add("A-2");
abuildings.add("A-3");
abuildings.add("A-5");
abuildings.add("A-7");
expandableListDetail.put("A", abuildings);
List<String> bbuildings = new ArrayList<String>();
bbuildings.add("B-1");
bbuildings.add("B-2");
bbuildings.add("B-3");
bbuildings.add("B-5");
bbuildings.add("B-6");
expandableListDetail.put("B", bbuildings);
List<String> cbuildings = new ArrayList<String>();
cbuildings.add("C-1");
cbuildings.add("C-3");
cbuildings.add("C-4");
cbuildings.add("C-5");
cbuildings.add("C-6");
expandableListDetail.put("C", cbuildings);
List<String> dbuildings = new ArrayList<String>();
dbuildings.add("D-1");
dbuildings.add("D-2");
dbuildings.add("D-3");
dbuildings.add("D-5");
dbuildings.add("D-7");
expandableListDetail.put("D", dbuildings);
return expandableListDetail;
}
}
I think that's everything related to ExpandableListView. I would really like to leave HashMap, but if there is no other way, can I just change it to TreeMap and use it's sorting function (as a last resort)? Thanks for help.
P.S. While doing my ExpandableListView I was following this tutorial (I don't know if that matters): Tutorial
This is because HashMap doesn't maintain the order of the items as those are inserted. So everywhere in your code (Activity & Adapter) change HashMap to LinkedHashmap. Like
private LinkedHashMap<String, List<String>> expandableListDetail;
According to the Java Specs for HashMap, the order is not guaranteed:
...This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time...
So, if you want to guarantee the order of your groups, you'll have to use a different data structure.
See this answer for a similar discussion, and the suggested usage of LinkedHashMap, which maintains insertion order.

Got strange in BaseAdapter

It should be simple part of app with GridView and Images in it, when button click go to camera, capture new photo and add it to GridView
Here I'm apply ImageAdapter extends BaseAdapter
partItem = getPart(position);
StaticGridView gridView = (StaticGridView) view.findViewById(R.id.ImagesGrid);
imageAdapter = new ImageAdapter(mContext, partItem.P_Code, partItem.Images);
gridView.setAdapter(imageAdapter);
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Map<Integer, String> partImages;
public String P_Code;
private static final String TAG = "ImageAdapter";
public ImageAdapter(Context c, String _P_Code, Map<Integer, String> _partImages) {
this.mContext = c;
this.partImages = _partImages;
this.P_Code = _P_Code;
}
public void changeModelList(Map<Integer, String> _partImages) {
partImages = _partImages;
notifyDataSetChanged();
}
public int getCount() {
return partImages.size();
}
#Override
public Object getItem(int position) {
return partImages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(mContext)
.load(API.ApiUrlBase + "/imagepreview/android" + getItem(position))
.placeholder(android.R.drawable.ic_menu_gallery)
.into(imageView);
Log.d(TAG, position + ":" + String.valueOf(getItem(position)));
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
got in log
: 1:/data/44452.P1090185.JPG
: 2:/data/44452.P1090186.JPG
: 3:/data/44452.P1090187.JPG
: 0:/data/44452.P1090184.JPG
: 0:/data/44452.P1090184.JPG
it's ok becose of Picasso, but when i try to add new image
partItem.Images.put(imageAdapter.getCount(), mCurrentPhotoPath);
// imageAdapter.notifyDataSetChanged();
imageAdapter.changeModelList(partItem.Images);
got in log
{
4=file:/storage/sdcard/Pictures/JPEG_20160306_213748_1027396172.jpg,
1=/data/44452.P1090185.JPG,
0=/data/44452.P1090184.JPG,
3=/data/44452.P1090187.JPG,
2=/data/44452.P1090186.JPG
}
: 0:/data/44452.P1090184.JPG
and first image appears at the GridView but there is no new file.
Is anyone have an idea?
GridView screenshot
So simple...
I gues BaseAdapter can't work with HashMap
changed this
public Map<Integer, String> partImages;
to this
public ArrayList partImages;

Displaying image retrieved from server

I am working on an Android app, in which the user uploads a photo. Now, I am calling a method from the server, which will return me a list of users. I would like to display this list along with the photos.
The images stored in the server are in byte-array. So I am converting them to String and then to Bitmap for showing. Unfortunately, it is not working as I am doing stuff with a Adapters as well. The image retrieved in adapter class is null. What am I doing wrong?
OtherUsers.java :
public class OtherUsers extends Activity {
private PersonServiceImpl personService = new PersonServiceImpl();
private static volatile List<RestPerson> restPersonList = new ArrayList<>();
public static final String firstName = "firstname";
public static final String userImage = "userimage";
static final Long userId = (long)0;
ListView listView;
UserAdapter userAdapter;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.displayuserlist);
restPersonList = this.personService.getOtherUsers();
// Base64 base64Encoder = new Base64();
ArrayList<HashMap<String, String>> usersArrayHashList = new ArrayList<>();
for(RestPerson restPerson : restPersonList){
HashMap<String, String> restDisplay = new HashMap<>();
restDisplay.put("userId",String.valueOf(restPerson.getUserId()));
restDisplay.put("firstName",restPerson.getFirstName());
restDisplay.put("userImage","data:image/png;base64," + Base64.encode(restPerson.getProfilePhoto()));
usersArrayHashList.add(restDisplay);
}
listView = (ListView) findViewById(R.id.usersdisplaylist);
userAdapter = new UserAdapter(this,usersArrayHashList);
listView.setAdapter(userAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Long userid = restPersonList.get(position).getUserId();
Log.d("Userid is ", String.valueOf(userid));
}
});
UserAdapter class :
public class UserAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public UserAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.userprofiles, null);
TextView username = (TextView) view.findViewById(R.id.userName);
ImageView userImage = (ImageView) view.findViewById(R.id.userImage);
HashMap<String, String> usersList = new HashMap<>();
usersList = data.get(position);
username.setText(usersList.get(OtherUsers.firstName));
String image = usersList.get(OtherUsers.userImage);
Log.d("Image is ",image);
byte[] newImageBytes = image.getBytes();
Bitmap bitmap = BitmapFactory.decodeByteArray(newImageBytes,0,newImageBytes.length);
userImage.setImageBitmap(bitmap);
userImage.setImageBitmap(null);
return view;
}
}
If there is any other information required, kindly let me know. Thanks.
Update
Sorry, forgot to put LogCat
08-24 13:43:16.618 29948-29948/internetlegion.com.gradlecheck E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: internetlegion.com.gradlecheck, PID: 29948
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at internetlegion.com.gradlecheck.Adapters.UserAdapter.getView(UserAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17547)
data struct that you have used in adapter is very strange. it's better to create one class with three property and send one list of those to adapter class.
any way, your problem is :
public static final String userImage = "userimage";
you define userImage with value userimage, then you insert into hashmap with "userImage" that is not equal with value of userImage ( value have lower case i) then you have try to get "userimage" from hashmap that is not exists, so value is null.
Answer:
you can change following line:
restDisplay.put("userImage","data:image/png;base64," + Base64.encode(restPerson.getProfilePhoto()));
to
restDisplay.put(userImage,"data:image/png;base64," + Base64.encode(restPerson.getProfilePhoto()));
but better way is create custom object.

Android : Setting and getting clicked item id for a list containing text and images.

I am working on an Android project in which I have a list of items shown to the user along-with its images. The problem right now is because of the modifications I had to do to set the list, I don't know how I can change the code to include the id of the object which is being clicked by the user. I would not like the position, but the id which is received from the server. Any help would be nice. Thanks a lot. :-)
Please note, relevant code is more in the first class below, I have just put the LazyAdapter if there are modifications requried in it. Thanks a lot.
GroupCanvasActivity.java :
public class GroupCanvasActivity extends Activity {
private static volatile Long groupAccountid = (long) 0;
List<RestCanvas> restCanvasList = new ArrayList<>();
private CanvasServiceImpl canvasService = new CanvasServiceImpl();
LazyAdapter lazyAdapter;
ListView listView;
static final String mcanvasid = "canvasid";
static final String mcanvastitle = "canvastitle";
static final String mcanvasname = "mcanvasname";
static final String mcanvasimage = "mcanvasimage";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.canvaslayout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
groupAccountid = extras.getLong("groupid");
}
restCanvasList = this.canvasService.getGroupCanvasForGroupAccount(groupAccountid);
ArrayList<HashMap<String, String>> canvaslist = new ArrayList<HashMap<String, String>>();
for (RestCanvas restCanvas : restCanvasList) {
HashMap<String, String> canvasDisplay = new HashMap<>();
// The below mcanvasid is what I would like to get when clicked
canvasDisplay.put("mcanvasid", String.valueOf(restCanvas.getMcanvasid()));
canvasDisplay.put("mcanvastitle", restCanvas.getMcanvastitle());
canvasDisplay.put("mcanvasname", restCanvas.getMcanvasname());
canvasDisplay.put("mcanvasimage", restCanvas.getMcanvasimage());
canvaslist.add(canvasDisplay);
}
listView = (ListView) findViewById(R.id.canlist);
lazyAdapter = new LazyAdapter(this, canvaslist);
listView.setAdapter(lazyAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// In the for loop above, you can see the mcanvasid, which I would like to use here.
}
});
}
}
LazyAdapter.java :
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_group_canvas, null);
TextView canvasName = (TextView)vi.findViewById(R.id.canvasname); // id
TextView canvasTitle = (TextView)vi.findViewById(R.id.canvastitle); // title
ImageView canvasImage=(ImageView)vi.findViewById(R.id.canvasimage); // image
HashMap<String, String> canvasList = new HashMap<String, String>();
canvasList = data.get(position);
canvasName.setText(canvasList.get(GroupCanvasActivity.mcanvasid));
canvasTitle.setText(canvasList.get(GroupCanvasActivity.mcanvasname));
canvasImage.setImageBitmap(convertByteArrayToBitmap(canvasList.get(GroupCanvasActivity.mcanvasimage)));
return vi;
}
private Bitmap convertByteArrayToBitmap(String string){
byte [] encodeByte= Base64.decode(string, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
}
}
Any help would be nice. Thank you. :-)
Do like this :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String mcanvasid = restCanvasList.get(position).get("mcanvasid"); // Here you get the mcanvasid
}
});
try this. In your adapter:
public Object getItem(int position) {
return d.get(position);
}
, in your activity:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String output = (HashMap<String, String>)(parent.getAdapter.getItem(position)).get("mcanvasid");
//do what you want with output
}
});

check value from custom listview if already exists

I have two custom listview in the same activity. Then, second listview can get the itemValue and itemID selected from the first listview. Then now, how do I check if the itemID is already exists in the second listview? Thanks in advance.
This is my second listview
private void listOrder(String itemValue, String itemID)
{
HashMap<String, String> map = new HashMap<String, String>();
String quantity = "1";
map.put(FOODID3, itemID);
map.put(FOODNAME3, itemValue);
map.put(FOODQUANTITY3, quantity);
LIST3.add(map);
LISTORDER = (ListView) findViewById(R.id.listOrder);
List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
LISTORDER.setAdapter(adapter);
adapter.notifyDataSetChanged();
LISTORDER.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
}
});
}
And this is the class of the listview
public class List3Adapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public List3Adapter(Activity a, ArrayList<HashMap<String, String>> d)
{
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.row, null);
}
TextView foodID3 = (TextView)vi.findViewById(R.id.foodID3);
TextView foodname3 = (TextView)vi.findViewById(R.id.foodName3);
TextView foodquantity3 = (TextView)vi.findViewById(R.id.foodQuantity3);
HashMap<String,String> food = new HashMap<String, String>();
food = data.get(position);
foodID3.setText(food.get(MainActivity.FOODID3));
foodname3.setText(food.get(MainActivity.FOODNAME3));
foodquantity3.setText(food.get(MainActivity.FOODQUANTITY3));
return vi;
}
}
You can check for the key in hash map to check if the itemID is already exists or not
if(data.containsKey(itemID))
{
// your code
}
where data is a HashMap<String, String> object

Categories

Resources