我有一个listview,片段中的每一行都有按钮,基于sqlite数据库检索的按钮数量。我想在按钮单击时移动到另一个新活动并解析按钮名称。但当我点击按钮没有任何反应时,我已经尝试在ListView setOnItemClickListener not working by adding button的xml文件中使用此代码,但它仍然无法正常工作。
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
有没有人有解决方案?谢谢你之前和之后是我的代码。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DatabaseHandler db = new DatabaseHandler(getActivity());
listView = (ListView) getView().findViewById(R.id.list_addcard);
List<CardType> listCardType = db.getAllCardType();
for(CardType ct : listCardType){
String log = "ID: " + ct.getTypeId() + " Category Name: " + ct.getTypeCategory();
Log.d("Result: ", log);
categoryArray.add(ct);
}
adapter = new ListAdapterAddCard(getActivity(), R.layout.list_row_addcard, categoryArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, final int position, long id){
categoryName = categoryArray.get(position).getTypeCategory();
Intent intent = new Intent(getActivity(), DetailCategory1.class);
intent.putExtra("categoryName", categoryName);
startActivity(intent);
}
});
}
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">
<ListView
android:id="@+id/list_addcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:layout_marginTop="30dp"/>
</RelativeLayout>
listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<Button
android:id="@+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/category"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RelativeLayout>
答案 0 :(得分:0)
首先从按钮xml中删除以下内容。
android:focusable="false"
android:focusableInTouchMode="false"
然后在您的自定义适配器中,在列表中为您的自定义xml单元格充气,获取按钮引用并在那里为该按钮设置onclick listenet。
答案 1 :(得分:0)
对我而言,如果在您无法访问上下文的函数中,在应用程序中使用startActivity函数,则需要添加主机活动的变量。基本上我认为你需要做以下事情:
Activity host = (Activity) listView.getContext();
Intent intent = new Intent(host, DetailCategory1.class);
....
host.startActivity(intent);
这是我需要在适配器的getView方法中为标准列表视图执行的操作。
答案 2 :(得分:0)
category_name按钮正在从ListView项目中窃取焦点,您可以将onClick侦听器添加到每个项目按钮,或者只使用TextView代替设置为Button,这是一种非常糟糕的做法,但会让用户感到困惑
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="8dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/category"
style="@android:style/Widget.Button"/>
</RelativeLayout>
答案 3 :(得分:0)
首先,您需要将Clickable设置为您的自定义布局按钮
<Button
android:id="@+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/category"
android:clickable="true"/>
接下来,您将OnItemClickListener设置为&#34; ListView&#34; (&lt;&lt; parent)本身不是
(按钮&gt;&gt;)子listView.setOnItemClickListener(new OnItemClickListener(){
,但
您可以通过执行以下操作对您的ListView进行>> 按钮来获取对 category_name 按钮的引用;
Button myButton = (Button) list_addcard.findViewById(R.id.category_name);
如果您需要更多帮助,可以随时访问Android文档 ListView ListView Documentation
干杯。
答案 4 :(得分:0)
所以最后我找到了答案,我将setOnClickListener更改为我的适配器类,它对我来说很好。谢谢大家的建议。
holder.txtCategoryName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, DetailCategory.class);
int pos = (Integer) v.getTag();
categoryName = categoryItems.get(pos).getTypeCategory();
intent.putExtra("categoryName", categoryName);
context.startActivity(intent);
}
});