Get only one row from hashmap - java

I'm new for android.
Now I'm using hashmap to show my listview
Below is my SimpleAdapter,
private SimpleAdapter createSimpleAdapter() {
List<Map<String, String>> data = this.createData();
return new SimpleAdapter(this, data, R.layout.menuui,
new String[] {
"name", "food"
}, new int[] {
R.id.membername, R.id.food
});
}
I want to get only one row but if I use listview.getItemAtPosition(position).toString() just like this {food=Onigiri, name=Hashimoto Nanami}
How can I get only one row ?
If there's not enough information to let you know what I ask, please tell me, thanks.

Related

Turning ArrayList of Rows into a JTable

I decided it would be easiest to store my data in a custom Row object based on this post
Java data structure to store tabular data
But Im wondering how I actually go onto then create a JTable with this Array of row objects
My current attempt starts out looking like this:
public class TestTable extends JTable {
private final String[] columnNames = {"col1", "col2", "col3"};
public TestTable() {
DefaultTableModel model = new DefaultTableModel(columnNames, 10); //10 is just a placeholder for the number of rows as I dont know how to add the data yet
}
}
My data would be stored in an ArrayList that looks like this:
Row row1 = new Row("1", "2", "3");
ArrayList<String> data = new ArrayList<String>;
data.add(row1);
data.add(row2);
...
data.add(row10);
where my final ArrayList would just look like a list of row objects
List<String> data = List.of("1", "2", "3" );
model.addRow(data.toArray());
would be a simple approach

SQLite grouped items and expandable list views for android

so I'm stuck on something probably pretty basic. I've got data stored in an SQLite database table and want to display that in an expandable list view. The data basically forms as headings and subheadings. There's many sub-headings to a single heading.
Retrieving the data is pretty straightforward. The part I'm stuck at is taking that data and giving it to the expandable list view.
I want the expandable list view to display the headings as the item that can be expanded, with the subheadings as the child items. How can I do this?
Thanks in advance.
You must provide an adapter to ExpandableListView. There is a base class adapter that you can extend it for your needs.
For creating the data lists. Assume Column1 is Heading, Column2 is SubHeading, try this in activity/fragment:
ArrayList<String> headings = new ArrayList<>();
HashMap<String, ArrayList<String>> subheadings = new HashMap<>();
String heading, subheading;
do{
heading = cursor.getString(1);
subheading = cursor.getString(2);
ArrayList<String> tmpChild;
if(headings.contains(heading)){
tmpChild = subheadings.get(heading);
}else{
headings.add(heading);
tmpChild = new ArrayList<>();
}
tmpChild.add(subheading);
subheadings.put(heading, tmpChild);
}while (cursor.moveToNext());
For the adapter, try my answer here: Tree with checkBox
Hope that helps!

Extracting string array from object array

So I have arraylist modelData that populates a recyclerview using sqlite database in some activity.!
Now in my MainActivity I want an string arraylist of names from the modelData !
that's what did so far..
// inside the onCreate of MainActivity
//code ..
db = new DBHandler(this, null, null, 1);
modelData = new ArrayList<AzkarModel>();
modelData = db.getDataFromDB();
for (AzkarModel o : modelData) {
ArrayList<String> names = new ArrayList<>();
names.add(o.getName());
}
for (int i = 0; i< modelData.size();i++){
names.add(modelData.get(i).getName());
Log.i("The List Log", names);
}
Two problems
1) [FIXED] The names arraylist is showing the same element twice at first and end
I/ The List Log: [Mike, John, Sam, Nora, Mike]
2) The arraylist names doesn't get updated..! when I add/edit/delete from the recycler and go back to the MainActivity I don't see the new changes unless I close the app then open it again..! I can't use notifyDataSetChanged since there's no adapter here.!
Once you have an array list object, populate it inside a loop
ArrayList<String> modelNames = new ArrayList<>();
for (AzkarModel model : modelData) {
modelNames.add(model.getName());
}
Now you have an array list with model names inside. Concerning the second question, please use an recyclerview adapter here you can find a nice tutorial.

Parse.com Cloud Code and HashMaps in Android: How to set an array inside Hashmap?

I'm getting mad because of this. I'm using Parse.com to handle my Backend and I've to call a Cloud Code function giving this "structure":
{ "idShop":"asdf", "idCostumer":"zxcv", "selection": [
{"idOffer":"product1", "quantity":3 },
{"idOffer":"product2", "quantity":1 }
] }
It works fine if I use curl but, I've no idea how to do it with Android. So, the idea is put an array into a JSON value.
I've tried to put all the info into a String and into a HashMap array and I got no result.
Any idea??
Thanks for helping! :)
After Sunil answer:
This is what I'm doing and doesn't work:
HashMap<String, String [] > params = new HashMap<String, String []>();
params.put("idCostumer", new String[]{costumerId});
params.put("idShop", new String[]{idShop});
OfferListItem item;
ArrayList<String> selectionList = new ArrayList<String>();
for(int i=0; i<offerList.size();i++){
item = offerList.get(i);
if (item.getClicks()>0){
selectionList.add("\"idOffer\":"+item.getId()+", "+"\"quantity\":"+item.getClicks());
}
}
String [] selection = new String [selectionList.size()];
for (int i=0;i<selection.length;i++) {
selection[i]=selectionList.get(i);
}
params.put("selection", selection);
According to Parse's Android Guide, the correct data type to use for arrays is JSONArray. Try using JSONArray in place of String [].
try this way, put the String array into hashmap with this way
HashMap subjects = new HashMap<String, String[]>();
subjects.put("calculus",new String[] {"math","logic"});
subjects.put("chemisty",new String[] {"ions","electrons"});
subjects.put("biology",new String[] {"life","bacteria"});
for(String s:subjects.get("biology")){
System.out.println(s);
}
This might be late but try this .
In your cloud code, use use 2 array variables. The first will be for building your array objects and the second for building an 'ArrayList' so assuming you want to return an arraylist of courses and the mark 3 students got in each you will end up with something like this.
-Cloud code
studentMarks={};
courses=[ ];
For(int i=0;i<3;i++)
{
studentMarks={stud1:3,stud2:10,stud3:45}
courses.push(studentMarks);
}
response.success(courses)
-Then in your Android code pass an ArrayList of type Object
public void done(ArrayList<Object> courses, ParseException e) {
// cast the course to a HashMap
HashMap<String,Object> object= (HashMap<String,Object>)courses.get(0);
int stud1 = (Integer) object.get("stud1");
}

How can I convert a String to a String[] // Android development // Java

I am quite new to Java and am currently coding an Android application. In my "Shortcuts" class, I have this bit of code (more of course, but not useful to you, I don't think):
final String[] items = new String[]{ "Please select a category",scanner.getCategorys() };
#SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selection.setAdapter(adapter);
I have another class named "Scanner", which has this code:
String categorys = "test";
public String getCategorys() {
return categorys;
}
This does work, and in my Spinner, I can fill in my "Please select a category" option with a single value (i.e "test"). The problem is, I would like to be able to select multiple categories. If I do this in "Shortcuts" class:
final String[] items = new String[]{ "Please select a category","test","test2" };
It would work, but I would like to set it from the "Scanner" class, so I tried this:
String[] categorys = "test","test2";
public String[] getCategorys() {
return categorys;
}
But it just gives me the error:
String cannot be converted to String[]
I would be grateful for any help.
This is wrong:
String[] categorys = "test","test2";
Change it to
String[] categorys = {"test","test2"};
In your last code sample, you have to put { } around your strings, like so:
String[] categorys = { "test","test2" };
public String[] getCategorys() {
return categorys;
}
According to your edit above, you can't add a String[] to an existing String[]. You need to add every item in the String[] you get from getCategorys() to the other array. I would probably switch to an ArrayList<string> in this case, so you don't have to decide what size the array should be, then add each item.
You can initialize categorys as follows
String[] categorys = {"test","test2"};
I'd call the variable as 'categories'.
EDIT :
scanner.getCategorys() cannot be used as the constructor expects a String and not a string array. A good idea would be use ArrayList as pointed out by uncocoder. You could then just use the add method to include "Please select a category".
I recomment to use ArrayList, it's fast and easy like this
ArrayList<String> strings = new ArrayList<String>();
strings.add("SOME TEXT");
strings.get(0);
//and more

Categories

Resources