简体   繁体   中英

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 insert in the ListView dynamically. Can anyone please suggest it to me? If possible with examples.

My current code:

public void displayHistory()
{

        int iHistCount = CycleManager.getSingletonObject().getHistoryCount();
        String strDisplay;
        int i;
        boolean bStarred = false;

        SimpleDateFormat sdFormatter = new SimpleDateFormat("dd-MMMM-yyyy");

        for (i=0; i<iHistCount; i++)
        {
                strDisplay = "";

                Date dtHist = CycleManager.getSingletonObject().getHistoryDate(i);

                strDisplay=sdFormatter.format(dtHist.getTime());

                LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayoutHist1);

                LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);

                LinearLayout rowLayout=new LinearLayout(this) ;

                layoutVertical.addView(rowLayout,param);

                TextView tTextHistory1=new TextView(this);
                tTextHistory1.setTextColor(Color.parseColor("#000000"));
                tTextHistory1.setPadding(15, 0, 0, 0);
                tTextHistory1.setText(strDisplay);

                rowLayout.setBackgroundResource(R.drawable.history_scroll_label_bg);
                rowLayout.addView(tTextHistory1,param);

                strDisplay=" ";

                if (i == 0)
                       if (CycleManager.getSingletonObject().isCycleStopped())
                               strDisplay = "Stopped";
                       else
                               strDisplay = "In progress";
                else
                {
                         Date dtNextHist = CycleManager.getSingletonObject().getHistoryDate(i-1);
                         long lDiff =dtNextHist.getTime()-dtHist.getTime();
                         lDiff=lDiff/(1000 * 60 * 60 * 24);

                         strDisplay=Long.toString(lDiff);

                         if (lDiff<DeclareVariable.CYCLE_MIN_LENGTH || lDiff>DeclareVariable.CYCLE_MAX_LENGTH)
                         {
                                 strDisplay=strDisplay+"*";
                                 bStarred = true;
                         }
                 }

                TextView tTextHistory2=new TextView(this);
                tTextHistory2.setTextColor(Color.parseColor("#000000"));
                tTextHistory2.setPadding(15, 0, 0, 0);
                tTextHistory2.setText(strDisplay);
                rowLayout.addView(tTextHistory2,param);  

        }
        strDisplay=" ";
        if (bStarred)
                strDisplay="* Shorter or longer than accepted";
        else
                strDisplay=" ";

        TextView tTextHistory3=(TextView) findViewById(R.id.txtViewHeading);
        tTextHistory3.setTextColor(Color.parseColor("#000000"));
        tTextHistory3.setPadding(15, 0, 0, 0);
        tTextHistory3.setText(strDisplay);
}

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

public class CheckinList extends ListActivity {

    private LayoutInflater mInflater;
    public void onCreate(Bundle savedInstanceState) {

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM