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
Related
Hi i am converting from kilo to stone-pounds, i want to place stones in one edittext and pounds in one edittext , below is my method
public ArrayList<HashMap<String, Integer>> kiloTostomepound(int s) {
int stone = (int) (s * 2.2);
int stonevalue = stone/14;
int spound = (stone % 14);
arrayList = new ArrayList<HashMap<String,Integer>>();
HashMap<String, Integer> h1 = new HashMap<String, Integer>();
h1.put(STONE,stonevalue);
h1.put(STONEPOUND, spound);
arrayList.add(h1);
return arrayList;
}
i want to get values from the arryalist and set it to edittext.
Doing a below but givinf nullpointerexception
edt2.setText(String.valueOf(kiloTostomepound(arrayList.get(0).get(STONE))));
edt3.setText(String.valueOf(kiloTostomepound(arrayList.get(1).get(STONEPOUND))));
The below code traverses through the list and prints key and value.
for (int a =0; a<myList.size();a++)
{
HashMap<String, Integer> tmpData = (HashMap<String, Integer>) myList.get(a);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
while (it.hasNext()) {
String hmKey = (String)it.next();
Integer hmData = (Integer) tmpData.get(hmKey);
System.out.println("Key: "+hmKey +" & Data: "+hmData);
it.remove(); // avoids a ConcurrentModificationException
}
}
So if you need the first value try following.
HashMap<String, Integer> tmpData = (HashMap<String, Integer>) myList.get(0);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
// Get the first element
String hmKey = (String)it.next();
Integer hmData = (Integer) tmpData.get(hmKey);
// Set the key and value to Edit text
edt2.setText(hmKey+" "+hmData);
Seems like your assigning the value to the arrayList variable inside of the method. Hence the
kiloTostomepound(arrayList.get(0).get(STONE))
Will throw a nullpointer for arrayList.
If I understand your code correctly, the below code should do the trick. But I don't know the input for the kiloTostomepound method.
edt2.setText(String.valueOf(kiloTostomepound("INPUT TO METHOD").get(0).get(STONE)));
edt3.setText(String.valueOf(kiloTostomepound("INPUT TO METHOD").get(1).get(STONEPOUND)));
This is because your method returns the arrayList with the hashmaps inside.
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.
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.
private HashMap<String, String> mHashMap = new HashMap<String, String>();
mHashMap.put("a", "ajay");
mHashMap.put("h", "hiren");
mHashMap.put("d", "darshan");
mHashMap.put("a", "anand");
mHashMap.put("h", "harsah");
for (String key: mHashMap.keySet()) {
Log.e(key,"" + mHashMap.get(key));
}
My Result:
d - darsha
h - harshad
a - anand
I want all values from HashMap?
If you have any idea related to it, then help me.
HashMap will override the value if a key for that value exists already. If you need to have more values for a key, you should use a Collection as value fro your HashMap:
private HashMap<String, Vector<String>> mHashMap = new HashMap<String, Vector<String>>();
Vector<String> tmp = new Vector<String>();
tmp.add("ajay");
tmp.add("anand");
mHashMap.put("a", tmp);
tmp = new Vector<String>();
tmp.add("hiren");
mHashMap.put("h", tmp);
and so on..
I m using hashmap for a listview.
map = new HashMap<String, Object>();
map.put("name", R.string.information);
map.put("address", R.drawable.info3);
mylist.add(map);
// ...
my problem is that i cant load string in my hashmap (R.string.information)..when i run it,i can only see some numbers instead of my text..am i doing something wrong?thanks
<string name="information">Informations</string>
R.string.information is just an int. If you want the actual String, use
map.put("name", getString(R.string.information));
string.information returns a integer instead use
getString(R.string.information);
which will give you the string that you have stored in the String.xml
Yes you are doing wrong because you have print the id the the name and address not value
you should put value of the name and address tag using this one according to your requirement.
String mess = getResources().getString(R.string.name);
this.getString(R.string.name)//if you are in context
String mess = Resources.getText(R.string.name);
R.string.information gives you int value not String
You have to make change in creating hashmap
Your hasmap should be
HashMap map = new HashMap<String, Integer>(); instead of `HashMap map = new HashMap<String, Object>();`
Finally code wil be
HashMap map = new HashMap<String, Integer>();
map.put("name", R.string.information);
map.put("address", R.drawable.info3);
mylist.add(map);
Thanks
Deepak