使用SimpleCursorAdapter OnItemClickListener不工作的ListView [英] ListView using SimpleCursorAdapter OnItemClickListener not working
问题描述
我可以填充我的名单与数据库值。我的问题是ListView的布局看起来像这样(例如) - >(TextView的,删除按钮)
活动1 || Delete_Button
活动2 || Delete_Button
和我想要的是,当我点击活动1,它会去活动1意图。或者当我点击活动2,它会去活动2意图。同为删除按钮,如果我点击第1行的删除按钮,它会删除第1行等。
如何做到这一点?我是新的Android处。
这是我的code:
的ListView ListView的;
ArrayList的<串GT;清单; 公众诠释goal_id;
INT I = 0; //数据库变量
MyDBAdapter dbhandler;
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_set_act);
TypefaceProvider.registerDefaultIconSets(); 捆绑额外= getIntent()getExtras()。 如果(演员== NULL){
返回;
} goal_id =的Integer.parseInt(extras.getString(goalid)); 工具条工具栏=(栏)findViewById(R.id.toolbar);
setSupportActionBar(工具栏);
toolbar.setSubtitle(活动列表);
动作条动作条= getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_back);
actionBar.setDisplayHomeAsUpEnabled(真); FloatingActionButton FAB =(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
Snackbar.make(看来,增加了新的活动,Snackbar.LENGTH_LONG)
.setAction(增加了新的活动,NULL).show(); onCLick_addAct();
}
}); dbhandler =新MyDBAdapter(本);
populateListView();
listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){ 龙TEMP = listView.getItemIdAtPosition(位置);
MessageTo.message(SetActActivity.this,Long.toString(临时));
}
}); } 公共无效populateListView(){
光标光标= dbhandler.getAllActivitiesByGoalCursor(goal_id);
的String [] = actlist新的String [] {} dbhandler.dbhandler.COLUMN_ACTIVITY_NAME;
INT [] = actNames新INT [] {} R.id.list_item_string; SimpleCursorAdapter myAdapter =新SimpleCursorAdapter(SetActActivity.this,R.layout.act_list,光标,actlist,actNames,0){
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V = convertView;
ViewHolder持有人;
如果(V == NULL){
持有人=新ViewHolder();
LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.act_list,NULL);
holder.textView =(TextView中)v.findViewById(R.id.list_item_string);
holder.button =(按钮)v.findViewById(R.id.delete_btn);
v.setTag(保持器);
holder.button.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
INT POS =(int)的v.getTag();
list.remove(POS)
notifyDataSetChanged();
}
});
}其他{
支架=(ViewHolder)v.getTag();
}
// holder.textView.setText(list.get(位置)获得(称号)); //的get(称号)是错误。未定义 holder.button.setTag(位置);
返回伏; } 类ViewHolder {
TextView中的TextView;
Button按钮;
}
};
//处理ListView和分配适配器
ListView控件=(ListView控件)findViewById(R.id.list);
//附加光标适配器到ListView
listView.setAdapter(myAdapter); }
这其中code - >它不会区分,如果我点击的TextView或删除按钮。它仍然会执行这不是我希望发生同样的功能。 (
listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){ 龙TEMP = listView.getItemIdAtPosition(位置);
MessageTo.message(SetActActivity.this,Long.toString(临时));
}
});
您正在使用其中覆盖的事件列表中的焦点的项(按钮
) OnItemClickListener
。该解决方案是使用两个单独的 onClickListeners
同时创造了适配器的意见。
此问题是pretty常见。见<一href=\"http://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview-android\">this以供参考。
现在,code定制 SimpleCursorAdapter
:
公共类MyAdapter扩展SimpleCursorAdapter
{ 私人语境mContext;
私人语境appContext;
私人诠释布局;
私人光标CR,
私人最终LayoutInflater吹气; 公共MyAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []为)
{
超(背景下,布局,C,从,到);
this.layout =布局;
this.mContext =背景;
this.inflater = LayoutInflater.from(上下文);
this.cr = C;
} @覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父)
{
返回inflater.inflate(布局,NULL);
} @覆盖
公共无效bindView(查看视图,最终上下文的背景下,光标光标)
{
super.bindView(查看,背景,光标);
RelativeLayout的parentView =(RelativeLayout的)view.findViewById(R.id.parentView);
TextView中的TextView =(TextView中)view.findViewById(R.id.itemLayoutText);
//设置使用光标列索引的文本。
textview.setText(cursor.getString(1));
Button按钮=(按钮)view.findViewById(R.id.itemLayoutBtn);
button.setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(查看视图)
{
//使用接口回调发送到活动
Toast.makeText(上下文,单击按钮,Toast.LENGTH_SHORT).show();
}
}); parentView.setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(查看视图)
{
//使用接口回调发送到活动
Toast.makeText(背景下,列表项点击Toast.LENGTH_SHORT).show();
}
}); }
}
列表项的布局:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / parentView&GT; &LT;的TextView
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_alignParentLeft =真
机器人:textAppearance =?机器人:textAppearanceMedium
机器人:ID =@ + ID / itemLayoutText/&GT; &LT;按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / itemLayoutBtn
机器人:文字=删除
机器人:layout_alignParentRight =真
/&GT;&LT; / RelativeLayout的&GT;
I can populate my list with the database values. My problem is that ListView layout looks like this (for example) -> ( textview, delete button )
Activity 1 || Delete_Button
Activity 2 || Delete_Button
and what i want is that when i clicked Activity 1, it will go to Activity 1 intent. or when i clicked Activity 2, it will go to Activity 2 intent. same as the delete button, if I clicked the row 1 delete button, it will delete row 1 and etc.
How to do this ? I'm new at Android.
This is my code:
ListView listView ;
ArrayList<String> list;
public int goal_id;
int i = 0;
//database variables
MyDBAdapter dbhandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_act);
TypefaceProvider.registerDefaultIconSets();
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
goal_id = Integer.parseInt(extras.getString("goalid"));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setSubtitle("Activities List");
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_back);
actionBar.setDisplayHomeAsUpEnabled(true);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Added new activity", Snackbar.LENGTH_LONG)
.setAction("Added new activity", null).show();
onCLick_addAct();
}
});
dbhandler = new MyDBAdapter(this);
populateListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Long temp = listView.getItemIdAtPosition(position);
MessageTo.message(SetActActivity.this, Long.toString(temp));
}
});
}
public void populateListView(){
Cursor cursor = dbhandler.getAllActivitiesByGoalCursor(goal_id);
String[] actlist = new String[] {dbhandler.dbhandler.COLUMN_ACTIVITY_NAME};
int[] actNames = new int[] {R.id.list_item_string};
SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(SetActActivity.this,R.layout.act_list,cursor,actlist,actNames,0){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.act_list, null);
holder.textView = (TextView) v.findViewById(R.id.list_item_string);
holder.button = (Button) v.findViewById(R.id.delete_btn);
v.setTag(holder);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (int) v.getTag();
list.remove(pos);
notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) v.getTag();
}
// holder.textView.setText(list.get(position).get("title")); //the get("title") is error. undefined
holder.button.setTag(position);
return v;
}
class ViewHolder {
TextView textView;
Button button;
}
};
//handle listview and assign adapter
listView = (ListView)findViewById(R.id.list);
// Attach cursor adapter to the ListView
listView.setAdapter(myAdapter);
}
This one code -> it wont distinguish if i clicked the textview or the delete button. It will still implement the same function which is not what i want to happen. :(
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Long temp = listView.getItemIdAtPosition(position);
MessageTo.message(SetActActivity.this, Long.toString(temp));
}
});
You are using a focusable item (Button
) in a list which is overriding the event of OnItemClickListener
. The solution is to use two separate onClickListeners
while creating the views in the adapter.
This issue is pretty common. See this for reference.
Now, the code for custom SimpleCursorAdapter
:
public class MyAdapter extends SimpleCursorAdapter
{
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, final Context context, Cursor cursor)
{
super.bindView(view, context, cursor);
RelativeLayout parentView = (RelativeLayout) view.findViewById(R.id.parentView);
TextView textview = (TextView) view.findViewById(R.id.itemLayoutText);
// Set the text using cursor with column index.
textview.setText(cursor.getString(1));
Button button = (Button) view.findViewById(R.id.itemLayoutBtn);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
// Use interface to send a callback to the activity
Toast.makeText(context, "Button clicked", Toast.LENGTH_SHORT).show();
}
});
parentView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
// Use interface to send a callback to the activity
Toast.makeText(context, "List item clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
Layout of list item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parentView">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:textAppearance="?android:textAppearanceMedium"
android:id="@+id/itemLayoutText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemLayoutBtn"
android:text="Delete"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
这篇关于使用SimpleCursorAdapter OnItemClickListener不工作的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!