找出点击的元素的android系统中的ListView父 [英] Finding out the parent of clicked element in android ListView
问题描述
我建设我根据材料第一应用从的http:// javatechig .COM /视频/ JSON-提要阅读器功能于Android的。
一切正常的话,到目前为止,但我发现一个错误的ListView与元素,这是我不能管理由我自己来解决:(
我已经2个字段扩展list_row_layout.xml:
I'm building my first app based on material from http://javatechig.com/video/json-feed-reader-in-android. Everything goes ok so far, but I found one bug with ListView elements, which I can not manage to resolve by myself :( I have extended list_row_layout.xml by 2 fields:
<Button
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="komcie"
android:textSize="11sp"
android:id="@+id/loadComments"
android:layout_gravity="center|bottom"
android:background="#bbb"
android:layout_marginLeft="5dp"
android:enabled="true"
android:clickable="true"
android:onClick="clickedLoadComments"
android:elegantTextHeight="true"
android:layout_toRightOf="@id/thumbImage"
android:layout_below="@+id/content"
android:padding="1px" />
<ListView
android:id="@+id/comment_list"
android:layout_toRightOf="@id/thumbImage"
android:layout_below="@+id/content"
android:paddingTop="5dp"
android:layout_marginTop="0dp"
android:paddingLeft="5dp"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:cacheColorHint="#00000000"
android:dividerHeight="1dp"
android:focusable="false"
android:listSelector="@drawable/list_selector_flatcolor"
android:visibility="invisible" />
Button.android:onClick=clickedLoadComments功能加载的Json与元素融入的ListView / comment_list。它的工作原理相当精细。但是,如果有比屏幕上(〜8元)有一个bug可以显示更多的元素。 从点击的元素评论加载到在ListView每8个元素。
有些code:
Button.android:onClick="clickedLoadComments" function load Json with elements into ListView/comment_list. It works quite fine. But if there are more elements than could be displayed on screen (~8 elements) there is a bug. Comments from clicked element are loaded into every 8th element in a ListView. Some code:
public void clickedLoadComments(View v)
{
try {
View parent = (View)v.getParent();
ViewHolder t = (ViewHolder) parent.getTag();
if( parent != null ) {
this.loadCommentsForLeaf(parent);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
protected void loadCommentsForLeaf( View view )
{
String tmpUrl = "http://some.url.com/Ajax/LoadComments?lid=" + this.currentLeafInUse;
JSONObject commentsJson = this.getJSONFromUrl(tmpUrl);
this.parseJsonComments(commentsJson);
if( commentsJson != null )
this.updateCommentList(view);
}
public void updateCommentList( View view) {
commentListView = (ListView) view.findViewById(R.id.comment_list);
commentListView.setVisibility(View.VISIBLE);
CommentListAdapter cla = new CommentListAdapter(this, this.commentList.get(this.currentLeafInUse));
commentListView.setAdapter(cla);
// Set list height.
ViewGroup.LayoutParams params = commentListView.getLayoutParams();
params.height = setListViewHeightBasedOnItems(commentListView) + 20;
commentListView.setLayoutParams(params);
commentListView.requestLayout();
}
CustomListAdapter.java code是大多相同,一个在教程。
CustomListAdapter.java code is mostly the same as the one in tutorial.
我真的AP preciate帮助,因为我花了很多时间想出来与不是成功:(
I would really appreciate help as I have spent many hours figuring it out with not success :(
推荐答案
这只是一个猜测。你可以发表您的适配器code和你的 parseJsonComments
此外,如果这是行不通的。
This is just a guess. You might post your Adapter code and your parseJsonComments
also if this does not work.
的原因:
您所描述的,由于回收和意见的回用可能造成的问题。就拿从 http://android.amberfog.com
The problem you are describing might be caused due to the recycling and the reusage of Views. Take a look at this image from http://android.amberfog.com
正如你所看到的1项是重复使用,滚动后变成了8项。
As you can see the 1. items is reused and becomes the 8. item after scrolling.
让我们假设第1项具有 OnClickListener
这将更新项目的文本。
例如我们设置为点击文本后 OnClickListener
被触发。
Let's assume that Item 1 has an OnClickListener
which updates a Text of the item.
For example we set the text to "clicked" after the OnClickListener
is triggered.
由于第1项被重新创造8项,第8项也将显示文本点击。
Because item 1 is reused to create item 8, item 8 will also display the text "clicked".
解决方案:
的常用方法是保存列表中的所有国家/内容(或其他),并更新getView调用一切。所以,如果你想更新文本:
The usual way is to save all states/content in a List(or whatever) and update everything in the getView call. So if you want to update text:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
...
holder.textView.setText(jsonTexts[position]);
...
return convertView;
}
如果你想更新项目刚刚更新您的适配器持有的内容/一个JSONObjects列表(等),并调用 notifyDataSetChanged
。
public void updateCommentList(JSONObject commentsJson, int position) {
// does not exist you might create something
//like that in your Adapter class
commentListAdapter.updateItem(commentsJson,position);
commentListAdapter.notifyDataSetChanged();
}
这篇关于找出点击的元素的android系统中的ListView父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!