ListView performitemClick Nullpointer

时间:2014-08-18 17:36:02

标签: android listview android-listview

我有一个列表视图,它会为包含按钮的行布局充气。我试图在用户点击时为按钮添加功能,但是我还没有找到办法。在用户首先关注listview项之前,按钮不具有功能。换句话说,用户必须单击该按钮两次才能获得应用程序的任何响应。我已经尝试过请求关注我在列表视图中设置的ArrayAdapter屏幕上项目的位置。但是,这总是会导致空指针。我的目标是请求关注用户在屏幕上看到的项目,以便每当用户决定点击按钮时,按钮都可以具有功能。这是导致空指针

的代码
final View view=inflater.inflate(R.layout.main_feed_layout, container, false);


    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    lv = (ListView)view.findViewById(R.id.listView);



    // on seleting single product
    // launching Edit Product Screen


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.i("Check", "Focused");

            usernameTV = (TextView) view.findViewById(R.id.postUnameTv);
            usernameTV.setOnClickListener(listener);

        }
    });`

在异步任务结束时,我将适配器设置为列表视图。然后我尝试执行项目单击,在尝试查找usernameTV时给出空指针。

  lv.performItemClick(lv.getChildAt(0), 0, lv.getChildAt(0).getId());

为什么当我以编程方式单击该项而不是空指针时,这是一个空指针,当我手动单击该项时?

修改 这是适配器代码

 private class MyArrayAdapter extends ArrayAdapter<String> {


    ViewHolder viewHolder;
    private final Context context;
    private List<String> posts;
    private List<String> viewContent;
    int index = 0;

    public MyArrayAdapter(Context context, List<String> strings, List<String> content) {
        super(context, R.layout.post_layout, strings);
        this.context = context;
        this.posts = strings;
        this.viewContent = content;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        viewHolder = new ViewHolder();

        if(convertView == null) {

            try {

                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.post_layout, parent, false);


                viewHolder.unameTV = (TextView) convertView.findViewById(R.id.postUnameTv);
                viewHolder.unameTV.setText(viewContent.get(index));


                viewHolder.image = (ImageView) convertView.findViewById(R.id.postProfPic);
                DisplayImageOptions options = initiateDisplayImageOptions();
               // viewHolder.image.setOnClickListener(listener);

                ImageLoader imageloader = ImageLoader.getInstance();

                initImageLoader(getActivity());
                imageloader.displayImage(viewContent.get(index + 1), viewHolder.image, options);


                viewHolder.addToCalendarButton = (TextView) convertView.findViewById(R.id.addToCalendarButton);
                viewHolder.addToCalendarButton.setText(viewContent.get(index + 2));

                viewHolder.eventTitle = (TextView) convertView.findViewById(R.id.postTitleTV);
                viewHolder.eventTitle.setText(viewContent.get(index + 3));

                viewHolder.eventImage = (ImageView) convertView.findViewById(R.id.eventImage);
                imageloader.displayImage(viewContent.get(index + 4), viewHolder.eventImage, options);


                viewHolder.likesTV = (TextView) convertView.findViewById(R.id.likesTV);
                viewHolder.likesTV.setText("" + viewContent.get(index + 5));

                viewHolder.planToAttendTV = (TextView) convertView.findViewById(R.id.planToAttendTV);
                viewHolder.planToAttendTV.setText(viewContent.get(index + 6));

                viewHolder.addressTV = (TextView) convertView.findViewById(R.id.postLocationTV);
                viewHolder.addressTV.setText("" + viewContent.get(index + 7));


                index = index + 8;
            }
            catch (IndexOutOfBoundsException ie)
            {
                ie.printStackTrace();
            }
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }




        return convertView;
    }


    @Override
    public int getCount()
    {
        return posts.size();
    }

    @Override
    public String getItem(int position) {
        return posts.get(position);
    }

     class ViewHolder {
         ImageView image;
         TextView addToCalendarButton;
         TextView eventTitle;
         ImageView eventImage;
         TextView likesTV;
         TextView planToAttendTV;
         TextView addressTV;
         TextView unameTV;

     }

}

这是布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:background="@drawable/fill_back"
>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="400dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/relativeLayout"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/postUnameTv"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp"
        android:gravity="center|center_vertical|center_horizontal"
        android:textColor="#ff518eff"
        android:textSize="12dp"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/postProfPic"
        android:text="Joshua" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Public"
        android:id="@+id/postProfileIcon"
        android:background="@drawable/publicicon"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="25dp"
        android:layout_marginRight="70dp"
        />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/postProfPic"
        android:layout_below="@+id/postUnameTv"
        android:layout_centerHorizontal="true"
        />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="330dp"
        android:id="@+id/eventImage"
        android:layout_alignTop="@+id/space"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <Space
        android:layout_width="20px"
        android:layout_height="20px"
        android:layout_alignBottom="@+id/postProfPic"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:id="@+id/space" />

    <Space
        android:layout_width="20px"
        android:layout_height="20px"
        android:layout_above="@+id/postProfPic"
        android:layout_centerHorizontal="true"
        android:id="@+id/space2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/postTitleTV"
        android:layout_below="@+id/postProfPic"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="117dp"
        android:gravity="center|center_vertical|center_horizontal"
        android:textSize="28dp"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton3"
        android:background="@drawable/details_button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="10dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/postLocationTV"
        android:layout_below="@+id/postProfPic"
        android:layout_alignParentStart="true"
        android:drawableLeft="@drawable/locate_button"
        android:drawablePadding="5dp"
        android:gravity="clip_horizontal"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:textSize="12dp"
        android:singleLine="true"
        android:layout_alignEnd="@+id/space"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/addToCalendarButton"
        android:textSize="12dp"
        android:layout_alignBottom="@+id/postLocationTV"
        android:drawableLeft="@drawable/add_to_cal_button"
        android:drawablePadding="5dp"
        android:gravity="bottom"
        android:layout_alignEnd="@+id/imageButton3"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="2 hours ago"
        android:id="@+id/textView2"
        android:textSize="8dp"
        android:gravity="center_vertical|center_horizontal"
        android:paddingRight="10dp"
        android:textColor="#ff828084"
        android:layout_above="@+id/eventImage"
        android:layout_toRightOf="@+id/postProfPic"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="3dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/posthelpSpace"
        android:layout_alignBottom="@+id/space"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/postProfPic"
        android:layout_alignTop="@+id/postProfPic" />

</RelativeLayout>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/likeButton"
    android:background="@drawable/like_button_unsel"
    android:layout_below="@+id/relativeLayout"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="15dp"
    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:id="@+id/likesTV"
    android:textColor="#ffe11100"

    android:layout_alignBottom="@+id/likeButton"
    android:layout_toRightOf="@+id/likeButton"
    android:layout_alignTop="@+id/likeButton"
    android:gravity="center|center_vertical|center_horizontal"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    />

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="0.3dp"
    android:id="@+id/imageView"
    android:layout_below="@+id/likeButton"
    android:layout_alignParentStart="true"
    android:layout_marginTop="100dp"
    android:background="@drawable/divider" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:id="@+id/planToAttendTV"
    android:layout_below="@+id/likesTV"
    android:paddingLeft="5dp"
    android:layout_marginTop="10dp"
    android:layout_alignParentEnd="true"
    android:gravity="center"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:text="Yes"
    android:id="@+id/button"
    android:textSize="10dp"
    android:layout_toRightOf="@+id/textView3"
    android:background="@drawable/border_circular"
    android:layout_marginLeft="2dp"
    android:layout_below="@+id/planToAttendTV"
    android:layout_marginTop="5dp"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:text="No"
    android:id="@+id/button2"
    android:textSize="10dp"
    android:layout_alignTop="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:background="@drawable/border_circular"
    android:layout_marginLeft="8dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Are you attending?"
    android:id="@+id/textView3"
    android:gravity="center"
    android:layout_alignBottom="@+id/button"
    android:layout_toRightOf="@+id/likeButton"
    android:layout_marginBottom="7dp"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="4dp"
    />

为了阐明视图的行为,如果我在适配器中添加onclicklistener,结果将不是动态的。这是一个例子

//Inside adapter
                viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String t = viewHolder.unameTV.getText().toString();
                        System.out.println(t);
                    }
                });

系统将打印出加载到列表视图中的最后一个视图中的文本,而不是打印的实际列表视图项。

1 个答案:

答案 0 :(得分:1)

setOnClickListener内容放回适配器并将onClick方法更改为:

public void onClick(View view) {
    String t = ((TextView)view).getText().toString();
    System.out.println(t);
}

这应该打印单击的“按钮”的文本。实际上,您可以只设置一个监听器并将其应用于所有列表项,它仍然可以正常工作。