Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Added ListView only displays 1 item
    text
    copied!<p>I have an activity that shows customer information. All the controls are drawn dynamically as the design for the screen is controlled by a central SQL database. (IE screen position, editable and mandatory) so there is no XML for the design.</p> <p>Now so far everything I have done has worked very well until I came to try adding a ListView object to list the customers communication data. (IE a list of phone numbers, emails, fax numbers or website URLs)</p> <p>This List view is added correctly and the first row of data is displayed but I cannot seem to get the anymore rows to display.</p> <p>The items are added into a table row and these are then added onto a table layout. Could this be the problem? However when adding the ListView item this is first added to a RelativeLayout item so this should allow more than one line to be displayed?!</p> <p>Any Ideas why this could be occurring? I have checked the values of my array and I definitely have more than one item in it.</p> <p>The following is the Activities onCreate method:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); model = SharedModel.getInstance(); dbHelper = new DatabaseHelper(this); LinearLayout baseLayout = new LinearLayout(this); baseLayout.setOrientation(LinearLayout.VERTICAL); // add the customer name and number field. // NOTE: We will always require this widget regardless of dialog design fields tvCustomerNameNumber = new TextView(this); tvCustomerNameNumber.setTextSize(20); baseLayout.addView(tvCustomerNameNumber); // buttons RelativeLayout.LayoutParams alignBotton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); LinearLayout buttons = new LinearLayout(this); buttons.setOrientation(LinearLayout.HORIZONTAL); alignBotton.addRule(RelativeLayout.ALIGN_BOTTOM, baseLayout.getId()); // button edit Button edit = new Button(this); edit.setId(EDIT_BUTTON_ID); edit.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), "buttonEdit", 1001000)); edit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setButtonsEnabled(false, true, true); setControlsEnabled(true); } }); buttons.addView(edit); // button save Button save = new Button(this); save.setId(SAVE_BUTTON_ID); save.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), "buttonSave", 1001000)); save.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { saveCustomer(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); buttons.addView(save); // button cancel Button cancel = new Button(this); cancel.setId(CANCEL_BUTTON_ID); cancel.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), "buttonCancel", 1001000)); cancel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { cancelChanges(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); buttons.addView(cancel); // add the buttons baseLayout.addView(buttons); // build up the table of controls TableLayout tl = new TableLayout(this); // get the dialog design json array for customer information dialogFields = model.getDialogDesignFields(); // reset the field type indexes addedIndexes = new ArrayList&lt;Integer&gt;(); addedCodeIndexes = new ArrayList&lt;Integer&gt;(); addedDateIndexes = new ArrayList&lt;Integer&gt;(); addedTextIndexes = new ArrayList&lt;Integer&gt;(); addedListIndexes = new ArrayList&lt;Integer&gt;(); mandatoryIndexes = new ArrayList&lt;Integer&gt;(); // for each control in the array for(int i=0;i&lt;=model.getDialogDesignFields().length() - 1; i++) { try { // if the control is for the customer page if(dialogFields.getJSONObject(i).get("formname").equals("CustomerInformation")){ handleField(tl, dialogFields.getJSONObject(i)); } } catch (JSONException e) { e.printStackTrace(); } } // Scroll view. // NOTE: We will always need this widget to enable us to scroll the page // if too many items are added for the display size ScrollView sv = new ScrollView(this); sv.addView(tl); baseLayout.addView(sv); // set the content view this.setContentView(baseLayout); // make sure we are not in edit mode setButtonsEnabled(true, false, false); setControlsEnabled(false); setResult(0); } </code></pre> <p>And now the handleField method that decides what to do with the field im adding:</p> <pre><code>private void handleField(TableLayout tl, final JSONObject field) throws JSONException { // get field index int id = field.getInt("viewindex") + 1; final String name = field.getString("name"); // if the name = CustomerAddressID don't do anything if(name.equals("CustomerAddressID") != true) { // if the name is not equal to CustomerCommunicationID if(name.equals("CustomerCommunicationID") == false) { if(id &gt; 0) { // create and add controls // add the field index to the addedIndexes.add(id); // layout parameters RelativeLayout.LayoutParams alignRight = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); // if the field is mandatory if(field.getInt("mandatory") == 1) { TextView mandatory = new TextView(this); mandatory.setText("*"); tl.addView(mandatory); mandatoryIndexes.add(id); } // field name description TextView fn = new TextView(this); fn.setId(id + 1000); fn.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), name, 1000030)); tl.addView(fn); // create a table row TableRow tr = new TableRow(this); tr.setId(id + 10000); tl.addView(tr); // create a relative layout RelativeLayout rl = new RelativeLayout(this); rl.setId(id + 100000); rl.setTag(field); rl.setBackgroundResource(R.drawable.myborder); // if the control is not disabled if(field.getInt("disabled") == 0) { // so we are adding a listener // if the name is code we are adding to code indexes if(name.endsWith("CODE") == true | name.equals("PreferredVisitDay")) { addedCodeIndexes.add(id); } else if (name.endsWith("DATE") == true) { addedDateIndexes.add(id); } else { addedTextIndexes.add(id); } // add a listener rl.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // show the code picker for customer group JSONObject field = (JSONObject) v.getTag(); try { prepareDialog(field); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // image view ImageView iv = new ImageView(this); iv.setId(id + 1000000); iv.setImageResource(R.drawable.edit16x16); iv.setVisibility(View.GONE); alignRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, iv.getId()); rl.addView(iv, alignRight); } tl.addView(rl); // fieldValue TextView fv = new TextView(this); fv.setId(id + 10000000); fv.setTextSize(18); rl.addView(fv); } } else { if(id &gt; 0) { addedIndexes.add(id); // field name description TextView fn = new TextView(this); fn.setId(id + 1000); fn.setText(dbHelper.getActivityString(dbHelper.getWritableDatabase(), model.getUserLang().getLanguage().GetCodeId(), name, 1000030)); tl.addView(fn); // create a table row TableRow tr = new TableRow(this); tr.setId(id + 10000); tl.addView(tr); // create a relative layout RelativeLayout rl = new RelativeLayout(this); rl.setId(id + 100000); rl.setTag(field); rl.setBackgroundResource(R.drawable.myborder); tl.addView(rl); // list view ListView lv = new ListView(this); lv.setId(id + 10000000); lv.setTag(field); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int position, long id) { try { communicationClicked(arg0, view, position, id, field); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); rl.addView(lv); } } } } </code></pre> <p>The ListView Item is added in the second block of code with the following code:</p> <pre><code>// list view ListView lv = new ListView(this); lv.setId(id + 10000000); lv.setTag(field); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int position, long id) { try { communicationClicked(arg0, view, position, id, field); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); rl.addView(lv); </code></pre> <p>Finally the List view is populated with the following code:</p> <pre><code>} else if (name.equals("CustomerCommunicationID")){ // set communications value ListView lv = (ListView)findViewById(index + 10000000); // create an array adapter with the customer type data array ArrayAdapter&lt;Communication&gt; communicationAdapter = new ArrayAdapter&lt;Communication&gt;(this, R.layout.communication_menu_item, currentCustomer.getCustomerCommunications()); // assign the my customers list control the array adapter lv.setAdapter(communicationAdapter); Log.i(TAG, "Communications Assigned"); } else { </code></pre> <p>I have done a test and added a Listview at the top of my application and set the listadapter to my array and all the lines are displayed.</p> <p>So looks like Its to do with the Table rows</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload