2013-04-09 45 views
6

Tôi muốn làm cho một listview trông giống như kiểm soát datagrid trong android.All cột được tạo ra bởi code-behand đó là đoạn mã dynamic.My có thể xây dựng thành công nhưng listview không hiển thị như tôi mong đợi.Điều gì là sai với nó?Tôi có thể chèn một cột trong listview vào thời gian chạy không?

@Override 
     public View getView(int position, View convertView, ViewGroup parentView) { 
      ViewHolder holder = null; 
      if (convertView == null) { 
       synchronized (MainActivity.this) { 
        convertView = mInflater.inflate(id_row_layout, null); 
        holder = new ViewHolder(); 

//I had add an textView to the convertView,but it not show 

        LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.rLayout); 
        TextView tx = new TextView(context); 
        tx.setText("ads"); 
        layout.addView(tx); 

        MyHScrollView scrollView1 = (MyHScrollView) convertView 
          .findViewById(R.id.horizontalScrollView1); 

        holder.scrollView = scrollView1; 
        holder.txt1 = (TextView) convertView 
          .findViewById(R.id.textView1); 
        holder.txt2 = (TextView) convertView 
          .findViewById(R.id.textView2); 
        holder.txt3 = (TextView) convertView 
          .findViewById(R.id.textView3); 
        holder.txt4 = (TextView) convertView 
          .findViewById(R.id.textView4); 
        holder.txt5 = (TextView) convertView 
          .findViewById(R.id.textView5); 

        MyHScrollView headSrcrollView = (MyHScrollView) mHead 
          .findViewById(R.id.horizontalScrollView1); 
        headSrcrollView 
          .AddOnScrollChangedListener(new OnScrollChangedListenerImp(
            scrollView1)); 

        convertView.setTag(holder); 
        mHolderList.add(holder); 
       } 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.txt1.setText(position + "" + 1); 
      holder.txt2.setText(position + "" + 2); 
      holder.txt3.setText(position + "" + 3); 
      holder.txt4.setText(position + "" + 4); 
      holder.txt5.setText(position + "" + 5); 

      return convertView; 
     } 
+0

cho tất cả tôi biết, nếu chúng ta không thể thêm xem trong xml động, nó không thể – Senthil

+0

nhờ trả lời của bạn, tôi muốn tải dữ liệu để hiển thị như một datagrid.i đã thử giải pháp tablelayout, nhưng nó hoạt động để slow.i phải sử dụng listview nhưng bảng sqlite của tôi là thế hệ bởi objects.so bất kỳ giải pháp khác cho điều này? – Whistler

Trả lời

0

Làm việc cho tôi trong ApiDemos với văn bản bổ sungXem;

public class List14 extends ListActivity { 

    private static class EfficientAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 
     private Bitmap mIcon1; 
     private Bitmap mIcon2; 
     private Context context; 

     public EfficientAdapter(Context context) { 
      // Cache the LayoutInflate to avoid asking for a new one each time. 
      mInflater = LayoutInflater.from(context); 
this.context = context; 
      // Icons bound to the rows. 
      mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1); 
      mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2); 
     } 

     /** 
     * The number of items in the list is determined by the number of speeches 
     * in our array. 
     * 
     * @see android.widget.ListAdapter#getCount() 
     */ 
     public int getCount() { 
      return DATA.length; 
     } 

     /** 
     * Since the data comes from an array, just returning the index is 
     * sufficent to get at the data. If we were using a more complex data 
     * structure, we would return whatever object represents one row in the 
     * list. 
     * 
     * @see android.widget.ListAdapter#getItem(int) 
     */ 
     public Object getItem(int position) { 
      return position; 
     } 

     /** 
     * Use the array index as a unique id. 
     * 
     * @see android.widget.ListAdapter#getItemId(int) 
     */ 
     public long getItemId(int position) { 
      return position; 
     } 

     /** 
     * Make a view to hold each row. 
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View, 
     *  android.view.ViewGroup) 
     */ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // A ViewHolder keeps references to children views to avoid unneccessary calls 
      // to findViewById() on each row. 
      ViewHolder holder; 

      // When convertView is not null, we can reuse it directly, there is no need 
      // to reinflate it. We only inflate a new View when the convertView supplied 
      // by ListView is null. 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.list_item_icon_text, null); 

       // Creates a ViewHolder and store references to the two children views 
       // we want to bind data to. 
       holder = new ViewHolder(); 
       holder.text = (TextView) convertView.findViewById(R.id.text); 
       holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
       LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.layout); 
       TextView child = new TextView(context); 
       child.setText("CHILD"); 
       layout.addView(child); 

       convertView.setTag(holder); 
      } else { 
       // Get the ViewHolder back to get fast access to the TextView 
       // and the ImageView. 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      // Bind the data efficiently with the holder. 
      holder.text.setText(DATA[position]); 
      holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 

      return convertView; 
     } 

     static class ViewHolder { 
      TextView text; 
      ImageView icon; 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new EfficientAdapter(this)); 
    } 

    private static final String[] DATA = { 
      "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", 
      }; 
} 

enter image description here

Kiểm tra tĩnh của bạn holderlayouts.

0

Thật dễ dàng để có thêm cột, chỉ hiển thị và ẩn phần tử cho vị trí đã chọn. Bạn phải ẩn chúng khi phần tử không được hiển thị.

Trong thực tế, bạn cũng có thể thổi phồng RelativeLayout từ bộ điều hợp của bạn, nhưng điều này có thể sẽ khó đạt được hơn.

Hy vọng rằng sẽ giúp

0

NẾU bố trí tập tin Row của bạn sẽ được tương tự cho mỗi hàng sau đó

bạn chỉ có thể thêm số mặt hàng (nói cách khác cột của bạn)

đến số ArrayList (có trách nhiệm điền vào listviewadapter) và chỉ cần gọi notifydatasetchanged(true) hoặc adapter.notifyDataSetChanged()

đang
2
1) In your adapter with Arraylist override the appropriate constructor 
2) In your activity, make your variable data a field (type ArrayList 
3) When you add a location you can use data.add(location) 
4) Then you can call notifyDatasetChanged() on your adapter 

Ví dụ: http://androidadapternotifiydatasetchanged.blogspot.in/

Các vấn đề liên quan