Converting Map to Linkedlist hashmap adapter - java

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;
}
}

Related

notifyDataSetChanged() doesn't refresh ListView

I have the following problem:
I am trying to load some data from firebase and after the data is loaded and stored in a HashMap I call the "updateAdapter" function (declared in the adapter below) and although the data has being changed the ListView doesn't refresh until some later random-(IMO) moment. I read couple of posts related to my problem and tried different solutions suggested there but they don't fix my problem. And by the way - Yes, I checked if the adapter loses the reference to the list and it doesn't.
My adapter looks like this:
public class NoteAdapterHashMap extends BaseAdapter{
private ArrayList mData = new ArrayList();
public NoteAdapterHashMap(Map<String, Note> map) {
mData.addAll(map.entrySet());
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map.Entry<String, Note> getItem(int position) {
return (Map.Entry) mData.get(position);
}
#Override
public long getItemId(int position) {
// TODO implement 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.item_note_listview, parent, false);
} else {
result = convertView;
}
Note note = getItem(position).getValue();
if(note!=null){
TextView title = (TextView) result.findViewById(R.id.list_note_title);
TextView date = (TextView) result.findViewById(R.id.list_note_date);
TextView content = (TextView) result.findViewById(R.id.list_note_content);
title.setText(note.getmTitle());
date.setText(note.getDateFormatted(parent.getContext()));
content.setText(note.getmContent());
if(note.getmContent().length()>50){
content.setText(note.getmContent().substring(0,50));
}
}
return result;
}
public void updateAdapter(HashMap<String,Note> map){
this.mData.clear();
this.mData.addAll(map.entrySet());
this.notifyDataSetChanged();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
}
}
Thanks in advance !

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.

Order items in a Multi Values Listview in Android

I'm looking to some way for sort my items inside my listview. Which is a multi values Listview with 3 items for each row. I use a Custom ListViewAdapter to achieve this, but they are sorted in order of introduction inside the ArraysList. Something like:
1-2-3-4-5-6....
I want to inverse this order, having in top of my Listview the last item introduced:
6-5-4-3-2-1....
Here is my Custom ListViewAdapter:
public class ListViewAdapter extends BaseAdapter {
static class ViewHolder {
TextView number;
TextView elapsedTime;
TextView totalTime;
}
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public ArrayList<HashMap<String, String>> list;
Activity activity;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
final ViewHolder viewHolder;
if(convertView == null){
convertView = inflater.inflate(R.layout.timer_row, null);
viewHolder = new ViewHolder();
viewHolder.number = (TextView) convertView.findViewById(R.id.number);
viewHolder.elapsedTime = (TextView) convertView.findViewById(R.id.elapsed_time);
viewHolder.totalTime =(TextView) convertView.findViewById(R.id.total_time);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map=list.get(position);
viewHolder.number.setText(map.get(FIRST_COLUMN));
viewHolder.elapsedTime.setText(map.get(SECOND_COLUMN));
viewHolder.totalTime.setText(map.get(THIRD_COLUMN));
return convertView;
}
}
And here is how I call it in my activity:
//ListView Adapter
mListviewLayout = (RelativeLayout)v.findViewById(R.id.listview_times_layout);
mTimesListview = (ListView)v.findViewById(R.id.times_listview);
mTimesArrayList = new ArrayList<HashMap<String,String>>();
mListViewAdapter= new ListViewAdapter(mActivity, mTimesArrayList);
mTimesListview.setAdapter(mListViewAdapter);
Finally, here is how I introduce items inside the Hashmap:
btnNextCicle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isTimerPaused()) {
onTimerStop();
} else {
mListviewLayout.setVisibility(View.VISIBLE);
mCiclesCounter++;
HashMap<String, String> newEntry = new HashMap<String, String>();
newEntry.put(ListViewAdapter.FIRST_COLUMN, "#" + mCiclesCounter);
newEntry.put(ListViewAdapter.SECOND_COLUMN, Util.getTimeForTimer(mElapsedCicleTime));
newEntry.put(ListViewAdapter.THIRD_COLUMN, Util.getTimeForTimer(mTimeInMilliseconds));
mCicleStartTime = SystemClock.elapsedRealtime();
mTimesArrayList.add(newEntry);
}
}
});
You can subclass HashMap<String, Sting> and make it implements Comparable. Then you can use Arrays.sort(yourComparable) to sort the array the way you want.

how to use notifyDataSetChanged()

Hi i have problem with my adapter, i don't know how to refresh my data in ListView when i use adapter.notifyDataSetChanged() after data changing nothing happen. I wonder if i have to override notifyDataSetChanged() in this class. I hope you will help me.
Here is my adapter class:
public class ListViewAdapter extends BaseAdapter{
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.lista_wlasnych_spolek, null);
txtFirst=(TextView) convertView.findViewById(R.id.nazwa_spolki);
txtSecond=(TextView) convertView.findViewById(R.id.wartosc_akt);
txtThird=(TextView) convertView.findViewById(R.id.wartosc_kupna);
txtFourth=(TextView) convertView.findViewById(R.id.wartosc_calosci);
}
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
}
here im adding my fragment class which use this adapter:
public class Tab2Fragment extends Fragment {
private ArrayList<HashMap<String, String>> list;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";
public ListViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.spolki_portfel, container, false);
ListView listView=(ListView) v.findViewById(R.id.listView1);
list=new ArrayList<HashMap<String,String>>();
LayoutInflater inflater1 = getActivity().getLayoutInflater();
View header = inflater1.inflate(R.layout.naglowek_wlasne_spolki, listView, false);
listView.addHeaderView(header);
for(int i = 0 ; i < MainActivity.zmienne.lista_moich_spolek.size();i++){
dodaj_spolke(MainActivity.zmienne.lista_moich_spolek.get(i).nazwa,MainActivity.zmienne.lista_moich_spolek.get(i).wartosc_aktualna,MainActivity.zmienne.lista_moich_spolek.get(i).ilosc,MainActivity.zmienne.lista_moich_spolek.get(i).cena_kupna);
}
adapter=new ListViewAdapter(getActivity(), list);
listView.setAdapter(adapter);
//THIS INCREASE VALUE
MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++;
adapter.notifyDataSetChanged();
//CHECKING IF VALUE IS HIGHER
System.out.println("cena " + MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna);
return v;
}
Not exactly, but you're probably not actually updating the adapter's list. Make a method in your adapter that looks like this
public void updateItems(ArrayList<HashMap<String, String>> newList) {
list.clear();
list.addAll(newList);
this.notifyDataSetChanged();
}
Then call this from wherever you have the adapter set up
mAdapter.updateItems(newList)
Your problem might be because you don't have the right Adapter class. Try this one, I didn't had time to test it but it should work, also call notifiyDataSetChanged() after adding new elements to that list.
public class ListViewAdapter extends ArrayAdapter<HashMap<String, String>> {
public ArrayList<HashMap<String, String>> list;
private Activity activity;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super(activity, R.layout.lista_wlasnych_spolek, list);
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public HashMap<String, String> getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ListViewHolder();
convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ListViewHolder) convertView.getTag();
}
HashMap<String, String> map=list.get(position);
listViewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
listViewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
listViewHolder.txtThird.setText(map.get(THIRD_COLUMN));
listViewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
public class ListViewHolder {
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
}
}
Ok, im sorry for wasting your time it was my fault i missed that i was changing source of data but no exacly ListView I should change list.get(0).put("Fourth", "5"); but i was changing MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++; and I wondered why it didn't work.

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