Domanda

i have implemented the onitemCLickListener() on my listview but it is not working. The thing is that while updating my listview through Custom Adapter i have added a view to it through my code. i have tried Focusable and focusableintouch mode to false also and also tried to block the descendents, but i am not getting any solution. Please find below my code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/top_greenbox"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/backArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:layout_centerVertical="true"
        android:src="@drawable/topleft_arrow" />

    <TextView
        android:id="@+id/showdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Today is, 07 March 2014"
        android:textColor="@android:color/white" />

    <ImageView
        android:id="@+id/nextArrow"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right"
        android:src="@drawable/topright_arrow" />
</RelativeLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="60dp"
    android:descendantFocusability="blocksDescendants"
    android:smoothScrollbar="true" />

</LinearLayout>

in my Custom Adapter i am adding view int his manner:

LinearLayout list = (LinearLayout) view.findViewById(R.id.show_time);
View line = list.inflate(mContext, R.layout.show_time_item, null);
list.addView(line);

My Layout show_time_item is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/separator" />

<TextView
    android:id="@+id/time4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#55b237"
    android:gravity="center"
    android:padding="10dp"
    android:text="9:45 AM"
    android:textColor="@android:color/white"
    android:textSize="14sp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/separator" />

</LinearLayout>

Listener

my_list.setOnItemClickListener(new OnItemClickListener() {



        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Log.e("hello","hi");

            Intent i = new Intent(ABCDListActivity.this,
                    ShowABCDProfile.class);

            i.putExtra("Doc_name", name_main);
            startActivity(i);


        }

    });

Custom Adapter getView():

    View view = null;
    view = inflator.inflate(R.layout.doctor_list_item, null);
    ViewHolder holder = new ViewHolder();

    holder.doc_image = (ImageView) view.findViewById(R.id.doc_img);
    holder.doc_name = (TextView) view.findViewById(R.id.doc_name);
    holder.doc_qualification = (TextView) view
            .findViewById(R.id.doc_qualification);
    holder.doc_speciality = (TextView) view.findViewById(R.id.Specialities);
    holder.doc_experience = (TextView) view.findViewById(R.id.experience);


    holder.doc_experience.setText(list.get(position)
            .get(ResponseConst.TAG_STATUS_DOCTOR_EXP).toString());
    holder.doc_name.setText((list.get(position).get(
            ResponseConst.TAG_STATUS_DOCTOR_NAME).toString()));

    JSONArray hours = avail_list.get(position);

    Log.e("hours are", "size is  " + hours.length());

    LinearLayout list = (LinearLayout) view.findViewById(R.id.show_time);

    for (byte a = 0; a < hours.length(); a++) {

        try {
            JSONObject object_avail = hours.getJSONObject(a);
            String doctor_id = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_ID);
            String hour = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_HOUR);
            String min = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_MIN);
            String ampm = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_AMPM);
            String status = object_avail
                    .getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_STATUS);

            String time = hour + ":" + min + " " + ampm;
            View line = list.inflate(mContext, R.layout.show_time_item, null);
            holder.time = (TextView) line.findViewById(R.id.time4);
            if(status.equals(0))
                holder.time.setTextColor(Color.GRAY);
            else holder.time.setTextColor(Color.WHITE);
            holder.time.setText(time);
            list.addView(line);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    String drawablename = "no_image.png";
    int resID = mContext.getResources().getIdentifier(drawablename,
            "drawable", mContext.getPackageName());
    holder.doc_image.setBackgroundResource(resID);


    return view;

Stuck since so long. Please help

È stato utile?

Soluzione 2

I simply added the click listener in my getView() of Custom Adapter. I still don't know why the listener was not working in my activity.

Altri suggerimenti

Your ImageView takes focus when you click on List Item

Add this to LinearLayout in show_time_item.xml.

android:descendantFocusability="blocksDescendants"

You can always try using the onclick function.

XML

 <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:descendantFocusability="blocksDescendants"
        android:smoothScrollbar="true"
        android:onClick="Start" />

And in the activity you will just write a function Start.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top