EDIT: I'm Still stuck in this Problem so i figured out and i need to, every time that i add a new item to the list, i need to read the status of the CheckBoxes and EditTexts and save them somewhere, then i have to use the data and add a new item, and finally put them one more time on the ListView, so, i don't know what is the right way to go over the ListView to get those states. Someone can help me with that? Bellow is the question that i have posted before.
I'm working on a app for my restaurant, and for the orders i have a list view with two checkboxes, one EditText and a Textview, so i have an
ArrayList<HashMap<String,String>>
That has a list of dishes the client order, so, if i have some text in my EditText if i add new items to the list it erases it, i know i have to save the data but i dont know how, same with checkboxes, the state loses if i add a new item to the list.
This the code:
The variables:
HashMap<String,String> comandas = new HashMap<>();
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
And this is what i have to add the items to the listview
private void mostrarOrden3(){
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
//El cont se utiliza para que el primer elemento de la lista sea el mas nuevo
int cont =result.length();
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(cont-1);
String id = jo.getString(Config.TAG_ID_PLATOS);
String nombre = jo.getString(Config.TAG_NOMBRE_PLATOS);
String precio = jo.getString(Config.TAG_PRECIO_PLATOS);
String descripcion = jo.getString(Config.TAG_DESCRIPCION_PLATOS);
String nombrecategoria = jo.getString(Config.TAG_NOMBRECATEGORIA_PLATOS);
comandas = new HashMap<>();
comandas.put(Config.TAG_ID_PLATOS,id);
comandas.put(Config.TAG_NOMBRE_PLATOS,nombre);
comandas.put(Config.TAG_PRECIO_PLATOS,precio);
comandas.put(Config.TAG_DESCRIPCION_PLATOS,descripcion);
comandas.put(Config.TAG_NOMBRECATEGORIA_PLATOS,nombrecategoria);
System.out.println(nombre);
list.add(comandas);
cont--;
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
NuevaOrdenActivity.this, list, R.layout.list_item_platos,
new String[]{Config.TAG_NOMBRE_PLATOS},
new int[]{ R.id.name});
listView.setAdapter(adapter);
}
I need to save those items whit his states when the adapter is refreshed put again on it an continuing adding items.
What is the correct way to do that?
And i have another issue im using a SwiperRefreshLayout on the ListView and when im scrolling on it the text on the EditText duplicates on the lis below, im from Guatemala so my examples are in Spanish, but in the example apears two items to have the number 7 and i have only added one.
Example 1
Example 2 whit the other number 7
I would thank you so much for helping me whit this proyect. Have a nice day!
Related
i develop android apps since one year so i'm not able to solve this kind of problem. I searched many times on our friend google but 0 real result. This is a very precise question, i try to display images dynamically into listview items, i mean :
1- I receive an array of int from my database (ex : 5, 6, 7, 7)
2- I want the adpater to display differents images depending of this numbers
for exemple i receive : "result" = {"1", "2", "3"} i want the app to associate images to this numbers (Images come from drawable folder)
if (int number = 1) {
imageview into item layout.setImageRessource(R.id.blabla)
}else ...
I really don't know how do that, i tried building a custom adapter but it doesn't display the listview...
I'll be the happiest developper if somebody can tell me what the good way to do that.
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON2);
Poeple2 = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < Poeple2.length(); i++) {
JSONObject c = Poeple2.getJSONObject(i);
String username = c.getString(TAG_USERNAME);
int mood = c.getInt(TAG_MOOD);
HashMap<String, String> persons = new HashMap<String, String>();
persons.put(TAG_USERNAME, username);
persons.put(TAG_MOOD, String.valueOf(mood));
personList2.add(persons);
}
// i used a simple adapter but i know it's a wrong way
adapter = new SimpleAdapter(getActivity(), personList2, R.layout.modeluser4, new String[]{TAG_USERNAME, TAG_MOOD}, new int[]{R.id.tvHypo2, R.id.tvId});
list3.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
create a switch like
public void runTheSwitch(int num){
int number = num;
switch(number){
case 1:
//add image to listview
case 2:
//add image to listview
case 3:
//add image to listview
....and so on...
}
}
When you recieve the Array of numbers from database (lets call it ArrayNum), run a loop through those num.
For(int num : ArrayNum){
runTheSwitch(num);
}
Put this into a method and run the method before you set your adapter. So basically in this method you add items to your Arraylist like arraylist.add(); then after this you define an Object of your custom adapter and pass the Arraylist in your adapter.
Hope it helps
First of all, i am sorry because there is similar question that already been asked before. I've tried but it does not worked in my program. Hence, I need some help from you to help me to finish my program. Thank you.
Problem : my list cannot be click to go to next activity.
ListPending.java
public void getTable(String r) {
final ListView lv;
lv = (ListView) findViewById(R.id.lsApproval);
ArrayList<HashMap<String, String>> feedList= new ArrayList<HashMap<String, String>>();
try {
JSONArray jArray = new JSONArray(r);
JSONObject jInit = jArray.getJSONObject(0);
String valid = jInit.getString("valid");
String none="NO";
String empty="EMPTY";
TextView tv;
tv = (TextView)findViewById(R.id.tvNone);
if (valid.equals(none))
{
TextView tv1,tv2,tv3;
tv1 = (TextView)findViewById(R.id.tvSname);
tv2 = (TextView)findViewById(R.id.tvDest);
tv3 = (TextView)findViewById(R.id.tvDate);
tv.setText("\n\n\n\n\n\n" + "You do not have applicant under your approval" + "\n\n\n\n" );
tv1.setText("");
tv2.setText("");
tv3.setText("");
}
else if (valid.equals(empty))
{
TextView tv1,tv2,tv3;
tv1 = (TextView)findViewById(R.id.tvSname);
tv2 = (TextView)findViewById(R.id.tvDest);
tv3 = (TextView)findViewById(R.id.tvDate);
tv.setText("\n\n\n\n\n\n" + "No Pending List" + "\n\n\n\n" );
tv1.setText("");
tv2.setText("");
tv3.setText("");
}
else
{
String j=String.valueOf(jArray.length());
tv.setText("Have " +j+" applications need your approval");
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
String date = json.getString("dtfrom");
StringTokenizer tk = new StringTokenizer(date);
String dat = tk.nextToken(); // <--- yyyy-mm-dd
String staff = json.getString("staffname");
String dest = json.getString("destination");
HashMap<String, String> map = new HashMap<String, String>();
map.put("image", String.valueOf(R.mipmap.view));
map.put("staff", staff);
map.put("dest", dest);
map.put("date", dat);
feedList.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, feedList, R.layout.list_pending,
new String[]{"staff", "dest","date","image"},
new int[]{R.id.tvStaff, R.id.tvDest, R.id.tvDate, R.id.ibView});
lv.setAdapter(simpleAdapter);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
Toast.makeText(ListPending.this, "try" , Toast.LENGTH_SHORT).show();
}
});
}
}
it successfully show the data from database but not clickable.
This kind of problem occur when another object on the list is either focusable or clickable. The solution is to:
Inspect your layout xml file and make sure that none of the views you are having on your list has its android:clickable set to true.
Avoid to use any clickable view like a button on your list, as this will make your list not clickable.
First of all find the views which are getting focus and not allow list to be focused and touch in all other views put
focusable=false and focusintouchMode=false in xml
and if it is still not allowing your listview to be clicked then can share your problem again.
Seem your view is not taking focus in listview.It's the issue of view when you click the list row, it is always the capture other view event. apply below property to your main layout in row file (list_pending.xml) and let me know whether its works or not?
android:descendantFocusability="blocksDescendants"
or programmatically
listView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
hi can you help me how to display the next 10 data in json by click the next button. i have 50 data and i want to display first 10. Then when I click the next button, 11-20 will display in listview. Ill post my code below and i dont have any idea how to do it. Also when i click previous button it will go back to previous listview which is 1-10. Thanks!
doctordata = new ArrayList<Map<String, String>>();
try {
jsonObject = new JSONObject(d);
jsonArray = jsonObject.optJSONArray("Doctors");
int arraylength = jsonArray.length();
for (int i = 0; i < arraylength; i++) {
Map<String, String> doctormap = new HashMap<String, String>();
JSONObject jsonChildNode = jsonArray.getJSONObject(i);
doctor = jsonChildNode.optString("Name").toString();
specialty = jsonChildNode.optString("Specialty").toString();
doctormap.put("name", doctor);
doctormap.put("specialty", specialty);
doctordata.add(doctormap);
}
String[] from = {"name", "specialty"};
int[] views = {R.id.doctorlist_name, R.id.doctorlist_specialty,};
final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, doctordata, R.layout.doctor_list, from, views);
list.setAdapter(myadapter);
} catch (JSONException e) {
e.printStackTrace();
}
Define a class called Doctors, with fields String name and String Specialty, and add the Doctors to a list that you can iterate or convert to Array.
class Doctors {
private final String specialty;
private final String name;
public Doctors (){
specialty= "Spe1";
name = "name";
}
}
public String convertToJson(){
Gson gson = new Gson();
return gson.toJson(this);
}
Ok, there are several ways to do what do you want to achieve. I will explain you how I would do it:
Firts, in the doctorData arraylist you have all the items (50 items) that you need to show.
Create a partialDoctorData arraylist and assing to it only the first 10 items from doctorData, ok? and add this new arraylist to the SimpleAdaper.
So you will need to do instead of your code:
final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, **partialDoctorData**, R.layout.doctor_list, from, views);
list.setAdapter(myadapter);
So when the user click in the next button, you can clean the partialDoctorData content, add from the 11-20 items from the original doctorData arrayList and and and directly call to the
myadapter.notifyDataSetChanged();
(you don't have to repeat the step to create a new SimpleAdapter, only changing the values of the arraylist and calling to this method, the content of the list is going to be updated with the content of the partialDoctorData)
Try ;)
Try this one:
Android ListView with Load More Button
You can use pagination when 10 items will be loaded after that you will call agin api to get next 10 items
i have an ExpendableListView with 5 childs for each groupView. But when i whants to add more than one groupView to the list, nothing works...
the childView are not under the respective groupView(5 per groupView), but they will be added additional to the other childViews from other groupViews(10 per groupView if ther are 2 groupViews in the list, 15 if ther are 3...).
This means all available childviews are in everyGroupVeiw element
Can anybody help me? I am sorry for my bad english.
I think the mistake is in the Hashmap constellation, but i dont get it...
This is how i set the adapter, i am using expendableBaseAdapter like in this tut:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
public void prepareListData(){
dataChild = new HashMap<String, List<String>>();
dataHead = new ArrayList<String>();
List<String> uebung = new ArrayList<String>();
for(int i=0;i<list.size();i++){
//set Head
dataHead.add(list.get(i).getName());
//set Childs
uebung.add(String.valueOf(list.get(i).getGewicht()));
uebung.add(String.valueOf(list.get(i).getWdhlg()));
uebung.add(String.valueOf(list.get(i).getSaetze()));
uebung.add(String.valueOf(list.get(i).getKommentar()));
uebung.add(String.valueOf(list.get(i).getEinheit()));
dataChild.put(dataHead.get(i), uebung);
}
adapt = new ListViewAdapterUebungH(getActivity(), dataHead, dataChild);
explistView.setAdapter(adapt);
Okay i have found my mistake, i have to create the new Listuebung in the for-loop.
Because of this mistake, the informations was added to the listfor every new head element
List<String> uebung;
//size is the filled list from Trainingsplansammlung
for(int i=0;i<list.size();i++){
uebung = new ArrayList<String>();
//set Head
dataHead.add(list.get(i).getName());
//set Childs
uebung.add(String.valueOf(list.get(i).getGewicht()));
uebung.add(String.valueOf(list.get(i).getWdhlg()));
uebung.add(String.valueOf(list.get(i).getSaetze()));
uebung.add(String.valueOf(list.get(i).getKommentar()));
uebung.add(String.valueOf(list.get(i).getEinheit()));
dataChild.put(dataHead.get(i), uebung);
}
in my app i am using a list view. The data which i want to list out are from a database of my app and an xml server database.
For example i am getting some two set of data from server and store it in a array as follows
firstname = {arun, Arun, Rajesh}
lastname = {kumar, sundar, kannan}
Now again i am getting some data from my app database and store it in array as follows
first = {arul}
last = {raj}
Now i have combined the array list together as follows
firstname.addAll(first);
lastname.addAll(last);
Now i have the output as
{arun, Arun, Rajesh, arul}
{kumar, sundar, kannan, raj}
Now i want to list out these items as in the following image
how to do this, please help me......
If you finally get all your firstname and lastname in two separate array then it is very simple to display on ListView as you want
first create a layout which has a listview element lets us name main.xml
then create a another layout having two TextView in Horizontal layout orientation let us name mainitem.xml
so now you create a new activity then call setContentView(R.layout.main)
then retrive list view
and now create a adapter by following code
this code is in onCreate(Bundle b) method
ListView lv= (ListView)findViewById(R.id.listview1);
// create the grid item mapping
String[] from = new String[] {"col_1","col_2"};
int[] to = new int[] { R.id.firstname, R.id.secondname};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i <firstname.length && lastname.length; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("col_1",firstname[i]);
map.put("col_2",lastname[i]);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.mainitem, from, to);
lv.setAdapter(adapter);
create mainitem.xml file having two TextView which id are 1)R.id.firstname, 2) R.id.lastname in layout horizontal orientation
Create a bean to store data
Example
public class NameDetails {
String firstName = null;
String middleName = null;
NameDetails(String firstName, String middleName){
this.firstName = firstName;
this.middleName=middleName;
}
String getFirstName(){
return firstName;
}
String getMiddleName(){
return middleName;
}
}
Add data to that bean using
NameDetails name1 = new NameDetails("arun","kumar");
..
...
Create array of Bean Objects. use this array for listview
{name1 , name2, name3, name4}
As per the snap you have attached in the question, you need to define the custom listview adapter for the same.
For defining a custom row with 2 columnar output, you need to define an xml layout file containing 2 textviews side by side. Don't be confused, just go through this example. In this example you need to modify the row layout file. Make a row file as such two textviews will be displayed side by side.
Are you looking for notifyDataSetChanged()?