2015-08-31 38 views
1

我正在根據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代碼大部分與教程中的代碼相同。

我真的很感激幫助,因爲我花了很多時間與沒有成功:(計算出來

+0

@AndreuRodrígueziDonaire:「點擊元素的評論被加載到ListView中的每個第8個元素中。」 –

回答

2

這只是一個猜測。你可能會發表您的適配器代碼和parseJsonComments此外,如果這行不通

的原因:

您所描述的問題可能是由於視圖的回收和重用造成的。看看這個形象從http://android.amberfog.com

enter image description here

正如你所看到的1項被重用和滾動後變成了8項。

讓我們假設項目1有一個OnClickListener它更新項目的文本。 例如,我們在觸發OnClickListener之後將文本設置爲「單擊」。

由於項目1被重新用於創建項目8,項目8還將顯示文本「點擊」。

解決方案:

的常用方法是保存在一個列表(或其他)所有國家/內容並更新getView調用一切。所以,如果你想更新文字:

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(); 
} 
+0

所以我想。你必須重新整個列表,不能只改變一個元素。非常感謝! –

+0

也可以更新一個視圖。我提供的答案只是一個簡單而乾淨的解決方案。 – FlanschiFox

+0

只更新一個視圖。您必須確保更新正確的視圖,並且必須確保在getView方法中正確重新創建視圖。 – FlanschiFox

0

後我填充ListView我調用這個方法:

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 

     } 

    }); 
} 
相關問題