如何在列表视图中动态插入textView

时间:2011-07-05 15:09:19

标签: android

实际上在我的应用程序中我想动态地在listview中插入textview但是我怎么能这样做。我没有想法..我发送我的代码我在哪里使用textView我想动态插入listview。 。任何人都可以建议我..如果可能的例子......

代码是:

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);
}

1 个答案:

答案 0 :(得分:0)

如果我理解你的要求,我不是100%确定,但是如果你想从数据源添加东西,你需要添加一个ListAdapter,它允许列表从数据源填充。最好的方法是使用inflater。下面是我用于Android应用程序的一些代码,它从URL获取数据并填充它。如果您需要更多说明,请告诉我们!

以下是教程的链接: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;
                  }
              });
    }

}