How to add spinner inside a listview - android

i'd like each for row in listview has a spinner. But can not. please help me.
//Setting player
adapter = new SimpleAdapter(this, list, R.layout.custom_listitem_edit,
new String[] { "cat", "duration", "order", "id_pos"}, new int[] {
R.id.spn_edibt_selectcat,
/*R.id.spn_editbt_selectpos,*/
R.id.txt_editbt_dur,
R.id.txt_editbt_order,
R.id.txt_edit_idpos
});
populateList();
setListAdapter(adapter);
static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
/* Set thong tin cho listview */
private void populateList() {
cursor_position_mycurrent = dbHelper_position_mycurrent.fetchAllPositionMyCurrentById(id_bt);
startManagingCursor(cursor_position_mycurrent);
Log.e("SO dong lay duoc la", String.valueOf(cursor_position_mycurrent.getCount()));
if (cursor_position_mycurrent != null) {
cursor_position_mycurrent.moveToFirst();
while (cursor_position_mycurrent.isAfterLast() == false) {
HashMap<String, String> tmp = new HashMap<String, String>();
Log.e("DUR", cursor_position_mycurrent.getString(5));
Log.e("DUR", cursor_position_mycurrent.getString(6));
tmp.put("duration", cursor_position_mycurrent.getString(5));
tmp.put("order", cursor_position_mycurrent.getString(6));
tmp.put("id_pos", cursor_position_mycurrent.getString(0));
//Setting spinner category
cursor_position_allposition =dbHelper_position_allposition.fetchAllPositions();
array_spinner_category = new String[cursor_position_allposition.getCount()];
if (cursor_position_allposition != null) {
int i = 0;
cursor_position_allposition.moveToFirst();
while (cursor_position_allposition.isAfterLast() == false) {
array_spinner_category[i] = cursor_position_allposition.getString(0);
cursor_position_allposition.moveToNext();
i++;
}
cursor_position_allposition.close();
}
Log.e("ALL POS", String.valueOf(cursor_position_allposition.getCount()));
Spinner spinner = (Spinner) findViewById(R.id.spn_edibt_selectcat);
int m = 3;//Integer.parseInt(camping.rulesList.getMaxPers().get(camping.tipSel));
String[] array_spinner=new String[m];
for (int indice = 0; indice < m; indice++)
{
if (indice == 0) array_spinner[indice] = String.format("%d persona", indice+1);
else array_spinner[indice] = String.format("%d persone", indice+1);
}
final ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, array_spinner);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(aa);
list.add(tmp);
cursor_position_mycurrent.moveToNext();
}
}
}

Related

Listview not updating based on value on Array Adapter

