我有一个listview的片段。我正在使用包含textview和imagebutton的list项填充lisview。我想在fragment的onCreateView()中的listitem按钮上设置onClickListener。我使用Cursoradapter和list_item.xml填充列表。但我无法在onCreateView()中获得对此按钮的引用,因为按钮不是膨胀布局的一部分,而是list_item.xml的一部分。如何在listitem按钮上设置单击侦听器。非常感谢。 这是我的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_account, container, false);
accountListView = (ListView)rootView. findViewById(R.id.list);
mCursorAdapter = new accountCursorAdapter(getActivity(), null);
accountListView.setAdapter(mCursorAdapter);
// I want to set click listener on button here.Button is in list_item.xml
//给我的cursoradapter充气。
return rootView;
}
我的适配器类: public class accountCursorAdapter扩展了CursorAdapter {
@SuppressWarnings("deprecation")
public accountCursorAdapter(Context context,Cursor c){ super(context,c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Inflate a list item view using the layout specified in list_item.xml
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
TextView nameTextView = (TextView) view.findViewById(R.id.name);
ImageButton image = (ImageButton) view.findViewById(R.id.account_menu);
int nameColumnIndex = cursor.getColumnIndex(accountContract.accountEntry.COLUMN_NAME);
String name = cursor.getString(nameColumnIndex);
nameTextView.setText(name);
int position;
position=cursor.getPosition();
image.setTag(position);
}
}
答案 0 :(得分:0)
您可以在适配器中注册onClickListener:
@Override
public void bindView(View view, final Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
TextView nameTextView = (TextView) view.findViewById(R.id.name);
ImageButton image = (ImageButton) view.findViewById(R.id.account_menu);
int nameColumnIndex =
cursor.getColumnIndex(accountContract.accountEntry.COLUMN_NAME);
String name = cursor.getString(nameColumnIndex);
nameTextView.setText(name);
int position;
position=cursor.getPosition();
image.setTag(position);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//do something
}
});
}