简体   繁体   English

Listview单元格不可点击

[英]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. 我正在尝试使listview中的每个项目都可单击。 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 . 数据来自listing.java的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: 这是main_cell.xml文件:

<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: 您应该将以下代码添加到ListingsAdapter中:

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

And you should edit the RelativeLayout in your main_cell.xml from 并且您应该在main_cell.xml从以下位置编辑RelativeLayout

<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: 尝试初始化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: UPD :还需要修改适配器类中的其他必要方法,因为您需要获取要在onItemClick方法中使用该项目的单击项的ID:

@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. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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