在 Android 中访问列表视图中的项目
Access To items in a Listview in Android
我有一个名为 list_item.xml 的文件 XML,其中包含以下代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="2"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:id="@+id/telephone"
android:src="@drawable/telephone"/>
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:id="@+id/favorie"
android:src="@drawable/heart"/>
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:id="@+id/consulter"
android:src="@drawable/consulter"/>
</LinearLayout>
在 java 代码中我有 activity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultat_recherche);
xml文件的代码(resultat_recherche.xml):
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
我已经用适配器填充了这个列表:
ListAdapter adapter = new SimpleAdapter(
Resultat_recherche.this, contactList,
R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});
setListAdapter(adapter);
现在我想从 list_item (ImageView) 恢复元素以管理点击和启动意图。
我想举个例子,当我点击图像视图(电话)时,我会做一些事情
如何在 activity 中做到这一点?
谢谢
我认为您正在寻找 ListView 的 OnItemClickListener。
像这样实现它
listviewobject.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
首先在您的 list_item.xml 中将 id 添加到每个 ImageView 并假设 img1、img2、img3 依次扩展 SimpleAdapter 并覆盖 getView,如下所示
class CusAdapter extends android.widget.SimpleAdapter {
public CusAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position,convertView,parent);
ImageView img1 = (ImageView)view.findViewById(R.id.img);
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some thing
}
});
ImageView img2 = (ImageView)view.findViewById(R.id.img);
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some thing
}
});
ImageView img3 = (ImageView)view.findViewById(R.id.img);
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some thing
}
});
return view;
}
}
然后使用 CusAdapter
ListAdapter adapter = new CusAdapter(
Resultat_recherche.this, contactList,
R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});
setListAdapter(adapter);
我有一个名为 list_item.xml 的文件 XML,其中包含以下代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="2"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:id="@+id/telephone"
android:src="@drawable/telephone"/>
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:id="@+id/favorie"
android:src="@drawable/heart"/>
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:id="@+id/consulter"
android:src="@drawable/consulter"/>
</LinearLayout>
在 java 代码中我有 activity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultat_recherche);
xml文件的代码(resultat_recherche.xml):
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
我已经用适配器填充了这个列表:
ListAdapter adapter = new SimpleAdapter(
Resultat_recherche.this, contactList,
R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});
setListAdapter(adapter);
现在我想从 list_item (ImageView) 恢复元素以管理点击和启动意图。
我想举个例子,当我点击图像视图(电话)时,我会做一些事情
如何在 activity 中做到这一点? 谢谢
我认为您正在寻找 ListView 的 OnItemClickListener。
像这样实现它
listviewobject.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
首先在您的 list_item.xml 中将 id 添加到每个 ImageView 并假设 img1、img2、img3 依次扩展 SimpleAdapter 并覆盖 getView,如下所示
class CusAdapter extends android.widget.SimpleAdapter {
public CusAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position,convertView,parent);
ImageView img1 = (ImageView)view.findViewById(R.id.img);
img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some thing
}
});
ImageView img2 = (ImageView)view.findViewById(R.id.img);
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some thing
}
});
ImageView img3 = (ImageView)view.findViewById(R.id.img);
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do some thing
}
});
return view;
}
}
然后使用 CusAdapter
ListAdapter adapter = new CusAdapter(
Resultat_recherche.this, contactList,
R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name});
setListAdapter(adapter);