I call the method below to update my listview
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
ListAdapter adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);
but that code is associated with a value that changes and also this is the other code
private ArrayList<plan_model> getItems_search(String param_cusname) {
Cursor data = myDb.get_search_plan(pattern_email, param_name);
int i = 0;
while (data.moveToNext()) {
String date = data.getString(3);
String remarks = data.getString(4);
items.add(new plan_model(cusname, remarks);
}
return items;
}
and this is my sorter
private ArrayList sortAndAddSections(ArrayList<plan_model> itemList) {
Collections.sort(itemList);
plan_model sectionCell;
tempList.clear();
tmpHeaderPositions.clear();
String header = "";
int addedRow = 0;
int bgColor = R.color.alt_gray;
for (int i = 0; i < itemList.size(); i++) {
String remarks = itemList.get(i).getRemarks();
String date = itemList.get(i).getDate();
if (!(header.equals(itemList.get(i).getDate()))) {
sectionCell = new plan_model(remarks, date);
sectionCell.setToSectionHeader();
tmpHeaderPositions.add(i + addedRow);
addedRow++;
tempList.add(sectionCell);
header = itemList.get(i).getDate();
bgColor = R.color.alt_gray;
}
sectionCell = itemList.get(i);
sectionCell.setBgColor(bgColor);
tempList.add(sectionCell);
if (bgColor == R.color.alt_gray) bgColor = R.color.alt_white;
else bgColor = R.color.alt_gray;
}
tmpHeaderPositions.add(tempList.size());
for (int i = 0; i < tmpHeaderPositions.size() - 1; i++) {
sectionCell = tempList.get(tmpHeaderPositions.get(i));
sectionCell.setDate(sectionCell.getDate() + " (" +
(tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");
}
return tempList;
}
my question is the value name changes but my listview is not how can I update my listview? because i need to update it based on search parameter
If your itemList is being updated properly, you don't need to create another instance of the adapter, just use notifyDataSetChanged():
private void createList() {
ListView list = (ListView) listView.findViewById(R.id.plan_list);
itemsList = sortAndAddSections(getItems_search(name));
adapter = new ListAdapter(getActivity(), itemsList);
list.setAdapter(adapter);
}
private void updateList() {
sortAndAddSections(getItems_search(name)); // Update itemList without re-assign its value, otherwise the adapter will loose reference
adapter.notifyDataSetChanged()
}
In getItems_search() add this line at the beginning:
items.clear();
Every time the value of name changes you have to do the following:
itemsList.clear();
itemsList = sortAndAddSections(getItems_search(name));
list.setAdapter(new ListAdapter(getActivity(), itemsList));

Implement expandalbelistview with no child

how to implement expandalbelistview with not any child
I am new in android I want to implement and expandalbelistview in which some groups have children and some have none.
Try This
It works for me
mExpandableListView = (ExpandableListView) findViewById(R.id.grouplist);
groupCollection = new LinkedHashMap<>();
List<String> newsCategories = new ArrayList<>();
List<String> nullcats = new ArrayList<>();
nullcats.add(null);
ArrayList<Categories> categories1 = new ArrayList<>();
//dataSource is my DBOpen helper class
categories1 = dataSource.findAllCategories();
if (categories1 != null) {
for (int i = 0; i < categories1.size(); i++) {
newsCategories.add(categories1.get(i).getCatagory_name_en());
}
} else {
newsCategories.add(null);
}
groupCollection.put(groupList.get(0), nullcats);
groupCollection.put(groupList.get(1), newsCategories);
expandableListAdapter = new ExpandableListAdapter(this, groupList, groupCollection);
mExpandableListView.setAdapter(expandableListAdapter);

How to repeat item in listview

I have the listview from the exel:
try {
Workbook wb = WorkbookFactory.create(getAssets().open("bos.xls"));
Sheet sheet = wb.getSheetAt(0);
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
IrrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
}
} catch(Exception ex) {
return;
}
When I use it I have the one value in my listview.
How can i correct it?
I tried to add this before "HashMap hm;":
for(int i1 = 0, l = IrrList.size(); i1 < l; i1++){
"I dont know what write here"
}
Try this..
Initilize the ArrayList before the for loop and set the listview adapter after the for loop
IrrList = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
}
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);

Multiple Spinners making various JSON calls

