我正在根據http://javatechig.com/video/json-feed-reader-in-android的素材構建我的第一個應用程序。 一切正常的話,到目前爲止,但我發現一個錯誤與ListView的元素,這是我不能管理由我自己來解決:( 我已經2場擴展list_row_layout.xml:在Android ListView中查找單擊元素的父項
<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代碼大部分與教程中的代碼相同。
我真的很感激幫助,因爲我花了很多時間與沒有成功:(計算出來
@AndreuRodrígueziDonaire:「點擊元素的評論被加載到ListView中的每個第8個元素中。」 –