我在android中有一个listview
,它显示来自数据库的人员信息。
listview
被赋予OnItemClickListener()
。在OnItemClickListener()中,我还为listview项中的按钮添加了少量onClickListener()
。在运行应用程序时,列表视图中的按钮不可单击,或者按钮的OnClickListener()
未被触发。 点击列表视图中的项目后,将不再显示此问题。所有按钮点击监听器都在此后工作。
有人可以给我一个解决方案。我使用的代码如下。
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);
答案 0 :(得分:5)
行xml必须包含以下行 android:descendantFocusability =“blocksDescendants”
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<ul class="nav nav-tabs" nav-justified role="tablist">
<li id="itemo" class="active"><a href="#loanInvoice" role="tab" data-toggle="tab">my loan invoices</a>
</li>
<li id="itemr"><a data-bind="enable: invoicedatasintable().length > 0" href="#investmentInvoice" role="tab" data-toggle="tab">my investment invoices</a>
</li>
</ul>
<div class="tabBorder">
<div id="tabs" class="tab-content">
<div class="tab-pane active" id="loanInvoice">
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="text-center">
<?php echo lang( "invoice_table_invoiced"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_Paid"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_Currency"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_rf"); ?>
</th>
<th class="text-center">
<?php echo lang( "myinvoice_table_type"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_status"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_pay"); ?>
</th>
</tr>
</thead>
<tbody data-bind="foreach: invoicedatasintable">
<tr data-bind="if : $data.type == 2 ">
<td class="text-center"><span data-bind="text: $data.invoiced_total"></span>
</td>
<td class="text-center"><span data-bind="text: $data.paid_total "></span>
</td>
<td class="text-center"><span data-bind="text: $data.Abbreviation"></span>
</td>
<td class="text-center"><a href="#" data-bind="click: $root.getSelectedInvoice"><span data-bind="text: $data.rf_reference"></span></a>
</td>
<!-- <td><span data-bind="text: $data.rf_reference"></span></td> -->
<td class="text-center"><span data-bind="text: $data.type_txt"></span>
</td>
<td class="text-center"><span data-bind="text: $data.status_description"></span>
</td>
<td class="text-center">
<a href="#" data-bind="if: parseFloat($data.invoiced_total()) > parseFloat($data.paid_total()), click: $root.getRepaymentInvoice">
<?php echo lang( "invoice_table_pay1"); ?>
</a>
<span data-bind="ifnot: parseFloat($data.invoiced_total()) > parseFloat($data.paid_total())"><?php echo lang("invoice_table_pay1"); ?></span>
</td>
</tr>
</tbody>
</table>
</div>
<!-- table-responsive -->
<div class="pager" data-bind="template:{ name: 'tpl-pager', data: Pagerinvoicedatas }"></div>
</div>
<!-- transhistory -->
<div class="tab-pane" id="investmentInvoice">
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="text-center">
<?php echo lang( "invoice_table_invoiced"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_Paid"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_Currency"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_rf"); ?>
</th>
<th class="text-center">
<?php echo lang( "myinvoice_table_type"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_status"); ?>
</th>
<th class="text-center">
<?php echo lang( "invoice_table_pay"); ?>
</th>
</tr>
</thead>
<tbody data-bind="foreach: invoicedatasintable">
<tr data-bind="if : $data.type == 1">
<td class="text-center"><span data-bind="text: $data.invoiced_total"></span>
</td>
<td class="text-center"><span data-bind="text: $data.paid_total "></span>
</td>
<td class="text-center"><span data-bind="text: $data.Abbreviation"></span>
</td>
<td class="text-center"><a href="#" data-bind="click: $root.getSelectedInvoice"><span data-bind="text: $data.rf_reference"></span></a>
</td>
<!-- <td><span data-bind="text: $data.rf_reference"></span></td> -->
<td class="text-center"><span data-bind="text: $data.type_txt"></span>
</td>
<td class="text-center"><span data-bind="text: $data.status_description"></span>
</td>
<td class="text-center">
<a href="#" data-bind="if: parseFloat($data.invoiced_total()) > parseFloat($data.paid_total()), click: $root.getRepaymentInvoice">
<?php echo lang( "invoice_table_pay1"); ?>
</a>
<span data-bind="ifnot: parseFloat($data.invoiced_total()) > parseFloat($data.paid_total())"><?php echo lang("invoice_table_pay1"); ?></span>
</td>
</tr>
</tbody>
</table>
</div>
<!-- table-responsive -->
<div class="pager" data-bind="template:{ name: 'tpl-pager', data: Pagerinvoicedatas }"></div>
</div>
</div>
</div>
答案 1 :(得分:2)
首先创建一个自定义适配器。这可能会对您有所帮助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;
}
}