如何插入TextView的动态列表视图 [英] how to insert textView dynamically in list view
问题描述
其实在我的应用我想在列表视图中插入TextView的动态,但我该怎么办它。我不要有一个idea..i我送我的code,其中我有使用TextView的,我想在插入列表视图dynamically..can有人请建议me..if可能的例子。
code是:
公共无效displayHistory()
{ INT iHistCount = CycleManager.getSingletonObject()getHistoryCount()。
串strDisplay;
INT I;
布尔bStarred = FALSE; SimpleDateFormat的sdFormatter =新的SimpleDateFormat(DD-MMMM-YYYY); 对于(i = 0; I< iHistCount;我++)
{
strDisplay =; 日期dtHist = CycleManager.getSingletonObject()getHistoryDate(ⅰ)。 strDisplay = sdFormatter.format(dtHist.getTime()); 的LinearLayout layoutVertical =(的LinearLayout)findViewById(R.id.liVLayoutHist1); 的LayoutParams参数=新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1); LinearLayout中的RowLayout =新的LinearLayout(本); layoutVertical.addView(的RowLayout,参数); TextView的tTextHistory1 =新的TextView(本);
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,参数); strDisplay =; 如果(我== 0)
如果(CycleManager.getSingletonObject()。isCycleStopped())
strDisplay =停止;
其他
strDisplay =进行中;
其他
{
日期dtNextHist = CycleManager.getSingletonObject()getHistoryDate(I-1)。
长lDiff = dtNextHist.getTime() - dtHist.getTime();
lDiff = lDiff /(1000 * 60 * 60 * 24); strDisplay = Long.toString(lDiff); 如果(lDiff< DeclareVariable.CYCLE_MIN_LENGTH || lDiff> DeclareVariable.CYCLE_MAX_LENGTH)
{
strDisplay = strDisplay +*;
bStarred = TRUE;
}
} TextView的tTextHistory2 =新的TextView(本);
tTextHistory2.setTextColor(Color.parseColor(#000000));
tTextHistory2.setPadding(15,0,0,0);
tTextHistory2.setText(strDisplay);
rowLayout.addView(tTextHistory2,参数); }
strDisplay =;
如果(bStarred)
strDisplay =*比接受更短或更长;
其他
strDisplay =; TextView的tTextHistory3 =(的TextView)findViewById(R.id.txtViewHeading);
tTextHistory3.setTextColor(Color.parseColor(#000000));
tTextHistory3.setPadding(15,0,0,0);
tTextHistory3.setText(strDisplay);
}
我不是100%肯定,如果我理解你的要求,但如果你想从一个数据源添加的东西,你需要添加一个ListAdapter,这使得该列表从数据源填充。要做到这一点,最好的办法是用吹气。下面是一些code,我使用我的Android应用程序从一个URL获取数据并填充它。让我知道如果你需要更多的澄清!
下面是一个教程的链接,以及: http://www.vogella.de /articles/AndroidListView/article.html
公共类CheckinList扩展ListActivity { 私人LayoutInflater mInflater;
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
最终的ListView列表= getListView();
mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list.setAdapter(新ArrayAdapter< CheckinItem>(这一点,R.layout.checkin_item,main.Crunch){
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排;
如果(空== convertView){
行= mInflater.inflate(R.layout.checkin_item,NULL);
}其他{
排= convertView;
}
TextView的名字=(TextView中)row.findViewById(R.id.name);
name.setText(的getItem(位置).getVenueName());
TextView的时间=(TextView中)row.findViewById(R.id.time);
time.setText(的getItem(位置).getTime()的toString());
TextView的地址=(TextView中)row.findViewById(R.id.address);
address.setText(的getItem(位置).getAddress());
TextView的crossStreet =(TextView中)row.findViewById(R.id.crossStreet);
如果(的getItem(位置).getCrossStreet()!=){
address.append(,);
crossStreet.setText(的getItem(位置).getCrossStreet());
}
返回行;
}
});
}}
Actually in my app i want to insert textview in the listview dynamically but how can i do it.i dont have an idea..i am sending my code where i have use textView which i want to insert in the listview dynamically..can anyone please suggest me..if possible with examples..
Code Is:
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;
}
});
}
}
这篇关于如何插入TextView的动态列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!