gpt4 book ai didi

android - 如何动态添加TextView到ListView

转载 作者:行者123 更新时间:2023-11-29 14:06:40 29 4
gpt4 key购买 nike

实际上,在我的应用程序中,我想在 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);
}

最佳答案

我不是 100% 确定我是否理解您的要求,但是如果您想从数据源添加内容,则需要添加一个 ListAdapter,它允许从数据源填充列表。最好的方法是使用充气机。下面是我用于我的 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;
}
});
}

}

关于android - 如何动态添加TextView到ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6584906/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com