android developer
android developer

Reputation: 47

Creating dynamic textview inside the listview in android

I am creating the programmatically TextView inside the ListView items, For it have done some following code.But not getting the desire solution, so please help me out from to solve my problem.

Here is my class.

package com.tv.ravindra;

/**
 * Created by Ravindra  on 22/01/16.
 */
public class DemoClass extends Activity {

    ListView list_demo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);

        list_demo = (ListView) findViewById(R.id.list_demo);

        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("One");
        arrayList.add("Two");
        arrayList.add("Three");
        arrayList.add("Four");

        DemoAdapter adapter = new DemoAdapter(DemoClass.this,arrayList);

        list_demo.setAdapter(adapter);
    }


    public class DemoAdapter extends BaseAdapter
    {

        Activity activity;
        ArrayList<String> arrayList =  new ArrayList<String>();

        public DemoAdapter(Activity activity,ArrayList<String> arrayList)
        {

            this.activity = activity;
            this.arrayList = arrayList;

        }
        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        public class ViewHolder {

            TextView header_text_tv;
            RelativeLayout child_ll;

        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder _viewHolder;
            if (convertView == null) {


                _viewHolder = new ViewHolder();
                LayoutInflater _layInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = _layInflater.inflate(R.layout.demo_item, null);

                _viewHolder.header_text_tv = (TextView)convertView.findViewById(R.id.header_text_tv);
                _viewHolder.child_ll = (RelativeLayout)convertView.findViewById(R.id.child_ll);


                convertView.setTag(_viewHolder);


            } else {
                _viewHolder = (ViewHolder) convertView.getTag();
            }

            _viewHolder.header_text_tv.setText(arrayList.get(position));


            for(int i=0;i<=10;i++)
            {
                TextView msg = new TextView(activity);
                msg.setBackgroundColor(activity.getResources().getColor(R.color.bg_blue_color));
                msg.setText("Number "+i);
                msg.setPadding(10, 10, 10, 10);
                msg.setTextColor(getResources().getColor(R.color.white));
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(10, 10, 10, 10);
                msg.setLayoutParams(params);

                _viewHolder.child_ll.addView(msg);
            }

            return convertView;
        }
      }
    }

From above code I am getting the following output as in Image MY OUTPUT FROM ABOVE CODE

But I want to get the output like these type of output as in the image MY DESIRE THESE

From my output from my above code , all the testview overlay on each other and only last item is visible that is Number10 in my first image.

So friends please help me to short out from these problem.

Upvotes: 2

Views: 1535

Answers (3)

Zilan Pattni
Zilan Pattni

Reputation: 236

in onCreate() method:

List<String> arrayList = new ArrayList<String>();
arrayList.add("One");
arrayList.add("Two");
arrayList.add("Three");
arrayList.add("Four");


ListViewAdapter adapter = new ListViewAdapter(DemoClass.this,arrayList);

list_demo.setAdapter(adapter);

you can use custom adapter(BaseAdapter) to add TextView as per the data in ListView

public class ListViewAdapter extends BaseAdapter {

    Context context;
    TextView contact_number;
    List<String> arrayList;

    DataAdapter(Context c, List<String> arrayList) {
        this.context = c;
    this.arrayList=arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();

    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_row, parent, false);

        textView = (TextView) row.findViewById(R.id.textView);

        textView.setText(arrayList.get(position));

        return row;

    }
}

You also create one layout file for generate custom TextView as single_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"/>

</LinearLayout>

Upvotes: 0

MPG
MPG

Reputation: 795

hi try this way it may help you..

   RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);

    Random rnd = new Random();
    int prevTextViewId = 0;     
    for(int i = 0; i < 10; i++)
    {                       
        final TextView textView = new TextView(this);
        textView.setText("Text "+i);     
        int curTextViewId = prevTextViewId + 1;
        textView.setId(curTextViewId);
        final RelativeLayout.LayoutParams params = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
                                            RelativeLayout.LayoutParams.WRAP_CONTENT);

        params.addRule(RelativeLayout.ALIGN_RIGHT, prevTextViewId);
        textView.setLayoutParams(params);

        prevTextViewId = curTextViewId;
        layout.addView(textView, params);
    }              

Upvotes: 1

Muralikrishna G S
Muralikrishna G S

Reputation: 829

This example code may help you out

public View getView(int position, View view, ViewGroup parent) {

    view = inflateIfRequired(view, position, parent);
    bind(getItem(position), view);

    return view;
}

private void bind(Eddystone eddystone, View view) {

    ViewHolder holder = (ViewHolder) view.getTag();

    String name = StringToHex.convertHexToString(eddystone.instance);

    holder.macTextView.setText("");
    holder.rssiTextView.setText("");
    holder.eddystoneNamespaceTextView.setText("");
    holder.eddystoneInstanceIdTextView.setText("");
}

private View inflateIfRequired(View view, int position, ViewGroup parent) {

    if (view == null) {
        view = inflater.inflate(R.layout.item, null);
        view.setTag(new ViewHolder(view));
    }

    return view;
}

static class ViewHolder {

    final TextView macTextView;
    final TextView rssiTextView;
    final TextView namespace;
    final TextView instanceId;

    ViewHolder(View view) {
        macTextView = (TextView) view.findViewWithTag("mac");
        rssiTextView = (TextView) view.findViewWithTag("rssi");
        namespace = (TextView) view.findViewWithTag("namespace");
        instanceId = (TextView) view.findViewWithTag("instance_id");
    }
}

Upvotes: 0

Related Questions