[英]Finding out the parent of clicked element in android ListView
我正在基於http://javatechig.com/video/json-feed-reader-in-android中的資料構建我的第一個應用程序。 到目前為止一切正常,但是我發現ListView元素存在一個錯誤,我自己無法解決:(我將list_row_layout.xml擴展了2個字段:
<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個元素),則存在錯誤。 單擊元素中的注釋將加載到ListView中的第8個元素中。 一些代碼:
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代碼與教程中的代碼基本相同。
我非常感謝您的幫助,因為我花了很多時間來解決問題,但沒有成功:(
這只是一個猜測。 如果這不起作用,您也可以發布適配器代碼和parseJsonComments
。
原因:
您描述的問題可能是由於視圖的回收和重復使用引起的。 看看來自http://android.amberfog.com的這張圖片
如您所見,項目1.被重新使用,並且在滾動后成為項目8.。
假設Item 1具有一個OnClickListener
,它會更新該Item的Text。 例如,在觸發OnClickListener
之后,我們將文本設置為“ clicked”。
因為項目1被重新使用來創建項目8,項目8也將顯示文本“單擊”。
解決方案:
通常的方法是將所有狀態/內容保存在List(或其他任何內容)中,並更新getView調用中的所有內容。 因此,如果要更新文本:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
...
holder.textView.setText(jsonTexts[position]);
...
return convertView;
}
如果要更新項目,只需更新適配器中包含內容/ JsonObjects(etc。)的列表,然后調用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();
}
在填充列表視圖后,我調用此方法:
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.lv);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
String xx = position+ ":" + id;
//then you can do what ever you want
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.