Skip to content Skip to sidebar Skip to footer

How To Add Textview Dynamically To Listview

Actually, in my app I want to insert TextView in the ListView dynamically but how can I do it? I don't have any idea. I am sending my code where I use textView which I want to inse

Solution 1:

I am not 100% sure if I understand what you are asking, but if you want to add things from a datasource you need to add a ListAdapter, which allows the list to populate from a datasource. The best way to do this is with an inflater. Below is some code I am using for my Android app that fetches data from a URL and populates it. Let me know if you need more clarification!

Here's a link to a tutorial as well: http://www.vogella.de/articles/AndroidListView/article.html

publicclassCheckinListextendsListActivity {

    private LayoutInflater mInflater;
    publicvoidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
            finalListViewlist= getListView();
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            list.setAdapter(newArrayAdapter<CheckinItem>(this, R.layout.checkin_item, main.Crunch) {
                  @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
                      View row;
                      if (null == convertView) {
                            row = mInflater.inflate(R.layout.checkin_item, null);
                        } else {
                            row = convertView;
                        }
                      TextViewname= (TextView) row.findViewById(R.id.name);
                      name.setText(getItem(position).getVenueName());
                      TextViewtime= (TextView) row.findViewById(R.id.time);
                      time.setText(getItem(position).getTime().toString());
                      TextViewaddress= (TextView) row.findViewById(R.id.address);
                      address.setText(getItem(position).getAddress());
                      TextViewcrossStreet= (TextView) row.findViewById(R.id.crossStreet);
                      if(getItem(position).getCrossStreet() != ""){
                          address.append(", ");
                          crossStreet.setText(getItem(position).getCrossStreet());
                      }
                      return row;
                  }
              });
    }

}

Post a Comment for "How To Add Textview Dynamically To Listview"