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
Related
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.
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
}
});
I am trying to create an android application using a database from Parse.com. I am using a custom adapter to create a listview. I don't find any errors with the code and yet the listeview is not showing up. Nothing there in the logcat as well. Just the listview does not show up.
lv = (ListView)findViewById(R.id.listView);
mProgress = (ProgressBar)findViewById(R.id.check_progress);
mProgress.setVisibility(View.VISIBLE);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Sellers");
query.orderByAscending("Name");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
studentsList = new ArrayList<Sellers>();
for (ParseObject ob : parseObjects) {
s = new Sellers();
s.setName(ob.getString("Name").toString());
s.setAddress1(ob.getString("Address1").toString());
s.setAddress2(ob.getString("Address2").toString());
s.setShopName(ob.getString("ShopName").toString());
s.setEmail(ob.getString("Email").toString());
s.setPhone(ob.getString("Phone").toString());
s.setZipcode(ob.getString("Zipcode").toString());
studentsList.add(s);
}
adapter = new ListviewAdapter(CheckStatus.this, studentsList);
lv.setAdapter(adapter);
mProgress.setVisibility(View.GONE);
} else {
mProgress.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
This is the activity where I am invoking the listview.
public class ListviewAdapter extends BaseAdapter{
private final static String TAG = ListviewAdapter.class.getSimpleName();
private Context activity;
private LayoutInflater inflater = null;
private List<Sellers> sellers;
int layout;
public ListviewAdapter(Context activity, List<Sellers> sellers) {
this.activity = activity;
this.sellers = sellers;
inflater = LayoutInflater.from(activity);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder {
TextView name ;
TextView shop ;
TextView address1 ;
TextView address2;
TextView phone;
TextView email;
RelativeLayout rl;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View v =view;
ViewHolder holder = new ViewHolder();
if (view == null) {
LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.list_item_layout,null);
holder.name = (TextView)v.findViewById(R.id.seller_name);
holder.shop = (TextView)v.findViewById(R.id.shop_name);
holder.address1 = (TextView)v.findViewById(R.id.address1);
holder.address2 = (TextView)v.findViewById(R.id.address2);
holder.phone = (TextView)v.findViewById(R.id.phone);
holder.email = (TextView)v.findViewById(R.id.emailID);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Sellers s = sellers.get(position);
// String a = s.Name;
// Log.d(TAG, a);
holder.name.setText(s.getName());
holder.shop.setText(s.getShopName());
holder.address1.setText(s.getAddress1());
holder.address2.setText(s.getAddress2());
holder.phone.setText(s.getPhone());
holder.email.setText(s.getEmail());
Log.d("CustomAdapter.class", "CustomAdapter");
// imageView.setImageDrawable(s.getPic());
return v;
}
}
And this is the custom adapter. There are no null pointer exceptions showing up in the logcat. I can't determine why the listview is not getting populated.
Try this;
#Override
public int getCount() {
return sellers.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position:
}
You have to implement your code on getCount() by return number of item listview will be created.
I have a LinkedList HashMap List<HashMap<String, String>>that I have created, I'm trying to send it to a custom adapter that I found but it is for a Map. What the best way to convert it to accept LinkedList Hashmap?
public class CustomListOutageAdapter extends BaseAdapter {
private final ArrayList mData;
public CustomListOutageAdapter( List<HashMap<String, String>> map) {
mData = new ArrayList();
mData.add(map);
}
#Override
public int getCount() {
return mData.size();
}
#Override
public HashMap.Entry<String, String> getItem(int position) {
return (HashMap.Entry) mData.get(position);
}
#Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.mylistoutagedetails, parent, false);
} else {
result = convertView;
}
HashMap.Entry<String, String> item = getItem(position);
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(R.id.name)).setText(item.getKey());
((TextView) result.findViewById(R.id.item)).setText(item.getValue());
return result;
}
}
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;
}
}