如何使我的数据库游标支持的 ListView 中的 ImageView 可点击?
How can I make an ImageView in my database cursor-backed ListView clickable?
我有一个人员列表,我正在使用 [修改过的] SimpleCursorAdapter 从数据库中提取这些人员。然后我遍历光标并在我的应用程序的 ListView 中打印信息。
现在,它经过编码,因此当我单击一行时,我的应用程序会根据从数据库中检索到的信息执行一项功能。但是,我想做的是,当我单击行内的 ImageView 时,我的应用程序将根据从数据库中检索到的信息执行适当的功能。
我想要的是当我单击 "status" ImageView 时,我希望它执行功能 "A",当我单击 "location" ImageView 时,我希望它执行功能"B".
我遇到问题的地方是隔离 ListView 行中的 ImageView,以便仅在该 ImageView 而不是整行上使用 setOnItemClickListener。我很确定我需要进一步修改修改后的游标适配器,但不确定如何修改。
这是我的:
Home.java
package myPackage;
public class Home extends Fragment {
private View rootView;
private AlternateRowColorSimpleCursorAdapter mySimpleCursorAdapter;
private ViewPager myViewPager;
private SwipeRefreshLayout studentSwipeRefresh;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
myViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
studentSwipeRefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.student_swipe_refresh);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
drawTheStudentView();
studentSwipeRefresh.setColorSchemeColors(Color.parseColor(Constants.RED), Color.parseColor(Constants.ORANGE), Color.parseColor(Constants.YELLOW), Color.parseColor(Constants.GREEN), Color.parseColor(Constants.BLUE), Color.parseColor(Constants.INDIGO), Color.parseColor(Constants.VIOLET));
studentSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
studentSwipeRefresh.setRefreshing(false);
drawTheStudentView();
}
});
}
private void drawTheStudentView(){
DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());
Cursor studentCursor = myDBHelper.getStudentsCursor();
String[] fromColumns = {"_id","studentID","status","location"};
int[] toViews = {R.id.student_number_textview, R.id.student_id_textview};
// What I want it to be ...
// int[] toViews = {R.id.student_number_textview, R.id.student_id_textview, R.id.student_status_imageview, R.id.student_location_imageview};
mySimpleCursorAdapter = new AlternateRowColorSimpleCursorAdapter(getActivity(), R.layout.student_layout, studentCursor, fromColumns, toViews, 0);
// Replace the _id column with a student count
mySimpleCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
String counter = Integer.toString((cursor.getPosition()+1));
TextView modifiedTextView = (TextView) view;
if(columnIndex == 0){
modifiedTextView.setText(counter);
return true;
}
return false;
}
});
ListView myListView = (ListView) rootView.findViewById(R.id.student_row);
// Listen for somebody clicking on a Student ID, and process
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position);
String studentIDNumber = subCursor.getString(subCursor.getColumnIndex("studentID"));
StudentStatus studentStatus = (StudentStatus) getFragmentManager().findFragmentByTag(getFragmentTag(Constants.TAB_INDEX_PATIENT_VITALS));
studentStatus.setStudentIDNumber(studentIDNumber);
myViewPager.setCurrentItem(Constants.TAB_INDEX_PATIENT_VITALS);
}
});
// Draw the list
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
// Pass me a tab index (see Constants.java) and I'll return a refrence to that tab.
private String getFragmentTag(int tagID){
return "android:switcher:" + R.id.pager + ":" + tagID;
}
}
AlternateRowColorSimpleCursorAdapter.java
package myPackage;
public class AlternateRowColorSimpleCursorAdapter extends SimpleCursorAdapter {
public AlternateRowColorSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = super.getView(position, convertView, parent);
if(position % 2 == 0){
row.setBackgroundColor(Color.parseColor(Constants.WHITE));
} else {
row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY));
}
return row;
}
}
student_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/student_number_textview"
android:typeface="monospace"
android:layout_weight="1"
android:textStyle="bold"
style="@style/StudentStyle" />
<TextView
android:id="@+id/student_id_textview"
android:typeface="monospace"
style="@style/StudentStyle" />
<ImageView
android:id="@+id/student_status_button"
style="@style/studentIconLink"
android:src="@drawable/status_icon"/>
<ImageView
android:id="@+id/student_location_button"
style="@style/studentIconLink"
android:src="@drawable/location_icon"/>
</LinearLayout>
修改您的适配器以添加点击侦听器:
public View getView(final int position, View convertView, ViewGroup parent){
// your code goes here, then add the following
ImageView statusButton = row.findViewById(R.id.student_status_button);
ImageView locationButton = row.findViewById(R.id. student_location_button);
OnClickListener statusButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
notifyStatusButtonListenerClicked(position);
}
};
OnClickListener locationButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
notifyLicationButtonListenerClicked(position);
}
};
return row;
}
这两个通知都可以通过界面传送到您的activity/fragment。只需添加类似 notifyLicationButtonListenerClicked(int position) 的方法,这些方法将在实现您的接口的侦听器上调用方法。还要确保删除每行设置的现有 setOnItemClickListener 侦听器。
在您的游标适配器中创建一个静态 class 来保存列表的元素(静态不是强制性的)然后在 getView
中初始化这些元素
使用
public void bindView(View视图,Context上下文,Cursor游标)
在列表视图的任何元素上设置点击侦听器的光标适配器方法
示例:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.list, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.mImage= (ImageView) view.findViewById(R.id.imageview);
view.setTag(viewHolder);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
final ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.mImage.setOnclickListener(new OnClickListener(){
//do stuff
});
}
public static Class ViewHolder{
public ImageView mImage;
}
你可以使用imageButton(这只是一个例子)
我有一个人员列表,我正在使用 [修改过的] SimpleCursorAdapter 从数据库中提取这些人员。然后我遍历光标并在我的应用程序的 ListView 中打印信息。
现在,它经过编码,因此当我单击一行时,我的应用程序会根据从数据库中检索到的信息执行一项功能。但是,我想做的是,当我单击行内的 ImageView 时,我的应用程序将根据从数据库中检索到的信息执行适当的功能。
我想要的是当我单击 "status" ImageView 时,我希望它执行功能 "A",当我单击 "location" ImageView 时,我希望它执行功能"B".
我遇到问题的地方是隔离 ListView 行中的 ImageView,以便仅在该 ImageView 而不是整行上使用 setOnItemClickListener。我很确定我需要进一步修改修改后的游标适配器,但不确定如何修改。
这是我的:
Home.java
package myPackage;
public class Home extends Fragment {
private View rootView;
private AlternateRowColorSimpleCursorAdapter mySimpleCursorAdapter;
private ViewPager myViewPager;
private SwipeRefreshLayout studentSwipeRefresh;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.home, container, false);
myViewPager = (ViewPager) getActivity().findViewById(R.id.pager);
studentSwipeRefresh = (SwipeRefreshLayout) rootView.findViewById(R.id.student_swipe_refresh);
return rootView;
}
@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
drawTheStudentView();
studentSwipeRefresh.setColorSchemeColors(Color.parseColor(Constants.RED), Color.parseColor(Constants.ORANGE), Color.parseColor(Constants.YELLOW), Color.parseColor(Constants.GREEN), Color.parseColor(Constants.BLUE), Color.parseColor(Constants.INDIGO), Color.parseColor(Constants.VIOLET));
studentSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
studentSwipeRefresh.setRefreshing(false);
drawTheStudentView();
}
});
}
private void drawTheStudentView(){
DatabaseHelper myDBHelper = new DatabaseHelper(getActivity());
Cursor studentCursor = myDBHelper.getStudentsCursor();
String[] fromColumns = {"_id","studentID","status","location"};
int[] toViews = {R.id.student_number_textview, R.id.student_id_textview};
// What I want it to be ...
// int[] toViews = {R.id.student_number_textview, R.id.student_id_textview, R.id.student_status_imageview, R.id.student_location_imageview};
mySimpleCursorAdapter = new AlternateRowColorSimpleCursorAdapter(getActivity(), R.layout.student_layout, studentCursor, fromColumns, toViews, 0);
// Replace the _id column with a student count
mySimpleCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
String counter = Integer.toString((cursor.getPosition()+1));
TextView modifiedTextView = (TextView) view;
if(columnIndex == 0){
modifiedTextView.setText(counter);
return true;
}
return false;
}
});
ListView myListView = (ListView) rootView.findViewById(R.id.student_row);
// Listen for somebody clicking on a Student ID, and process
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position);
String studentIDNumber = subCursor.getString(subCursor.getColumnIndex("studentID"));
StudentStatus studentStatus = (StudentStatus) getFragmentManager().findFragmentByTag(getFragmentTag(Constants.TAB_INDEX_PATIENT_VITALS));
studentStatus.setStudentIDNumber(studentIDNumber);
myViewPager.setCurrentItem(Constants.TAB_INDEX_PATIENT_VITALS);
}
});
// Draw the list
myListView.setAdapter(mySimpleCursorAdapter);
myDBHelper.close();
}
// Pass me a tab index (see Constants.java) and I'll return a refrence to that tab.
private String getFragmentTag(int tagID){
return "android:switcher:" + R.id.pager + ":" + tagID;
}
}
AlternateRowColorSimpleCursorAdapter.java
package myPackage;
public class AlternateRowColorSimpleCursorAdapter extends SimpleCursorAdapter {
public AlternateRowColorSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = super.getView(position, convertView, parent);
if(position % 2 == 0){
row.setBackgroundColor(Color.parseColor(Constants.WHITE));
} else {
row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY));
}
return row;
}
}
student_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="@+id/student_number_textview"
android:typeface="monospace"
android:layout_weight="1"
android:textStyle="bold"
style="@style/StudentStyle" />
<TextView
android:id="@+id/student_id_textview"
android:typeface="monospace"
style="@style/StudentStyle" />
<ImageView
android:id="@+id/student_status_button"
style="@style/studentIconLink"
android:src="@drawable/status_icon"/>
<ImageView
android:id="@+id/student_location_button"
style="@style/studentIconLink"
android:src="@drawable/location_icon"/>
</LinearLayout>
修改您的适配器以添加点击侦听器:
public View getView(final int position, View convertView, ViewGroup parent){
// your code goes here, then add the following
ImageView statusButton = row.findViewById(R.id.student_status_button);
ImageView locationButton = row.findViewById(R.id. student_location_button);
OnClickListener statusButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
notifyStatusButtonListenerClicked(position);
}
};
OnClickListener locationButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
notifyLicationButtonListenerClicked(position);
}
};
return row;
}
这两个通知都可以通过界面传送到您的activity/fragment。只需添加类似 notifyLicationButtonListenerClicked(int position) 的方法,这些方法将在实现您的接口的侦听器上调用方法。还要确保删除每行设置的现有 setOnItemClickListener 侦听器。
在您的游标适配器中创建一个静态 class 来保存列表的元素(静态不是强制性的)然后在 getView
中初始化这些元素使用 public void bindView(View视图,Context上下文,Cursor游标)
在列表视图的任何元素上设置点击侦听器的光标适配器方法
示例:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.list, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.mImage= (ImageView) view.findViewById(R.id.imageview);
view.setTag(viewHolder);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
final ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.mImage.setOnclickListener(new OnClickListener(){
//do stuff
});
}
public static Class ViewHolder{
public ImageView mImage;
}
你可以使用imageButton(这只是一个例子)