我现在有问题!我有自定义ListView,我有两个TextView元素。我想在ListView元素中为一个TextView设置setVisibility(TextView.gone)。我不知道怎么做
编写一个示例,说明如何在ListView
的所有项目中隐藏TextView ienter代码的ListView:
<ListView
android:id="@+id/listResultAvia"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#ffffff"
android:cacheColorHint="#ffffff"
android:drawingCacheQuality="auto" >
<!-- Preview: listitem=@layout/item -->
</ListView>
ListVIew的自定义项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff" >
<TextView
android:id="@+id/textViewTimeResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<TextView
android:id="@+id/textViewTimeResult2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"/>
</LinearLayout>
告诉我如何在onCreate中隐藏textViewTimeResult2。 如果我写:
TextView test = (TextView) findById(R.id.textViewTimeResult2);
test.setVisibility(TextView.GONE);
我的程序在开始时崩溃了。
添加
我使用下面的答案,但现在我得到空ListView。我该如何解决这个问题?
答案 0 :(得分:3)
在适配器中,您可以尝试使用此代码隐藏第二个文本视图。
public View getView(int position, View convertView, ViewGroup parent){
convertView = mInflater.inflate(R.layout.your_custom_list_layout, null);
TextView textOne = (TextView)convertView.findViewById(R.id.txtOne);
TextView textTwo = (TextView)convertView.findViewById(R.id.txtTwo);
textTwo.setVisibility(View.GONE);
return convertView;
}
<强>增加:强>
你可以添加一个在onClick()
内切换的布尔值,然后修改getView()
;
boolean hideTextTwo = false;
onClick(View v){
hideTextTwo = true;
yourAdapter.notifyDatasetChanged();
}
public View getView(int position, View convertView, ViewGroup parent){
convertView = mInflater.inflate(R.layout.your_custom_list_layout, null);
TextView textOne = (TextView)convertView.findViewById(R.id.txtOne);
TextView textTwo = (TextView)convertView.findViewById(R.id.txtTwo);
if(hideTextTwo){
textTwo.setVisibility(View.GONE);
}
return convertView;
}
答案 1 :(得分:1)
//在适配器中
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = context.getLayoutInflater();
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_row, null);
holder.first_line=(TextView) convertView.findViewById(R.id.textview_row1);
holder.second_line=(TextView) convertView.findViewById(R.id.second_row);
convertView.setTag(holder);
}
//TODO: hiding the first_line in the listview
holder.first_line.setVisibility(View.GONE);
holder.second_line.setText("");
return (convertView);
}