0

I want to create a Listview that will hold 1 to 'n' nos of TextView data horizontally, this Textview data and width size will come from DB? I don't know how to set the width of TextView dynamically with holder?

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    ViewHolder holder  = new ViewHolder();

    if (view == null) {
        view = View.inflate(cntxts, R.layout.list_header_col, null);
        String [] ColNmae= (String[]) objects.get(0);

        int Width= Integer.parseInt(ColNmae[2].toString());
        LinearLayout layout=new LinearLayout(cntxts);
       // layout.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        TextView Textview = new TextView(cntxts);
        HorizontalScrollView hr = new HorizontalScrollView(cntxts);
        for(int i=0;i<objects.size();i++) {
            holder.Textview=new TextView(cntxts);
            layout.setLayoutParams(new RelativeLayout.LayoutParams(Width,80));
            layout.addView(holder.Textview);
        }
        hr.addView(layout);
        view = hr;
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    notifyDataSetChanged();
    final String[] objMain = (String[]) getItem(position);
        holder.Textview.setText(objMain[0]);
    return view;
}
Arnaud
  • 6,339
  • 8
  • 47
  • 64
BAIJU SHARMA
  • 227
  • 3
  • 6
  • 1
    You would have to use an Adapter for a list. Its basic Android101. Have a look at: http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter – zIronManBox Jan 19 '16 at 12:57
  • I am using adapter, I want in Listview Row as ....Col1,Col2, Col3,Col4......and its data in below row. – BAIJU SHARMA Jan 19 '16 at 14:05

3 Answers3

1
Thanks for your help I have got the solution.

 try {
                LayoutInflater mInflater = (LayoutInflater)
                        cntxts.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = mInflater.inflate(R.layout.list_header_col, null);

                holder.layoutmain = (LinearLayout) view.findViewById(R.id.lyt_header);

                view.setTag(holder);

                String[] ColNmae = (String[]) objects.get(0);

                int Width = Integer.parseInt(ColNmae[2].toString());
                if (holder.layoutmain != null && holder.layoutmain.getChildCount() > 0)
                    holder.layoutmain.removeAllViews();

                for (int i = 0; i < objects.size(); i++) {
                    final String[] objMain = (String[]) getItem(i);
                    TextView textView = new TextView(cntxts);
                    textView.setText(objMain[0]);
                    holder.layoutmain.addView(textView);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
BAIJU SHARMA
  • 227
  • 3
  • 6
0

Your data needs to come from somewhere. To fulfill a Spinner you have the following options:

  • Put the values in the layout file
  • Put in the code
    • Building your own values
    • Retrieving it from the DB
Victor Viola
  • 481
  • 1
  • 4
  • 13
0

try this,

 yourholder.textView.getLayoutParams().width = dynamic width value;