我在listview中输入了大约20列。但我希望用户可以选择禁用(隐藏)视图中的某些列。我发现我可以制作自己的阵列适配器并查看行。但我不想将其定义为一行中的2到20个项目。有什么选择吗?
我找到了一种方法。例如,我在listview中有这样的项目布局。我将制作自己的适配器,然后只需调用setVisibility(View.GONE);我将从listview中隐藏此项目。PS:这是realcode中唯一的例子,应该有一些IF()来隐藏列。
项的布局。
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/description"
android:layout_alignBottom="@+id/description"
android:layout_toRightOf="@+id/description"
android:text="TextView" />
和MyAdapter。
private class ConnectedAdapter extends ArrayAdapter<ConnectionClass> {
private ArrayList<ConnectionClass> connectionList;
public ConnectedAdapter(Context context, int textViewResourceId,
ArrayList<ConnectionClass> countryList) {
super(context, textViewResourceId, countryList);
this.connectionList = new ArrayList<ConnectionClass>();
this.connectionList.addAll(countryList);
}
private class ViewHolder {
TextView description;
TextView value;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.activity_connected_item,
null);
holder = new ViewHolder();
holder.description = (TextView) convertView
.findViewById(R.id.description);
holder.value = (TextView) convertView
.findViewById(R.id.value);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ConnectionClass connection = connectedList.get(position);
holder.description.setText(connection.getDescription());
holder.description.setVisibility(View.GONE);
holder.value.setText(connection.getValue());
return convertView;
}
}