2

在我的主要活动中,我调用了一个对象,编写了一个方法 itemClicked()。

public void itemClicked(){

        System.out.println("Clicked");
        efficienadapter.notifyDataSetChanged();
    }

当我从列表视图中选择一行时,我正在尝试打印 Clicked 项目。注意:我正在使用一个名为 EfficientAdapter 的列表适配器。

这是适配器:

     public class EfficientAdapter extends BaseAdapter {



            private Activity activity;
            private ArrayList<ComptePost> data;
            private static LayoutInflater inflater = null;
            public ImageLoader imageLoader; 
            public  Boolean isActusAstuce;
            public static int flag = 0, counter=0;
            NVirementEmmeteur main;
            int num=0;

            ViewHolder holder;
            static String src;

            public EfficientAdapter(Activity a, ArrayList<ComptePost> d) {


                activity = a;
                data = d;
                inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //  imageLoader = new ImageLoader(activity.getApplicationContext());
                imageLoader=new ImageLoader(activity.getApplicationContext());
            }

            public EfficientAdapter(NVirementEmmeteur m) {
                main=m;
            }

            @Override
            public int getCount() {
                return data.toArray().length;

            }

            @Override
            public Object getItem(int position) {

                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            public static class ViewHolder {

                public TextView one;
                public TextView two;
                public TextView three;
                public ImageView image;
                        public RelativeLayout relative;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View vi = convertView;



                if (convertView == null) {

                    vi = inflater.inflate(R.layout.single_item, null);
                    holder = new ViewHolder();
                    holder.one = (TextView) vi.findViewById(R.id.text_view_one);
                    holder.image = (ImageView)vi.findViewById(R.id.imageView1);
                    holder.relative = (Relativelayout)vi.findViewById(R.id.relative);
vi.setTag(holder);

                } else
                    holder = (ViewHolder) vi.getTag();

                    holder.one.setTextColor(Color.WHITE);
                    holder.one.setText("Compte No."+data.get(position).getNumComtpe());
                    holder.image.setVisibility(View.GONE);


                    holder.relative_layout.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

            v.findViewById(R.id.imageView1).setVisibility(View.VISIBLE);

                    main.itemClicked();

                }

            });


                    //flag=0; only first row become white


                return vi;
            }


i used main.itemClicked() to print "Clicked". 


        holder.relative_layout.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    v.findViewById(R.id.imageView1).setVisibility(View.VISIBLE);

                    main.itemClicked();

                }

            });

但是当我选择一个行项目时,我得到了这个错误。

04-10 14:21:43.832: W/dalvikvm(4985): threadid=1: thread exiting with uncaught exception (group=0x40015578)
04-10 14:21:43.847: E/AndroidRuntime(4985): FATAL EXCEPTION: main
04-10 14:21:43.847: E/AndroidRuntime(4985): java.lang.NullPointerException
04-10 14:21:43.847: E/AndroidRuntime(4985):     at .EfficientAdapter$1.onClick(EfficientAdapter.java:111)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at android.view.View.performClick(View.java:2538)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at android.view.View$PerformClick.run(View.java:9152)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at android.os.Handler.handleCallback(Handler.java:587)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at android.os.Looper.loop(Looper.java:130)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at java.lang.reflect.Method.invoke(Method.java:507)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-10 14:21:43.847: E/AndroidRuntime(4985):     at dalvik.system.NativeStart.main(Native Method)

我在哪里弄错了?

4

3 回答 3

1

在这种情况下,您不应该使用 setOnItemClickListener 吗?如果是,请将其放入您的 ListView。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        view.setVisibility(View.VISIBLE);
        MainActivity.itemClicked();
    }
});
于 2016-04-04T20:02:44.903 回答
0

我怀疑这是在这里发生的:

@Override
public void onClick(View v) {
    System.out.println("Clicked "+data.get(position).getDebitMin());
    v.findViewById(R.id.imageView1).setVisibility(View.VISIBLE);
    main.itemClicked();
}

错误发生在第 111 行,这意味着发生了三个选项之一:
1. 数据是null或数据。getpositionnull
2. 您在下一行找到的视图正在返回null
3.主要是null

于 2013-04-10T10:37:25.640 回答
0

这是如何从列表视图中隐藏图像的简单示例。在修改您的代码之后,首先尝试理解此代码。

在此处输入图像描述

我有带有文本和图像的列表视图。

这是列表视图的行文件my_spinner_style.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="2" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

我的活动课:

package com.example.testapp;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    private static final String[] COUNTRIES = new String[] { "Belgium",
            "France", "Italy" };

    private static final Integer[] img = new Integer[] {
            R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher };

    private MyArrayAdapter adapter;
    Bundle savedInstanceState;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        adapter = new MyArrayAdapter(MainActivity.this);
        getListView().setAdapter(adapter);

    }

    private class MyArrayAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public MyArrayAdapter(Context con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return COUNTRIES.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.my_spinner_style, null);
                holder = new ListContent();

                holder.name = (TextView) v.findViewById(R.id.textView1);

                holder.name2 = (ImageView) v.findViewById(R.id.imageView1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.name.setText("" + Html.fromHtml("" + COUNTRIES[position]));

            holder.name2.setImageDrawable(getResources().getDrawable(
                    img[position]));
            holder.name.setOnClickListener(mOnTitleClickListener3);

            return v;
        }

    }

    static class ListContent {

        TextView name;

        ImageView name2;

    }

    public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
        public void onClick(View v) {
            final int position = getListView().getPositionForView(
                    (View) v.getParent());

            View rowView = getListView().getChildAt(position);
            if (rowView != null) { // do whatever you want here
                View mainv = ((ViewGroup) rowView).getChildAt(1);
                Log.e("select dd--->", "" + mainv);

                // hide Image from here....
                mainv.setVisibility(View.GONE);
            }

        }
    };

    protected void onRestart() {
        // adapter.notifyDataSetChanged();
        onCreate(savedInstanceState);
    };

}

我已经补充说你可以使用你OnClickListenerTextViewperent布局。我的情况是LinearLayout

于 2013-04-10T12:09:35.040 回答