Android ListView setOnItemClickListener未写入
我已经尝试过Android ListView setOnItemClickListener未写入,android,listview,android-listview,Android,Listview,Android Listview,我已经尝试过setFocusable,setItemsCanFocus,android:genderantfocusability=“blocksdessendants”,android:focusable=“false”, 但它不起作用 类中的ListView[在这个类中,我创建ListView对象,并在单击时打印一个toast,但它不起作用] ListView listviewobj; listviewobj = (ListView) findViewById(R.id.location1_
setFocusable
,setItemsCanFocus
,android:genderantfocusability=“blocksdessendants”
,android:focusable=“false”
,但它不起作用 类中的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[线性布局中创建的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>
试试这个:
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();
}
});
listviewobj.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
Toast.makeText(Location1.this,“clicked”,Toast.LENGTH_SHORT.show();
}
});
还记得“导入android.widget.AdapterView.OnItemClickListener”吗?我刚刚在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;
}
尝试将此设置到主布局
locationlistitem
android:DegenantFocusability=“BlocksDescents”@SimplePlan仍不工作适配器获取不做任何事情,只需说:P@zgc7009怎样?其显示列表在屏幕上,但仅单击“不”working@RizwanAhmed用这种方法尝试最终的解决方案。只需使用“new OnItemClickListener(){”和“import android.widget.AdapterView.OnItemClickListener”仍然不起作用。我在adapter view.SetOnClickListener中使用它
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;
}