列表视图的 onItemClickListener 内的按钮的 onClickListener 在项目单击之前不起作用
onClickListener for buttons inside the onItemClickListener of listview not working before item click
我在 android 中有一个 listview
显示数据库中的人员信息。
listview
被赋予 OnItemClickListener()
。在 OnItemClickListener() 中,我还为列表视图项中的按钮添加了几个 onClickListener()
。在 运行 应用程序中,列表视图中的按钮不可单击或分配给按钮的 OnClickListener()
未被触发。 一旦列表视图中的项目 clicked.All 按钮点击侦听器开始工作,此问题将不再出现。
有人可以给我一个解决方案 this.The 我正在使用的代码如下。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int listrowposition = arg2;
HashMap<String, String> item = (HashMap<String, String>) arg0
.getAdapter().getItem(arg2);
final String id = item.get("ID");
Button vd = (Button) arg1.findViewById(R.id.viewDetails);
// this onclick listener is not working bfore the onitemclick
// listener
vd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"view details clicked Id " + id,
Toast.LENGTH_LONG).show();
}
});
Button gd = (Button) arg1.findViewById(R.id.getDirections);
// this onclick listener is not working bfore the onitemclick
// listener
gd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"getDirections clicked Id " + id,
Toast.LENGTH_LONG).show();
}
});
Button ra = (Button) arg1.findViewById(R.id.reqAppointment);
// this onclick listener is not working bfore the onitemclick
// listener
ra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"reqAppointment clicked Id " + id,
Toast.LENGTH_LONG).show();
}
});
}
});
我使用的适配器是SimpleAdapter。适配器详情如下。
String[] from = { "flag", "txt", "cur", "viewDet", "getDirect",
"reqAppnmnt" };
// Ids of views in listview_layout
int[] to = { R.id.flag, R.id.name, R.id.specialization,
R.id.viewDetails, R.id.getDirections, R.id.reqAppointment };
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listview_layout, from, to);
new CallToServer(adapter, aList).execute("");
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
首先创建一个自定义适配器。这可能对你有帮助 Custom Adapter for List View
然后在你的适配器中添加这些方法
private OnClickListener vdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "vd clicked, row %d", position);
}
};
private OnClickListener gdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "gd clicked, row %d", position);
}
};
private OnClickListener ravClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "ra clicked, row %d", position);
}
};
mListView
是您正在使用的列表视图。
在你的适配器 getView
方法中做这样的事情
Button vd = (Button) convertView.findViewById(R.id.viewDetails);
Button gd = (Button) convertView.findViewById(R.id.getDirections);
Button ra = (Button) convertView.findViewById(R.id.reqAppointment);
vd.setOnClickListener(vdClickListener);
gd.setOnClickListener(gdClickListener);
ra.setOnClickListener(raClickListener);
编辑
制作自定义简单适配器的通用代码是。您需要在 getVeiw()
方法中进行更改。
public class MyAdapter extends SimpleAdapter{
HashMap<String, String> map = new HashMap<String, String>();
public Adapter(Context context, List<? extends Map<String, 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 row = convertView;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.mylistlayout, parent, false);
}
TextView rw1 = (TextView)findViewById(R.id.row1);
rw1.setText("my text");
return row;
}
}
行xml必须有这一行android:descendantFocusability="blocksDescendants"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// All your widgets here
</LinearLayout>
我在 android 中有一个 listview
显示数据库中的人员信息。
listview
被赋予 OnItemClickListener()
。在 OnItemClickListener() 中,我还为列表视图项中的按钮添加了几个 onClickListener()
。在 运行 应用程序中,列表视图中的按钮不可单击或分配给按钮的 OnClickListener()
未被触发。 一旦列表视图中的项目 clicked.All 按钮点击侦听器开始工作,此问题将不再出现。
有人可以给我一个解决方案 this.The 我正在使用的代码如下。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int listrowposition = arg2;
HashMap<String, String> item = (HashMap<String, String>) arg0
.getAdapter().getItem(arg2);
final String id = item.get("ID");
Button vd = (Button) arg1.findViewById(R.id.viewDetails);
// this onclick listener is not working bfore the onitemclick
// listener
vd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"view details clicked Id " + id,
Toast.LENGTH_LONG).show();
}
});
Button gd = (Button) arg1.findViewById(R.id.getDirections);
// this onclick listener is not working bfore the onitemclick
// listener
gd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"getDirections clicked Id " + id,
Toast.LENGTH_LONG).show();
}
});
Button ra = (Button) arg1.findViewById(R.id.reqAppointment);
// this onclick listener is not working bfore the onitemclick
// listener
ra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"reqAppointment clicked Id " + id,
Toast.LENGTH_LONG).show();
}
});
}
});
我使用的适配器是SimpleAdapter。适配器详情如下。
String[] from = { "flag", "txt", "cur", "viewDet", "getDirect",
"reqAppnmnt" };
// Ids of views in listview_layout
int[] to = { R.id.flag, R.id.name, R.id.specialization,
R.id.viewDetails, R.id.getDirections, R.id.reqAppointment };
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listview_layout, from, to);
new CallToServer(adapter, aList).execute("");
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
listView.setOnScrollListener(this);
首先创建一个自定义适配器。这可能对你有帮助 Custom Adapter for List View
然后在你的适配器中添加这些方法
private OnClickListener vdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "vd clicked, row %d", position);
}
};
private OnClickListener gdClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "gd clicked, row %d", position);
}
};
private OnClickListener ravClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v(TAG, "ra clicked, row %d", position);
}
};
mListView
是您正在使用的列表视图。
在你的适配器 getView
方法中做这样的事情
Button vd = (Button) convertView.findViewById(R.id.viewDetails);
Button gd = (Button) convertView.findViewById(R.id.getDirections);
Button ra = (Button) convertView.findViewById(R.id.reqAppointment);
vd.setOnClickListener(vdClickListener);
gd.setOnClickListener(gdClickListener);
ra.setOnClickListener(raClickListener);
编辑
制作自定义简单适配器的通用代码是。您需要在 getVeiw()
方法中进行更改。
public class MyAdapter extends SimpleAdapter{
HashMap<String, String> map = new HashMap<String, String>();
public Adapter(Context context, List<? extends Map<String, 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 row = convertView;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.mylistlayout, parent, false);
}
TextView rw1 = (TextView)findViewById(R.id.row1);
rw1.setText("my text");
return row;
}
}
行xml必须有这一行android:descendantFocusability="blocksDescendants"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// All your widgets here
</LinearLayout>