繁体   English   中英

在Android ListView中找到单击元素的父级

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM