如何根据一个条件修改列表视图的元素
我有一个使用简单适配器的列表视图。基本上每个元素都由一个图像、1 个字符串字段和一个评级栏组成。
ListView元素的布局位于文件element.xml中 相反,列表视图位于 main.xml 内。
字符串字段的值可以是数字、单词 FREE 或单词 INSTALLED。
在 element.xml 布局中靠近此字段的地方有一个标签,其中 ID = Price 且文本值 =“应用程序价格”
这是我在显示列表时得到的结果: 「申请价格」4 “申请价格”免费 《申请价格》7 “应用程序价格”已安装 “应用程序价格”免费
基本上我想做的是检查字符串字段的值,如果它等于已安装,则标签价格必须变为不可见才能获得此结果:
“应用程序价格”4 “申请价格”免费 《申请价格》7 已安装 “应用程序价格”免费
你知道怎么做吗......我红色,扩展我的简单适配器的 getview 方法可能是正确的,但我不知道如何......尚不清楚我知道这个方法是如何工作的以及他的参数代表什么......你能帮助我或建议我正确的做法吗?
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView costo = (TextView) view.findViewById(R.id.costo);
TextView prezzo = (TextView) view.findViewById(R.id.appPrezzo);
if (prezzo.getText().equals("Installed"))
{
costo.setVisibility(View.INVISIBLE);
//view = inflater.inflate(R.layout.elem_applist, null);
}
else
{
costo.setVisibility(View.VISIBLE);
}
return view;
我是这样做的......我不知道为什么我不能评论或投票给你的答案Blessemn......无论如何它有效,但我没有使用充气机......没有它,希望也是正确的:)
i have a listview that use a simple adapter. Basically each element consist of an image, 1 string field and a ratingbar.
ListView element's layout is in the file element.xml
instead the list view is inside main.xml
The value of the string field can be a number, the word FREE, or the word INSTALLED.
Just close to this field in the element.xml layout there is a label with ID = Price and text value = "Application price"
This is what i get now when i display my list:
"Application price" 4
"Application price" FREE
"Application price" 7
"Application price" INSTALLED
"Application price" FREE
Basically what i would like to do is to check the value of the string field and if it is equal to INSTALLEd the label Price has to be become Invisible to get this result:
"Application price" 4
"Application price" FREE
"Application price" 7
INSTALLED
"Application price" FREE
Do you know how to do that......i red that could be right to extend the method getview of my simple adapter, but i don't know how.....it's not clear to me the how this method works and what his parameters stand for.....can you help me or suggest me the right thing to do?
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView costo = (TextView) view.findViewById(R.id.costo);
TextView prezzo = (TextView) view.findViewById(R.id.appPrezzo);
if (prezzo.getText().equals("Installed"))
{
costo.setVisibility(View.INVISIBLE);
//view = inflater.inflate(R.layout.elem_applist, null);
}
else
{
costo.setVisibility(View.VISIBLE);
}
return view;
I did it in this way......i don't know why i can'y comment or vote for your answer Blessemn.....anyway it works, but i didn't use an inflater....Hope is correct also whithout it :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 getview 方法中调用返回代表每一行的视图。即使您滚动列表,它也会被多次调用。您获得的参数
位置 ->行号。
转换视图->代表行的视图。一开始它是空的。但一旦被渲染
它不会为空。
父级->父视图
在 getview 方法中,您必须返回一个视图来表示该行。
这是正常的 getView 方法的样子
这里我们只是使用 inflater 获取一个视图并在视图中找到 textview。
我们检查条件并相应地设置文本并返回视图。
那里有更好的教程。您应该搜索自定义基础适配器。
如果它适合您,那么使用它就没有问题。只要您返回有效的视图,该列表就可以工作。 Inflater 仅用于从 xml 文件创建视图。
但请注意帕雷什的回答。正如您所看到的,他使用静态类来保存对行内部子级的引用。当列表首次呈现时, getview 方法仅被调用 n 次,其中 n 是当前可见行数(不准确)。假设 5 行一次可见,适配器会重用现有视图并仅更新其中的值。
因此,在他的代码中,他检查 ConvertView 是否为空,如果是,他会膨胀一个新行,否则他会使用具有更新值的现有行。我建议您观看来自 Google IO 的视频
In the getview method is called to return the view representing each row. It is called multiple time even when you scroll the list. The arguments you get
position -> The row number.
convertview -> the view representing the row. At beginning its null. But once is rendered
it wont be null.
parent -> the parent view
In the getview method, you MUST return a view to represent the row.
Heres how a normal getView method looks like
Here we just get a view using the inflater and find the textview inside the view.
We check for a condition and set the text accordingly and return the view.
There are way better tutorials out there. You should search for custom base adapter.
If it works for you then there is no problem in using it. As long as you return a valid view the list will work. Inflater is just used to create a view from an xml file.
But do notice paresh's answer. As you can see he uses a static class to hold references to a rows internal childs. When the list is first renderedm the getview method is only called the n times where n is the number of visible rows at the moment(not exact). Suppose the 5 rows are visible at a time, the adapter reuses an existing view and just updates the values in them.
So in his code he check it the convertview is null, if it is he inflates a new row otherwise he uses an existing row with updated values. I recomend you watch this video from Google IO
尝试下面的代码来定义自定义列表视图。
例如:
Try the below code to define custom listview.
For example: