Listview单元格无法单击

时间:2014-01-10 22:04:00

标签: java android listview android-listview

我有一个自定义列表适配器,用于填充列表视图。 我试图让listview中的每个项目都可以点击。单击时,我希望应用程序加载另一个活动并使用正确的数据填充它。数据来自listing.java

的java列表

我似乎无法回应点击,这是我到目前为止所尝试的内容:

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

这是我的第一次尝试(这只是为了让吐司工作,但它不起作用)

    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();
        };
    });

我也试过这个:

    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);
        };
    });

帮助将不胜感激。如果我也应该发布我的适配器,请告诉我!

这是适配器:

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;
    }
}

这是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> 

2 个答案:

答案 0 :(得分:0)

是的,所以我正在从严重的宿醉中恢复,我只记得我答应详细说明我发布的链接 - 忽略链接!

您应该将以下代码添加到您的ListingsAdapter:

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

您应该从

编辑RelativeLayout中的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:longClickable="true">

<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">

这是有效的,我已经测试过了。

答案 1 :(得分:0)

尝试初始化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 : 还要修改适配器类中的其他必要方法,因为您需要获取一个项目的id,以便在onItemClick方法中使用它:

@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;
}

作为一个例子。希望这会对你有所帮助。