简体   繁体   中英

Custom ListView item row not clickable

This is the activity for the listview. But the rows of listview are not clickable.I have written onItemClickListener on the custom listview but it is not working .Please help

this is change.class

public class change extends Activity {
private ListView actlist;
private String database_name,table_name;
private  String DB_Path;
private  ArrayList<String>Steps,Audio_path;
private ArrayList<Bitmap>Image_path;
private SQLiteDatabase db;
private Cursor c;
private int count;
private CustomListAdapter customListAdapter;
private Bitmap bitmap;
private File f1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change);
    actlist=(ListView)findViewById(R.id.actlist);
    table_name=getIntent().getStringExtra("Value");
    database_name=getIntent().getStringExtra("Head");
    Steps=new ArrayList<String>();

    Image_path=new ArrayList<Bitmap>();
    Audio_path=new ArrayList<String>();

    //Steps.clear();
//  Image_path.clear();
    //Audio_path.clear();




    if(database_name.equals("Kitchen"))
    {
        DB_Path="/mnt/sdcard/Kitchen_Data.db";
        Steps=new ArrayList<String>();
        db=SQLiteDatabase.openDatabase(DB_Path, null,SQLiteDatabase.OPEN_READWRITE);
         c=db.query(table_name, null, null, null, null, null, null);
         c.moveToFirst();
         count = c.getCount();
            for(int i=1;i<=count;i++)
             {
                  Steps.add(c.getString(1));
                  Image_path.add(ImgBitmap(c.getString(2)));
                 Audio_path.add(c.getString(3));
                    if(c.isAfterLast()==false)
                    {
                            c.moveToNext();
                    }
             }

    }
else
    {
        DB_Path="/mnt/sdcard/Washroom_Data.db";

        db=SQLiteDatabase.openDatabase(DB_Path, null,SQLiteDatabase.OPEN_READWRITE);
         c=db.query(table_name, null, null, null, null, null, null);
         c.moveToFirst();
         count = c.getCount();
            for(int i=1;i<=count;i++)
             {
                  Steps.add(c.getString(1));
                 Bitmap b2=(ImgBitmap(c.getString(2)));
                 Image_path.add(b2);
                 Audio_path.add(c.getString(3));
                    if(c.isAfterLast()==false)
                    {
                            c.moveToNext();
                    }
             }


    }
    customListAdapter = new CustomListAdapter(this, Steps,Image_path,Audio_path);
    actlist.setAdapter(customListAdapter);
    actlist.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "u clicked", Toast.LENGTH_LONG).show();
        }
    });
}


private   Bitmap ImgBitmap(String filename)
{
    f1=new File(filename);
    if(f1.exists())
    {

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize=8;
         bitmap=BitmapFactory.decodeFile(f1.getAbsolutePath(), options);
        return bitmap;
    }
    return null;
}
}

This is CustomListAdapter.class

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> data;
private ArrayList<Bitmap> image_bit;
private  ArrayList<String> audio_bit;
private MediaPlayer player;

private static LayoutInflater layoutInflater;

public CustomListAdapter(Activity acivity, ArrayList<String> data,ArrayList<Bitmap>image_bit,ArrayList<String>audio) {
    // TODO Auto-generated constructor stub
    this.activity = acivity;
    this.data = data;
    this.image_bit=image_bit;
    this.audio_bit=audio;
    layoutInflater = (LayoutInflater) acivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@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( final int position, View convertView, ViewGroup parent) {

    ListCell listCell;
    if(convertView == null)
    {
        convertView = layoutInflater.inflate(R.layout.listact_kitchen, null);
        listCell = new ListCell();
        listCell.tvStep = (TextView) convertView.findViewById(R.id.tvStep);
        listCell.list_image=(ImageView)convertView.findViewById(R.id.list_image);
        listCell.imageButton1=(ImageButton)convertView.findViewById(R.id.imageButton1);
        listCell.imageButton1.setTag(position);
        convertView.setTag(listCell);
    }
    else
    {
        listCell = (ListCell) convertView.getTag();
    }

    try
    {
        listCell.tvStep.setText(data.get(position));
        listCell.list_image.setImageBitmap(image_bit.get(position));
        listCell.imageButton1.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {
                // TODO Auto-generated method stub
                player=new MediaPlayer();
                     try {
                        player.setDataSource(audio_bit.get(position));
                        player.prepare();
                        player.start();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 


            }
        });


    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    // TODO Auto-generated method stub
    return convertView;
}

private class ListCell
{
    TextView tvStep;
    ImageView list_image;
    ImageButton imageButton1;
}
}

And this is the layout of each custom row in listview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/list_image"
    android:layout_width="150sp"
    android:layout_height="110dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/tvStep"
    android:layout_width="match_parent"
    android:layout_height="54dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/list_image"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageButton
    android:id="@+id/imageButton1"``
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/list_image"
    android:layout_alignParentRight="true"
    android:src="@android:drawable/ic_media_play" />

ok i see.

the problem is, your list item contains ImageButton . It grabs the focus, so list item can not be selected.

Add this line to your item layout: android:descendantFocusability="blocksDescendants"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:descendantFocusability="blocksDescendants">
    [...] //
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM