如何创建动态ListView和TextView中的可见性设置 [英] how to create dynamic listview and set visibility of Textview
问题描述
我是新来的Android的发展。我成功地创建一个示范项目与动态列表视图,但现在我的问题是,我在我的 list_row
XML文件中的两个textviews。
Textview2是默认隐藏(能见度=飘)。
现在我设定的TextView只在列表视图第五的位置可见的知名度。我写了code表示,但随机TextView中显示出来,而不仅仅是在第五的位置。我有100条记录中的列表视图。
我在做什么错了?
编辑:
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
// TODO自动生成方法存根
ViewHolder持有人; 如果(convertView == NULL)
{
convertView = mLayoutInflater.inflate(R.layout.list_row,NULL);
持有人=新ViewHolder();
holder.text1 =(TextView中)convertView.findViewById(R.id.mytext);
holder.text2 =(TextView中)convertView.findViewById(R.id.invisibletext);
convertView.setTag(保持器);
如果(位置== 5)
holder.text2.setVisibility(View.VISIBLE);
其他
holder.text2.setVisibility(View.GONE);
}
其他
{
支架=(ViewHolder)convertView.getTag();
} 返回convertView;
}静态类ViewHolder
{
TextView的文本1;
TextView的文本2;
}
如果您convertView不等于零时,如果其他condtion不火,所以当convertView为null,则它的火所以它的工作原理随机。
更新您的code按如下...
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根
ViewHolder持有人; 如果(convertView == NULL){
convertView = mLayoutInflater.inflate(R.layout.list_row,NULL); 持有人=新ViewHolder(); convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
holder.text1 =(TextView中)convertView.findViewById(R.id.mytext);
holder.text2 =(TextView中)convertView.findViewById(R.id.invisibletext); 如果(位置== 5){
holder.text2.setVisibility(View.VISIBLE); }其他{
holder.text2.setVisibility(View.GONE);
} 返回convertView; }}
I'm new to Android development. I successfully created a demo project with a dynamic listview but now my problem is that I have two textviews in my list_row
xml file.
Textview2 is default hide(Visibility=Gone).
Now I set the visibility of that textview visible only in fifth position in listview. I wrote code for that but the textview shows up randomly, not just in the fifth position. I have 100 records in the listview.
What am I doing wrong?
EDIT :
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null)
{
convertView = mLayoutInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.mytext);
holder.text2 = (TextView) convertView.findViewById(R.id.invisibletext);
convertView.setTag(holder);
if (position == 5)
holder.text2.setVisibility(View.VISIBLE);
else
holder.text2.setVisibility(View.GONE);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder
{
TextView text1;
TextView text2;
}
if your convertView is not null then if-else condtion not fire, So when convertView is null then its fire so it works randomly.
Update your code as per below...
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1 = (TextView) convertView.findViewById(R.id.mytext);
holder.text2 = (TextView) convertView.findViewById(R.id.invisibletext);
if (position == 5) {
holder.text2.setVisibility(View.VISIBLE);
} else {
holder.text2.setVisibility(View.GONE);
}
return convertView;
}
}
这篇关于如何创建动态ListView和TextView中的可见性设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!