Jawed Warsi
Jawed Warsi

Reputation: 1

Click Imageview and reply textview within a ListView ListItem and get the position in android

  1. I want click event replyTextview and HeartImageview
  2. Get Position each click of listview
  3. go to from arrayadapter to other activity using intent

enter image description here

4.Deaclare listview in mainactivity.java

 @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Toast.makeText(mContext, "Postion no." +position, Toast.LENGTH_LONG).show();


        }

This is array Adapter class

    public class ActorAdapter extends ArrayAdapter<Actors> implements OnClickListener {
        ArrayList<Actors> actorList;
        LayoutInflater vi;
        int Resource;
        ViewHolder holder;
        int count=0;

        public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
            super(context, resource, objects);
            vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            Resource = resource;
            actorList = objects;

        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // convert view = design
            View v = convertView;
            if (v == null) {
                holder = new ViewHolder();
                v = vi.inflate(Resource, null);
                holder.ivheart = (ImageView) v.findViewById(R.id.iv_likeheart);
                holder.tvadd=(TextView)v.findViewById(R.id.tv_add);
                holder.tvreply=(TextView)v.findViewById(R.id.tv_reply);
                holder.tvreplyadd=(TextView)v.findViewById(R.id.tv_replyadd);
                holder.imageview = (ImageView) v.findViewById(R.id.iv_userpic);
                holder.tvName = (TextView) v.findViewById(R.id.tv_username);
                holder.tvDOB = (TextView) v.findViewById(R.id.tv_title);
                holder.tvCountry = (TextView) v.findViewById(R.id.tv_description);
                holder.ivheart.setOnClickListener(this);
                holder.tvreply.setOnClickListener(this);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.imageview.setImageResource(R.drawable.ic_launcher);
            new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
            holder.tvName.setText(actorList.get(position).getName());
            holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
            holder.tvCountry.setText(actorList.get(position).getCountry());
            return v;

        }

        static class ViewHolder {
            public ImageView imageview;
            public TextView tvName;
            public TextView tvDOB;
            public TextView tvCountry;
            public ImageView ivheart;
            public TextView tvadd;
            public TextView tvreply;
            public TextView tvreplyadd;



        }



    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.iv_likeheart:

            Toast.makeText(getContext(), "Hello", Toast.LENGTH_LONG).show();
            break;
         case R.id.tv_reply:

            //not working settext here
            //holder.tvadd.setText(count++);
            // Toast.makeText(getContext(), "Hello" +count++, Toast.LENGTH_LONG).show();
             //Intent i=new Intent(ActorAdapter.this, ReplyActivity.class);

            break;

        default:
            break;
        }

    }
}

Upvotes: 0

Views: 243

Answers (2)

cristiangoncas
cristiangoncas

Reputation: 76

Jawed, you are implementing the OnClickListener but you should use OnItemClickListener.

See the example code below:

final ListView list = (ListView) findViewById(R.id.list);

    ArrayList<String> arrayList = new ArrayList<String>();
    String[] strings = new String[] {"A", "B", "C", "D", "E"};
    for (int i=0; i<strings.length; i++) {
        arrayList.add(i, strings[i]);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(MainActivity.this, DetailActivity.class);
            Object item = list.getItemAtPosition(i);
            intent.putExtra("title", item.toString());
            startActivity(intent);
        }
    });

This code are working.

Upvotes: 0

Sanwal Singh
Sanwal Singh

Reputation: 1793

Try this:- Instead using list View’s onItemClick use convert-view on-click in adapter.

View v = convertView;

v.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    String id = actorList.get(position).getID();
                    String name = actorList.get(position).getName();
                    // Do what you want to do there. 
                }
            });

Hope this will help you.

Upvotes: 1

Related Questions