I am trying to search, track name from arraylist of hashmap. I have searched lots of sites on this, but couldn't get any solution.
Here is the code for creating Arraylist of hash maps:
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
searchText = (EditText) findViewById(R.id.searchText);
//Count from the server
int count = dataCount();
for (int i = 0; i < dataCount; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
// adding data to HashMap key => value
map.put(KEY_ID, trackNumber);
map.put(KEY_TITLE, trackTitle);
map.put(KEY_ARTIST, trackArtist);
map.put(KEY_DURATION, trackDuration);
map.put(KEY_THUMB_URL, trackAlbumArt);
// adding HashList to ArrayList
songsList.add(map);
}
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
On TextChanged I am trying to get the search results and update the adapter with new results.
I was trying out similar logic that was given in
HashMap Searching For A Specific Value in Multiple Keys
Is it there a better way to Search HashMap from ArrayList
But couldn't get any results.
Thanks!
Option 1: Iterate the list items and search for a match:
// this code wasn't tested - but I'm sure you'll get the idea
Map getSong(String title) {
for(Map song : songsList) {
if(song.get(KEY_TITLE).equals(title)) {
return song;
}
}
return Collections.emptyMap();
}
Option 2 (preferred): save the songs into another Map (instead of a list) and use the title as the "key":
Map<String, HashMap<String, String>> songsList = new HashMap<String, HashMap<String, String>>();
// add a song:
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, trackNumber);
map.put(KEY_TITLE, trackTitle);
map.put(KEY_ARTIST, trackArtist);
map.put(KEY_DURATION, trackDuration);
map.put(KEY_THUMB_URL, trackAlbumArt);
// adding HashList to Map
songsList.put(trackTitle, map);
then you don't have to iterate a list to find your song, but simply do:
Map getSong(String title) {
return songsList.get(title);
}
Related
I have a LinkedHashMap of contacts. For each contact I save inside the LinkedHashMap , a new LinkedHashMap with a variable amount of results of an Action. For example,
Contact 1(Key)
Action1(Key) - 2/10(Value)
Action2 - 3/10
.....
Action10 - 3/5
Contact 2
Action1 - 2/10
Action2 - 3/10
Action3 - 4/3
How I am going to put them in the following order like:
Action1 Action2 Action3 ..... Action10
Contact1 result
Contact2
To display them in gridview or tablelayout or webview?
I am trying with following to access elements but I get all of them in the arraylist
ArrayList Valuestrings = new ArrayList();
ArrayList KValuestrings = new ArrayList();
for (Map.Entry<String, LinkedHashMap<String, String>> entry : LnkHshPlayerData.entrySet())
{
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet())
{
KValuestrings.add(entry2.getKey());
Valuestrings.add(entry2.getValue());
}
}
i'm not sure it's what you ask for but if you do:
ArrayList strings = new ArrayList();
HashMap<String, HashMap<String, String>> selects = new HashMap<String, HashMap<String, String>>();
for (Map.Entry<String, HashMap<String, String>> entry : selects.entrySet()) {
for (Map.Entry<String, String> entry2 : entry.getValue().entrySet()) {
strings.add(entry2.getValue());
}
}
gridViewAdapter.addAll(strings);
I am trying to do a listview with 2 database collumn. I succeded but my problem is that only the first information of the database appears in the listview. If i have x data in the database it will show the x rows in the listview with only the first entry.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);
if (cursor.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
data.clear();
do {
data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
data.add(cursor.getString(cursor.getColumnIndex("nome")));
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
mylist.add(map);
} while (cursor.moveToNext());
SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
lista.setAdapter(resul);
I see two problems in your code:
You don't clear data (Like said in a previous answer) so you always get the first two values
You always use the same map. Since map is an object you will always modify the same and your list will be fill will one map.
To fix this two points try this:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);
if (cursor.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
do {
HashMap<String, String> map = new HashMap<String, String>();
data.clear();
data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
data.add(cursor.getString(cursor.getColumnIndex("nome")));
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
mylist.add(map);
} while (cursor.moveToNext());
SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
lista.setAdapter(resul);
look at this,
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
You always put the same key in the map, always "id, nome and hora" you need put diferent key.
ps: Why you create a List of Map? You don't need to this.
You need to clear data each time through the loop. You need to move your call to data.clear() to just after the do { loop begins. What you're doing at the moment will mean that after the first item is retrieved from the cursor, data will contain 2 items. After the second time it will contain 4, then 6, etc. You then always read the same two items. If you clear data it should fix your problem.
This is my code to check Internet and download XMl and show in a ListView
What I want is, while XML loads , show my custom layout or show a loading picture .
If I should Use AsynceTask please explain how . Thanks.
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_COST,KEY_NAME,KEY_DESC,KEY_ID}, new int
[] {
R.id.cost,R.id.name,R.id.desciption,R.id.linke});
setListAdapter(adapter);
Yes, an AsyncTask would be good for this.
Show whatever image/layout you want while loading in onPreExecute(). You can use a ProgressDialog/ProgressBar if you want or show something else here.
Do your parsing in doInBackground()
Then dismiss your ProgressDialog/ProgressBar, image, or whatever you decide to use in onPostExecute(). Also, update your Adapter in this method.
You can make the AsyncTask an inner class of your Activity if you don't need it anywhere else to make it easier.
Be sure you read through the AsyncTask Docs a couple times to understand how they work.
Here is an example of setting up an AsyncTask
#Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(activity, activity.getString(R.string.spinner_title), activity.getString(R.string.spinner_message_retrieving), true);}
#Override
protected void doInBackground() {
super.onPreExecute();
// do here network stuff
}
protected void onPostExecute(Void result) {
progress.dismiss();}
this is a part of your asynktask methode
I have the json array below which I am parsing and displaying it in listview row wise but I would like to combine/group same users to one row .Should I do it in android or mysql ? I am giving the working code for mysql too .I really appreciate any help.Thanks in Advance.
MYSQL:
http://sqlfiddle.com/#!2/19ea8/5
ANDROID:
wihin mainactivity for listview:
JSONArray arr = result.getJSONArray("JsonResultArray");
for(int i=0;i<arr.length();i++)
{
JSONObject e1 = arr.getJSONObject(i);
JSONObject json2 = (JSONObject) e1.get("data");
String name = json2.getString("name").trim();
String receiver = json2.getString("receiver").trim();
String sender = json2.getString("sender").trim();
String date = json2.getString("date").trim();
String msg = json2.getString("msg").trim();
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, Integer.toString(i+1));
map.put(KEY_TITLE, name);
map.put(KEY_msg, msg);
map.put(KEY_date,date);
// adding HashList to ArrayList
msgList.add(map);
}
}catch(Exception e){
e.printStackTrace();
}
in convert view:
HashMap<String, String> msgs = new HashMap<String, String>();
msgs = data.get(position);
name.setText(msgs.get(PopoverViewActivity.KEY_TITLE));
msg.setText(msgs.get(PopoverViewActivity.KEY_msg));
date1.setText(msgs.get(PopoverViewActivity.KEY_date));
If you want to group same users to one row. I suggest you to edit your mysql. You need to use
select * from table group by user
in your mqsql query, that can make you convenient to change your db and doesn't influence your UI.
In Android, just call your api and present data to listView.
I have a HashMap populated in the listview. This hashmap contains EditText that must be filled up by the user. My problem is I don't know to get the value of the EditText inside the HashMap.
EditText txt_iDesc = (EditText)findViewById(R.id.txt_iDesc);
mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < listSelectedFileNames.size(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(FILE_NAME, selectedFileNames[i]);
map.put(DESC, "");
map.put(UPLOADED_BY, "User");
map.put(DATE_UPLOADED, myDate);
map.put(ACTION, "Delete");
map.put(ID, String.valueOf(i));
map.put(FILE_URI, selectedFileUri[i]);
mylist.add(map);
}
The DESC in my HashMap holds the txt_iDesc. how can I possibly get the value of that EditText when the user enter something on the editText then save it. Thanks in advance. Below is my code for saving the data in the hashmap in the database and it only gets the value of the first editText.
SQLiteDatabase db = databaseHandler.getWritableDatabase();
db.beginTransaction();
for(HashMap<String, String> map : mylist)
{
ContentValues cv = new ContentValues();
cv.put(Constants.ATTACH_REPORTCODE, ReportCode);
cv.put(Constants.ATTACH_FILENAME, map.get(FILE_NAME));
cv.put(Constants.ATTACH_DESCRIPTION, txt_iDesc.getText().toString());
cv.put(Constants.ATTACH_FILELOCATION, map.get(FILE_URI));
cv.put(Constants.ATTACH_CREATEDBY, map.get(UPLOADED_BY));
cv.put(Constants.ATTACH_DATECREATED, map.get(DATE_UPLOADED));
db.insert(Constants.TABLE_ATTACH, null, cv);
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
Use
txt_iDesc.getText().toString();
If you want to fill the hashmap after the editText is being filled,
txt_iDesc.setOnTextChangedListener(this);
Implement the onTextChangedListener in your activity and in afterTextChanged method, you would probably fill the data.
HashMap<String, String> map = new HashMap<String, String>();
map.put(FILE_NAME, selectedFileNames[i]);
map.put(DESC, txt_iDesc.getText().toString.trim());
map.put(UPLOADED_BY, "User");
map.put(DATE_UPLOADED, myDate);
map.put(ACTION, "Delete");
map.put(ID, String.valueOf(i));
map.put(FILE_URI, selectedFileUri[i]);
mylist.add(map);
txt_iDesc.getText().toString.trim();
Add this line to hasmap value