2012-02-03 86 views
0

我有一個顯示客戶信息的活動。所有控件都是動態繪製的,因爲屏幕的設計由中央SQL數據庫控制。 (IE屏幕位置,可編輯和強制),因此設計沒有XML。動態添加ListView只顯示1個項目

到目前爲止,我所做的一切工作得很好,直到我嘗試添加一個ListView對象來列出客戶通信數據。 (IE的電話號碼,電子郵件,傳真號碼或網站URL列表)

此列表視圖添加正確,第一行數據顯示,但我似乎無法得到任何行顯示。

將項目添加到表格行中,然後將這些項目添加到表格佈局中。這可能是問題嗎?但是,當添加ListView項目時,首先將它添加到RelativeLayout項目中,這樣應該允許顯示多行代碼?

任何想法,爲什麼這可能發生?我檢查了我的數組的值,並且我肯定有多個項目。

以下是活動onCreate方法:

@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<Integer>(); 
    addedCodeIndexes = new ArrayList<Integer>(); 
    addedDateIndexes = new ArrayList<Integer>(); 
    addedTextIndexes = new ArrayList<Integer>(); 
    addedListIndexes = new ArrayList<Integer>(); 
    mandatoryIndexes = new ArrayList<Integer>(); 

    // for each control in the array 
    for(int i=0;i<=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); 
} 

而現在handleField方法決定如何處理領域的IM將做到:

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 > 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 > 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<?> 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); 
      }    
     } 
    } 
} 

的ListView項是在第二加使用以下代碼代碼塊:

// list view 
ListView lv = new ListView(this); 
lv.setId(id + 10000000); 
lv.setTag(field); 
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> 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); 

最後列表視圖填充用下面的代碼:

} 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<Communication> communicationAdapter = new ArrayAdapter<Communication>(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 { 

我曾做過一個試驗,並在我的應用程序的頂部增加了一個列表視圖和listadapter設置爲我的數組,並顯示所有行。

所以看起來像它與錶行

回答

1

不要把ListView控件在滾動型。

+2

因爲我是新來的stackoverflow,我不能投票的答案;但我想讓人們知道解決方案的工作原理。對不起,重複的答案! – TheMan 2013-04-09 20:04:00