1

I can open the app but I am not able to click on the image .. what wrong I am doing ?

the below is the adapter

adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList.setOnItemClickListener(new OnItemClickListener() {         
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Send intent to SingleViewActivity
           Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
           // Pass image index
           i.putExtra("id", position);
           startActivity(i);
           } });

this is the activity main

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dip" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Latest" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"

        android:background="#B29090" >
    </ListView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:gravity="center_vertical"
        android:text="Recomanded" />

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:background="#4A9C67" >
    </ListView>
</LinearLayout>

this is ContactImageAdapter1 class

package com.example.sqlfirst;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactImageAdapter1 extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter1(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact picture = data.get(position);
holder.txtTitle.setText(picture ._name);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
Moudiz
  • 7,211
  • 22
  • 78
  • 156

4 Answers4

1

I suppose that you have somewhere the ContactImageAdapter1 class. You have to attach an View.OnClickListener at the getView method, after you load your ImageView from the list item layout.

So, your getView method have to be something like this :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View row = convertView;
  ImageHolder holder = null;
  if(row == null)
  {
   LayoutInflater inflater = ((Activity)context).getLayoutInflater();
   row = inflater.inflate(layoutResourceId, parent, false);
   holder = new ImageHolder();
   holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
   holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
   // lets attach the click listener!
   holder.imgIcon.setOnClickListener(new OnClickListener() { 
      @Override
      public void onClick(View view) {
       Intent i = new Intent(((Activity)context), SingleViewActivity.class);  // or whatever method you have to get the context
       // Pass image index
       i.putExtra("id", position);
       startActivity(i);
      }
   });
   row.setTag(holder);
  }else
  {
    holder = (ImageHolder)row.getTag();
  }
  Contact picture = data.get(position);
  holder.txtTitle.setText(picture ._name);
  //convert byte to bitmap take from contact class
  byte[] outImage=picture._image;
  ByteArrayInputStream imageStream = new    
  ByteArrayInputStream(outImage);
  Bitmap theImage = BitmapFactory.decodeStream(imageStream);
  holder.imgIcon.setImageBitmap(theImage);
  return row;
}
karvoynistas
  • 1,295
  • 1
  • 14
  • 30
  • yes I have getview in the adapter .. where should I put it ? – Moudiz Jun 08 '15 at 18:27
  • like this is corretct ? Intent i = new Intent(ContactImageAdapter1 .getview(), SingleViewActivity.class); – Moudiz Jun 08 '15 at 18:28
  • No, this is totally wrong. If you want post your code of getView method. The listener will be attached inside the getView body at the ImageView. If you want to open the intent only when the ImageView will be clicked , you have to remove the OnItemClickListener from details and let the other ImageView listener to do the job – karvoynistas Jun 08 '15 at 18:29
  • ok ill post ContactImageAdapter1 adapter , i was stuck on this all day I am not able to set the onclicklistner – Moudiz Jun 08 '15 at 18:31
  • I am having this erro The method setOnClickListenerOnClickListener() is undefined for the type ImageView .. by the way i am not using imageview in the xml , i am using listview – Moudiz Jun 08 '15 at 18:48
  • Okay ill check it but in my xml i am not calling imageview .. i am just calling a list .. is that correct ? – Moudiz Jun 08 '15 at 18:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79997/discussion-between-karvoynistas-and-moudiz). – karvoynistas Jun 08 '15 at 22:22
  • [this](http://stackoverflow.com/questions/30739336/setonitemclicklistener-doenst-work-when-add-a-button) solved my problem – Moudiz Jun 10 '15 at 08:47
0

go into the root of your custom Layout (screen_list1) and add this:

android:descendantFocusability = "blocksDescendants"

It would be probably your problem.

Francis Ngueukam
  • 994
  • 1
  • 10
  • 28
0

Try android.support.v7.internal.widget.AdapterViewCompat.OnItemClickListener implementation if you are using Activity/Fragment from appcompat library.

JavaGhost
  • 406
  • 4
  • 8
0

In your code you have made ImageHolder class a static class inside the Adapter class this thus forces DVM to create only one copy for all the items in the ListView hence the problem

Abdul Rahman K
  • 664
  • 5
  • 16