The aim of the application is to have spinners left (home team) and on the right (Away team). I can successfully populate the home team spinners but when i try to populate the away team spinners i get the error:
06-25 18:02:36.052: W/System.err(17916): org.json.JSONException: Value at 0 is null.
06-25 18:02:36.057: W/System.err(17916): at org.json.JSONArray.get(JSONArray.java:259)
06-25 18:02:36.057: W/System.err(17916): at org.json.JSONArray.getJSONObject(JSONArray.java:480)
06-25 18:02:36.057: W/System.err(17916): at app.league.npd.CaptainsInfo.useaJson(CaptainsInfo.java:309)
I can see that the error is coming from the "ateam" array, but i dont know what the problem is. I am new to android programming so any help would be appreciated.
Here is the code:
public class CaptainsInfo extends Activity {
JSONArray jsonArray = null;
JSONArray str_login = null;
public String items[];
public String aitems[];
private Spinner
spinner1, spinner2, spinner3, spinner4,
spinner5, spinner6, spinner7, spinner8,
spinner9, spinner10, spinner11, spinner12,
spinner13, spinner14;
private Button btnSubmit;
public String kode;
public String Home_team;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
addListenerOnSpinner2ItemSelection();
}
public void addItemsOnSpinner(String items[]) {
spinner3 = (Spinner) findViewById(R.id.player11);
spinner5 = (Spinner) findViewById(R.id.player12);
spinner7 = (Spinner) findViewById(R.id.player21);
spinner9 = (Spinner) findViewById(R.id.player22);
spinner11 = (Spinner) findViewById(R.id.player31);
spinner13 = (Spinner) findViewById(R.id.player32);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner5.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner7.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner9.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner11.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner13.setAdapter(adapter);
}
public void addItemsOnSpinner2(String aitems[]) {
spinner4 = (Spinner) findViewById(R.id.aplayer11);
spinner6 = (Spinner) findViewById(R.id.aplayer12);
spinner8 = (Spinner) findViewById(R.id.aplayer21);
spinner10 = (Spinner) findViewById(R.id.aplayer22);
spinner12 = (Spinner) findViewById(R.id.aplayer31);
spinner14 = (Spinner) findViewById(R.id.aplayer32);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner6.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner8.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner10.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner12.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, aitems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner14.setAdapter(adapter);
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.h_type);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//String test1 = parent.getItemAtPosition(position).toString();
String test = (String) spinner1.getSelectedItem();
//String test2 = (String) spinner2.getSelectedItem();
items=useJson(test);
//aitems=useaJson(test2);
//addItemsOnSpinner2(aitems);
addItemsOnSpinner(items);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
} });}
public void addListenerOnSpinner2ItemSelection(){
spinner2 = (Spinner) findViewById(R.id.a_type);
spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//String test2 = parent.getItemAtPosition(position).toString();
//String test = (String) spinner1.getSelectedItem();
String test2 = (String) spinner2.getSelectedItem();
// items=useJson(test);
aitems=useaJson(test2);
addItemsOnSpinner2(aitems);
//addItemsOnSpinner(items);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
} });
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.h_type);
spinner2 = (Spinner) findViewById(R.id.a_type);
spinner3 = (Spinner) findViewById(R.id.player11);
spinner4 = (Spinner) findViewById(R.id.aplayer11);
spinner5 = (Spinner) findViewById(R.id.player12);
spinner6 = (Spinner) findViewById(R.id.aplayer12);
spinner7 = (Spinner) findViewById(R.id.player21);
spinner8 = (Spinner) findViewById(R.id.aplayer21);
spinner9 = (Spinner) findViewById(R.id.player22);
spinner10 = (Spinner) findViewById(R.id.aplayer22);
spinner11 = (Spinner) findViewById(R.id.player31);
spinner12 = (Spinner) findViewById(R.id.aplayer31);
spinner13 = (Spinner) findViewById(R.id.player32);
spinner14 = (Spinner) findViewById(R.id.aplayer32);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(CaptainsInfo.this,
"OnClickListener : " +
"\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
String links_url = "http://192.168.9.59/NPD/detail-info.php?Match_id="+kode;
// Call the FunctionParser to parse the information being returned
// From the FHL databaseList<NameValuePair> params = new ArrayList<NameValuePair>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.FunctionParser(links_url);
// The array is called and the information is returned from
// the database corresponding to each string below
try {
str_login = json.getJSONArray("temp");
String jdwl = "";
TextView isi = (TextView) findViewById(R.id.fixdet);
for(int i = 0; i < str_login.length(); i++){
JSONObject ar = str_login.getJSONObject(i);
Home_team = ar.getString("Home_team");
jdwl += "" + ar.getString("Home_team")+"\n vs \n"+
"" + ar.getString("Away_team")+"\n"+
"" + ar.getString("Fixture_date")+"\n";
}
isi.setText(jdwl);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String[] useJson(String test) {
JSONParser jParser = new JSONParser();
String link_url = "http://10.2.0.0/NPD/P_name.php?"+test;
JSONObject json = jParser.FunctionParser(link_url);
try {
jsonArray = json.getJSONArray("team");
items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i]=jsonObject.getString("name");
}
return(items);}
catch (JSONException e) {
e.printStackTrace();
}
return items;
}
public String[] useaJson(String test2) {
JSONParser jjParser = new JSONParser();
String links_url = "http://10.2.0.0/NPD/P_name1.php?"+test2;
JSONObject json = jjParser.FunctionParser(links_url);
try {
jsonArray = json.getJSONArray("ateam");
aitems = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
aitems[i]=jsonObject.getString("name");
}
return(aitems);}
catch (JSONException e) {
e.printStackTrace();
}
return aitems;
}
}
According to json.org (http://www.json.org/javadoc/org/json/JSONArray.html#getJSONObject(int)):
getJSONObject throws JSONException if there is no value for the index
or if the value is not a JSONObject.
How your ateam array is being populated? Seems that you are adding null values inside it. Make sure that your ateam array is populated only with initialized JSONObject, maybe the JSONObject is null when you add it to ateam array so you must check if is null before adding it to ateam.

Android: Use WebView at child in SimpleExpandableListAdapter

Below is Simple declaration in this a child and parent is being build with dynamic list value.With relevant code as createGroupList() and createChildList(). I am able to manipulate with TextView but one of field is my dynamic image from getting to JSON not is being load in WebView.
expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), // Creating group List.
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.`enter code here`row_name },
createChildList(), // Creating Child List.
R.layout.child_row,
new String[] { "Sub Item0", "Sub Item1", "Sub Item2", "Sub Item3" },
new int[] { R.id.grpChildName, R.id.grpChildAddress, R.id.grpChildPhoneNo, R.id.childImage }
);
setListAdapter(expListAdapter);
private List createGroupList() {
String header = "";
int headerSize = 0;
ArrayList result = new ArrayList();
for (int i = 0; i < dynamicList.size(); i++) {
HashMap m = new HashMap();
String taxiCompanyData[] = dynamicList.get(i);
if (header != taxiCompanyData[3]) {
m.put("Group Item", taxiCompanyData[3]);
header = taxiCompanyData[3];
headerList[headerSize] = header;
headerSize++;
result.add(m);
counter++;
}
}
return (List) result;
}
Above is Group creating and
private List createChildList() {
ArrayList result = new ArrayList();
Log.i("Info ", "headerList.length" + headerList.length);
for (int i = 0; i < headerList.length; i++) {
final ArrayList secList = new ArrayList();
System.out.println("dynamic list size is in main"
+ dynamicList.size());
webView = (WebView) findViewById(R.id.childImage);
for (int j = 0; j < dynamicList.size(); j++) {
String taxiCompanyData[] = dynamicList.get(j);
if (taxiCompanyData[3].equalsIgnoreCase(headerList[i])) {
final HashMap child = new HashMap();
child.put("Sub Item0", taxiCompanyData[0]);
child.put("Sub Item1", taxiCompanyData[1]);
child.put("Sub Item2", taxiCompanyData[2]);
// child.put("Sub Item3", taxiCompanyData[4]);
Thread th = new Thread(){
#Override
public void run() {
webView.loadUrl("https://lh5.googleusercontent.com/-u8heQyD_4y4/T5VMu-Zc6wI/AAAAAAAi71s/EP32a3D-fc0/s90/Corsino");
secList.add(child);
};
};
th.start();
}
}
result.add(secList);
}
return (List)result;
}
I am unable to load WebUI. Please help anyone.

Categories

Resources