我从我的数据库中提取一些数据并让它正确填充TextView。我遇到的问题是,当我点击列表中的按钮时按钮什么都不做。我在onClick中添加了代码完成,但按钮没有响应。我查看了其他堆栈帖子并尝试了解决方案,但它们无法正常工作。我确信这是我遗漏的明显事实。这是我的代码
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:id="@+id/visitorNotLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/companies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<Button
android:id="@+id/timeOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
android:layout_weight="1"
android:text="@string/timeOut" />
</TableRow>
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
timeOut = (Button)v.findViewById(R.id.timeOut);
timeOut.setTag(position);
timeOut.setFocusable(false);
timeOut.setFocusableInTouchMode(false);
timeOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
}
这是我的适配器代码
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter(GetDataFromDB.this,
productsList, R.layout.list_of_visitors_not_signed_out,
new String[] { TAG_NAME, TAG_COMPANY }, new int[] {
R.id.names, R.id.companies });
// updating listview
setListAdapter(adapter);
}
如果您希望我添加更多代码,请与我联系。
答案 0 :(得分:1)
尝试使用此代码并以任何不明显的方式为我提供回馈
ListAdapter adapter = new SimpleAdapter(GetDataFromDB.this, productsList, R.layout.list_of_visitors_not_signed_out,
new String[] { TAG_NAME, TAG_COMPANY }, new int[] {
R.id.names, R.id.companies }){
public View getView(int position, View convertView, android.view.ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Button buttonObject= (Button)v.findViewById(R.id.timeOut);
buttonObject.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// Write your code here
}
});
return v;
}
};
// updating listview
setListAdapter(adapter);'
答案 1 :(得分:0)
您应该在getView overriden函数中创建onClickListener。见下面的例子:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
TextView text = (TextView) rowView.findViewById(R.id.text);
Button myButton = (Button) rowView.findViewById(R.id.mybutton);
myButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Do something
}
});
return rowView;
}