1

I have a custom adapter set for a Listview but my itemClickEvent is not working . I have included my adapter inside my main activity.I have tried many suggestions but havent got any solutions till yet.

public class MainActivity extends Activity {

private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     Model.LoadModel();
        listView = (ListView) findViewById(R.id.list);
        String[] ids = new String[Model.Items.size()];
        for (int i= 0; i < ids.length; i++){

            ids[i] = Integer.toString(i+1);
        }

        GraphListAdapter adapter = new GraphListAdapter (this,R.layout.list_row, ids);
        listView.setAdapter(adapter);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class GraphListAdapter extends BaseAdapter {

     private final Context context;
        private final String[] Ids;
        private final int rowResourceId;

        public GraphListAdapter(Context context, int textViewResourceId, String[] objects) {

            super();

            this.context = context;
            this.Ids = objects;
            this.rowResourceId = textViewResourceId;

        }

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

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(rowResourceId, parent, false);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
            TextView textView = (TextView) rowView.findViewById(R.id.dashboard_name);
           TextView textView2 = (TextView) rowView.findViewById(R.id.dashboard_info);

            int id = Integer.parseInt(Ids[position]);
            String imageFile = Model.GetbyId(id).imageName;

            textView.setText(Model.GetbyId(id).dashboardName);
            textView2.setText(Model.GetbyId(id).information);
            // get input stream
            InputStream ims = null;
            try {
                ims = context.getAssets().open(imageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            imageView.setImageDrawable(d);
            return rowView;

        }




        private OnItemClickListener clickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {
                // TODO Auto-generated method stub
                System.out.println(pos);

            }
        };

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

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

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


 }

My list row contains image and text both...................

4

5 回答 5

2

listView.setOnItemClickListener在你的内部使用onCreate()

GraphListAdapter adapter = new GraphListAdapter (this,R.layout.list_row, ids);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }
        });
于 2013-08-07T19:15:53.643 回答
1

尝试使用

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> l, View v, int position,
                long id) {
    });

在 onCreate ...希望这将解决您的问题... :)

于 2013-08-07T19:15:46.337 回答
0

listView.setOnItemClickListener(clickListener); 在 Oncreate 中调用它

于 2013-11-13T09:05:15.027 回答
0

clickListener没有依附于任何东西。将其移入OnCreate并使用listView.setOnItemClickListener.

于 2013-08-07T19:15:40.507 回答
0

您需要在 listView 上设置 clickListener。

例如:

datumSpinner = (Spinner) findViewById(R.id.spinner1);
datumSpinner.setOnItemSelectedListener(this);
于 2013-08-07T19:21:47.810 回答