Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I insert a column in listview at runtime?

I want to make a listview looks like datagrid control in android.All columns are generated by code-behand which is dynamic.My code snippets could build successfully but the listview does not display as I expected.What is wrong with it?

    @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;
        }
like image 643
Whistler Avatar asked Apr 09 '13 08:04

Whistler


1 Answers

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

Example code: http://androidadapternotifiydatasetchanged.blogspot.in/

like image 66
Alka Jadav Avatar answered Nov 15 '22 01:11

Alka Jadav