简体   繁体   中英

Listview cells are not clickable

I have a custom list adapter that populates a listview. I am trying to get each item in the listview to be clickable. When clicked, I want the app to load another activity and populate it with the proper data. The data comes from a java list of listing.java .

I can't seem to get it to respond to clicks, here is what I've tried so far:

//this is in the onCreate method
    final ListView listview = (ListView) findViewById(R.id.listview);
    listingsAdapter = new ListingsAdapter(this,  mylistings);
    listview.setAdapter(listingsAdapter);

here is my first attempt (this was just to get toast working, but it didn't work)

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, final View view,int position, long id) {
        Toast.makeText(getApplicationContext(),
        "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
        };
    });

I have also tried this:

    listview.setOnItemClickListener( new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent i = new Intent(HomeScreenActivity.this, DetailedViewActivity.class);
            startActivity(i);
        };
    });

Help would be appreciated. Let me know if I should post my adapter as well!

Here is the adaptor:

public class ListingsAdapter extends BaseAdapter{

    List<Listing> listings;
    Context context;
    LayoutInflater inflater;

    public ListingsAdapter(Context context, List<Listing> listings){
        this.context = context;
        this.listings = listings;
        inflater = (LayoutInflater) context.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public boolean isEnabled(int position)
    {   
        return true;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View _localView = convertView;
        if (_localView == null){
            _localView = inflater.inflate(R.layout.main_cell, parent, false);
        }
        TextView text1 = (TextView) _localView.findViewById(R.id.firstline);
        TextView text2 = (TextView) _localView.findViewById(R.id.secondLine);;
        Listing listing = listings.get(position);
        text1.setText(listing.getTitle());
        text2.setText(listing.getAddress());
        return _localView;
    }



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

    @Override
    public Object getItem(int arg0){
        return listings.get(arg0);
    }

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

this is the main_cell.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dp"
android:background="#CC7C43"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" />

<TextView
    android:id="@+id/firstline"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLine"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp"/>

</RelativeLayout> 

Right, so I am recovering from a severe hangover and I just remembered that I promised to elaborate on the link I posted - disregard the link!

You should add the following piece of code to your ListingsAdapter:

@Override
public boolean isEnabled(int position)
{
    return true;
}

And you should edit the RelativeLayout in your main_cell.xml from

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dp"
    android:background="#CC7C43"
    android:longClickable="true">

to

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dp"
    android:background="#CC7C43">

This works, I have tested it.

try to initialize local instance of View:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View _localView = convertView;
    if (_localView == null){
        _localView = inflater.inflate(R.layout.main_cell, parent, false);
    }
    TextView text1 = (TextView) _localView.findViewById(R.id.firstline);
    TextView text2 = (TextView) _localView.findViewById(R.id.secondLine);;
    Listing listing = listings.get(position);
    text1.setText(listing.getTitle());
    text2.setText(listing.getAddress());
    return _localView;
}

UPD : Also modify other necessary methods in your adapter class, because you need to get id of an item clicked for using it in onItemClick method:

@Override
public Object getItem(int arg0){
    // TODO Auto-generated method stub
    listings.get(arg0);
}

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

as an example. Hope this will help you.

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