2012-10-03 90 views
0

我正在創建一個列表視圖,其中每一行都有兩個部分,左邊是一個圖像視圖,右邊部分包含一些文本字段。但該字段應該動態加載..意味着值從0到9會動態地出現。我很困惑如何使用正確的部分。我想創建一個在Java編碼..不是.. XML創建我的XML這樣..`在運行時在列表中添加行內容

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="2dp" 
    android:background="@android:color/white" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:layout_weight=".88" 
     android:background="#D8D8D8" 
     android:clickable="true" 
     android:gravity="center" 
     android:text="image" 
     android:textColor="@android:color/black" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight=".15" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" /> 
     </LinearLayout> 

</LinearLayout> 

`enter image description here

我的適配器類是這樣的:

public class SimpleAdapter extends ArrayAdapter<Simple> { 
     TextView mImage; 
     LinearLayout layout_VER; 
     LinearLayout layout_hor; 
     private ArrayList<Simple> dd; 
     Activity context = null; 
     Intent myIntent; 

     public SimpleAdapter(Activity context, int textViewResourceId, ArrayList<Simple> objects) { 
      super(context, textViewResourceId, objects); 
      this.dd = objects; 
      this.context = context; 
     } 

     @Override 
     public Simple getItem(int position) { 
      // TODO Auto-generated method stub 
      return dd.get(position); 
     } 

     @Override 
     public int getCount() { 

      return dd.size(); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = convertView; 
      pos = position; 

      if (view == null) { 
       LayoutInflater inflater = context.getLayoutInflater(); 
       view = inflater.inflate(R.layout.row, null); 
      } 
      view.setTag(pos); 
      mImage = (ImageView) view.findViewById(R.id.image); 
      if (dd != null) { 
       Simple simple = dd.get(position); 

      } 

      layout_VER=(LinearLayout)view.findViewById(R.id.dyn_main); 
      layout_VER.setTag(pos); 
      showDEtails(view, layout_VER, 10); 
      return view; 
     } 

     private void showDEtails(View view, LinearLayout layout_VER2, int Obj) { 
      View myView=view; 
      LinearLayout ver_layout=layout_VER2; 
      Log.i("showAppointments0000000000","vr_layout:::"+ver_layout.getTag()); 
      LinearLayout hr_layout1 = (LinearLayout)myView.findViewById(R.id.dyn_new); 
      hr_layout1.setTag(ver_layout.getTag()+","+0); 
      LinearLayout hr_layout2 =null; 
      for (int i = 0; i < Obj i++) { 
       if (i > 4) { 
        if (Obj % 5 == 0) { 
         int mp=Obj % 5; 
         hr_layout2 = new LinearLayout(context); 
         hr_layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
         hr_layout2.setOrientation(LinearLayout.HORIZONTAL); 
         hr_layout2.setTag(ver_layout.getTag()+","+mp); 
        }  
        TextView text = new TextView(context); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
        layoutParams.setMargins(2, 2, 2, 2); 
        text.setLayoutParams(layoutParams); 
        text.setPadding(3, 3, 3, 3); 
        text.setClickable(true); 
        hr_layout2.addView(text);    
       }else{ 
        TextView text = new TextView(context); 
        text.setText("Value of j is : " + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
        layoutParams.setMargins(2, 2, 2, 2); 
        text.setLayoutParams(layoutParams); 
        hr_layout1.addView(text); 
        text.setClickable(true); 
        text.setPadding(3, 3, 3, 3); 
       } 
      } 
     } 

我的o/p是這樣的enter image description here

最新問題。

回答

0

通過java動態創建佈局實用代碼。

請檢查鏈接。它可以幫助您使用視圖創建佈局。

Link

Link

感謝。 祝你好運。

+0

我已經檢查了那些。那些是簡單的行。檢查我更新的帖子 – mainu

0

如果你的右側部分在田靜佈局和純文本將改變,然後創建與佈局的單獨的XML,然後膨脹並與ListView控件適配器您在getView值()填充它。這樣的事情:

public View getView(int position, View v, ViewGroup parent) { 
     LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = layoutInflater.inflate(mTextViewResourceId, null); 
     TextView text = (TextView) v.findViewById(R.id.field1); 
     text.setText(mItems.get(position).field1text); 
     return v; 
} 
+0

不,我的權利部分是全部動態..檢查我更新的帖子.. – mainu