Android ListView setOnItemClickListener无法正常工作

时间:2014-05-23 12:29:54

标签: android listview android-listview

我已经尝试setFocusablesetItemsCanFocusandroid:descendantFocusability="blocksDescendants"android:focusable="false"
但它不起作用。

Class中的ListView [在这个类中我创建了listview对象,并在点击时打印了一个toast但它不起作用]

ListView listviewobj;
listviewobj = (ListView) findViewById(R.id.location1_list);
Location_adapter adapter = new Location_adapter(Location1.this);
        listviewobj.setAdapter(adapter);
        Toast.makeText(Location1.this, "clicked", Toast.LENGTH_SHORT).show();
        listviewobj.setOnItemClickListener(new ListView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(Location1.this, "clicked", Toast.LENGTH_SHORT).show();
            }

        });

适配器[将listview设置为item的listview适配器]

public class Location_adapter extends BaseAdapter {

Context context;

public Location_adapter(Context context) {
    this.context = context;
}

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

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

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

class Holder {
    LinearLayout ll;
    ImageView img;
    TextView name, adress, rating, icon;
}

@Override
public View getView(int position, View viewcontainer, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = viewcontainer;
    Holder holder = null;
    if (view == null) {
        LayoutInflater inflater = ((Activity) context)
                .getLayoutInflater();
        view = inflater.inflate(R.layout.locationlistitem, parent,
                false);
        holder = new Holder();
        holder.ll = (LinearLayout) view
                .findViewById(R.id.location_list_item_star);
        holder.img = (ImageView) view
                .findViewById(R.id.location_list_item_img);
        holder.name = (TextView) view
                .findViewById(R.id.location_list_item_shop);
        holder.adress = (TextView) view
                .findViewById(R.id.location_list_item_address);
        holder.rating = (TextView) view
                .findViewById(R.id.location_list_item_rating);
        holder.icon = (TextView) view
                .findViewById(R.id.location_list_item_fvt_icon);
        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }
    holder.img.setImageResource(R.drawable.star2);
    holder.name.setText("Norve coffee shop");
    holder.adress.setText("Lahore");
    holder.rating.setText("0.1 M");
    holder.icon.setBackgroundResource(R.drawable.like);
    return view;
}

}

布局中的ListView [以线性布局创建的列表视图]

<LinearLayout
            android:id="@+id/location1_listlayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8.35"
            android:visibility="gone" >

            <ListView
                android:id="@+id/location1_list"
                android:layout_width="match_parent"
                android:divider="#DEDEDE"
                android:dividerHeight="1dp"
                android:layout_height="match_parent" >
            </ListView>
        </LinearLayout>

2 个答案:

答案 0 :(得分:0)

试试这个:

listviewobj.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(Location1.this, "clicked", Toast.LENGTH_SHORT).show();
        }

    });

记住&#39;导入android.widget.AdapterView.OnItemClickListener&#39;

答案 1 :(得分:0)

我刚刚在getview里面的适配器类中使用它,就像下面的代码一样,现在正在使用

public class Location_adapter extends BaseAdapter {

Context context;

public Location_adapter(Context context) {
    this.context = context;
}

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

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

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

class Holder {
    LinearLayout ll;
    ImageView img;
    TextView name, adress, rating, icon;
}

@Override
public View getView(int position, View viewcontainer, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = viewcontainer;
    Holder holder = null;
    if (view == null) {
        LayoutInflater inflater = ((Activity) context)
                .getLayoutInflater();
        view = inflater.inflate(R.layout.locationlistitem, parent,
                false);
        holder = new Holder();
        holder.ll = (LinearLayout) view
                .findViewById(R.id.location_list_item_star);
        holder.img = (ImageView) view
                .findViewById(R.id.location_list_item_img);
        holder.name = (TextView) view
                .findViewById(R.id.location_list_item_shop);
        holder.adress = (TextView) view
                .findViewById(R.id.location_list_item_address);
        holder.rating = (TextView) view
                .findViewById(R.id.location_list_item_rating);
        holder.icon = (TextView) view
                .findViewById(R.id.location_list_item_fvt_icon);
        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }
    holder.img.setImageResource(R.drawable.star2);
    holder.name.setText("Norve coffee shop");
    holder.adress.setText("Lahore");
    holder.rating.setText("0.1 M");
    holder.icon.setBackgroundResource(R.drawable.like);
view.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) { // TODO Auto-generated
                    Toast.makeText(Location1.this, "clicked", Toast.LENGTH_SHORT).show();   
                    }
                });
    return view;
}