I am trying to create a 4 column listview using arrayAdapter, and add values manually in the populateList()
private void populateList()
{
list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put(FIRST_COLUMN,"Diaries");
temp1.put(SECOND_COLUMN, "Products");
temp1.put(THIRD_COLUMN, "Rs. 400");
temp1.put(FOURTH_COLUMN, "ggg Unit");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put(FIRST_COLUMN,"Note Books");
temp2.put(SECOND_COLUMN, "Products");
temp2.put(THIRD_COLUMN, "Rs. 600");
temp2.put(FOURTH_COLUMN, "hhh Unit");
list.add(temp2);
}
My problem is i want to add list items dynamically from a two dimensional array
How it is possible??? the item count have the same size of the string array??
ie; insted of
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put(FIRST_COLUMN,"Diaries");
temp1.put(SECOND_COLUMN, "Products");
temp1.put(THIRD_COLUMN, "Rs. 400");
temp1.put(FOURTH_COLUMN, "ggg Unit");
i want to display
temp1.put(FIRST_COLUMN,"myArry[i][j]");
temp1.put(SECOND_COLUMN, "myArry[i][j]");
temp1.put(THIRD_COLUMN, "myArry[i][j]");
temp1.put(FOURTH_COLUMN, "myArry[i][j]");
in a for loop
also my doubut is how to create hashmap dynamically
I didn't quite understand what you want exactly, but may you can try this:-
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp = null;
for(int i=0;i<2;i++){
temp = new HashMap<String,String>();
for(int j=0;j<4;j++){
temp.put(String.valueOf(j), myArr[i][j]);
}
list.add(temp);
}
Note:- Instead of FIRST_COLUMN, SECOND_COLUMN, you'll have to use 1,2,3.. as your keys for HashMap.
try some thing like this.
HashMap<String,String> temp1 = null;
for (int i = 0; i < myArry.length; i++) {
temp1 = new HashMap<String, String>();
temp1.put(FIRST_COLUMN, myArry[i][0]);
temp1.put(SECOND_COLUMN, myArry[i][1]);
temp1.put(THIRD_COLUMN, myArry[i][2]);
temp1.put(FOURTH_COLUMN, myArry[i][3]);
list.add(temp1);
}
In your code I no where see a list view creation. You need to create a list view in your xml and populate the data using an adapter
Related
I'm trying to populate a ListView using a HashMap and a SimpleAdapter from a nested array with 12 columns and 12 rows
So here is my code
try {
String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
cursor.moveToFirst();
for (int i =0; i<cursor.getCount();i++){
for(int j = 0; j < cursor.getColumnNames().length; j++) {
String uname = cursor.getString(j);
array2[i][j]=uname;
}
cursor.moveToNext();
}
} finally {
cursor.close();
}
and then the simple adapter with hashmap
int[] to = new int[]{R.id.item2, R.id.item3, R.id.item4, R.id.item5, R.id.item6, R.id.item7, R.id.item8 ,R.id.item9, R.id.item10, R.id.item11, R.id.item12};
List<HashMap<String,String>> fillMaps = (List<HashMap<String, String>>) new ArrayList<HashMap<String, String>>();
for(int i = 0; i<10;i++){
HashMap<String,ArrayList> map = new HashMap<String, ArrayList>();
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, array2, to);
when I try this I get an error
SimpleAdapter(android.content.Context, java.util.List<? extends java.util.Map<java.lang.String,?>>, int, java.lang.String[], int[])' in android.widget.SimpleAdapter cannot be applied to
(com.example.thevenom1215.prueba.MainActivity, java.util.List<java.util.HashMap<java.lang.String, java.lang.String>>, int, java.util.ArrayList<java.lang.String>, int[])
Or I thought that I could convert the nested array to a simple array is that possible? If so how can I accomplish that? I tried this
String from [] = new String from[252]:
from[0]= array[0][1]+"|"+array[0][2]+"|"+array[0][3]+......+array[0][12];
but it doesn't work.
In the array2, which is array2[12][21], every row has 12 columns, which are information of a person, for example (name, age)
Cesar Joel Gurrola, xx
The first row of the array is: [Cesar, Joel, Gurrola, xx]
I need that in an array because further in my code I need String by String and not a whole String "Cesar, Joel, Gurrola, xx"
Sql query
sql="select b.CVE_CONTR, r.NO_RECIBO , a.NOM_SOLICIT ,r.NO_PARCIAL ,r.SDO_TOTAL, r.STS_PAGO ,a.AP_PATSOLICIT,a.AP_MATSOLICIT, " +
"f.DESCRIPCION, b.NO_PARCIALID , b.PAGO_PARCIAL, b.TOT_APAG from MFRF_M_SOLICITANTES a, MFRF_M_CONTPREV_H b, MFRF_M_CONTPREV_D_RECGEN_2 r," +
"C_PAQUETE f , C_PARCIALIDADES g, MFRF_C_COLONIAS c where b.CVE_CONTR = '"+etnumcontrato.getText().toString() + "' and r.STS_PAGO in ('1','10','11','12')" +
"and c.ID_COLONIA = a.ID_COLONIA and f.ID_PAQUETE = b.ID_PAQUETE and g.ID_PARCIALIDAD = b.ID_PARCIAL AND a.ID_SOLICIT = b.ID_SOLICITANTE ";
Overall, I wouldn't recommend this.
String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
An adapter displays one row-item at a time, not the whole table.
I would instead recommend a CursorAdapter since you do have a Cursor object, but I'll continue with a SimpleAdapter.
First, you need to create an XML layout, let it be named item_layout.xml. It contains all the Views that you want to bind the row from the database to. I believe these must all be TextViews for a SimpleAdapter.
String[] from = new String[] {"rowid", "fname", "mname", "lname"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
Then, loop over the rows of the cursor, and pull out the necessary information into a List of HashMaps. Note: this is one row of data each time that we are looping over the cursor results. And, as described earlier, you only have TextViews, so you can only store String values.
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
final int rows = cursor.getCount();
for (int i = 0; i < rows; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("fname", cursor.getString(cursor.getColumnIndexOrThrow("fname"));
map.put("mname", cursor.getString(cursor.getColumnIndexOrThrow("mname"));
map.put("lname", cursor.getString(cursor.getColumnIndexOrThrow("lname"));
fillMaps.add(map);
cursor.moveToNext();
}
Now that you have looped over all the rows of the data and are satisfied with that data, you just set the adapter.
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
R.layout.item_layout,
from, to);
listView.setAdapter(adapter);
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 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
I want to set imagese in listview but I don't do it properly. What's wrong with images I dont know .plz help me my code is here.
final ArrayList<HashMap<String,String>> dataListfeture = new ArrayList<HashMap<String,String>>();
ArrayList<HashMap<String,String>> dataList = new ArrayList<HashMap<String,String>>();
for(int j=0; j<nodesfeture.getLength(); j++)
{
Element e1 =(Element)nodesfeture.item(j);
Node node = nodesfeture.item(j);
HashMap<String, String> mapfeautre = new HashMap<String, String>();
mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
String rename = mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
rename.replaceAll("", "%20");
mapnamelist= mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
mapdistancelist =mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
mapvenuelist=mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
mapfeautre.put("imageupload",node.getAttributes().getNamedItem("imageupload").getNodeValue());
String strimageurl=mapfeautre.put("imageupload",node.getAttributes().getNamedItem("imageupload").getNodeValue());
mapimagelist="http://www.afterdarknightlife.com/"+strimageurl;
System.out.println("Image URL "+j+" :"+mapimagelist);
mylistfeature.add(mapfeautre);
}
System.out.println("data list = "+dataListfeture);
ListAdapter adapterfeaturetype = new SimpleAdapter(MainScreen.this, mylistfeature, R.layout.demophoto, new String[]
{
"imageupload","name", "distance","venuetype
},
new int[]
{
R.id.item_featureimage,
R.id.item_featurename,
R.id.item_featuredistance,
R.id.item_featurevenuetype,
});
may be this will help you...
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
Please give GreedDroid a try. It has very advanced set of list view items (including an item with an image) which you can use from code and from xml as well.