(Android practice) custom CursorAdapter to dynamically load buttons and click events in ListView

Source: Internet
Author: User

Preface:

The built-in CursorAdapter adapter in Android can load data bindings in fixed formats. However, in actual application development, the Item display in ListView varies with the data.

Example:This problem is encountered in our actual development.

Different buttons are displayed based on the answer to a topic, and click the button to go to the corresponding processing interface.

Topic status] First answer
, Display 【Start] Button
 

Event: Click the button to download the question library and go to the answer page.

Topic status] The answer is not over.
, Display 【Continue] Button
 

Event: Click the button to go to the answer page.

Topic status] All answers are answered
, Display 【Redo] Button
 

Event: Click the button to clear the answer trace, go to the answer page, and answer questions again

Implemented:

In this case, the original built-in CursorAdapter cannot meet the requirements.

1Custom CursorAdapterDescription

Two functions need to be rewritten for custom CursorAdapter

1.1 public
Void bindView (View view, Context context, Cursor cursor)

Views function binding

ViewParameters: The Item View of the current ListView, which contains the ListView
Item interface elements

The interface element is called in the following way:

TextView noteData = (TextView)
View. findViewById (R. id. note_list_time );

CursorParameter: current cursor

AccordingCursorTo obtain data

String ID = Cursor. getString (3)

1.2 public
View newView (Context arg0, Cursor arg1, ViewGroup arg2)

Get the function of the Item Interface Element in ListView

In general, the XML interface layout file is extended and defined

Return
MInflater. inflate (R. layout. note_item, arg2, false );

2 ListViewMedium ButtonClick Event implementation

ListView
To implement the Button event in the Item, the most important thing in implementing the Button event is that the current button has a unique identifier for the Item, and how to save this unique identifier to the button,

The View containsSetTagAndGetTagA pair of methods, like this method, can save the unique identifier of Item to the Tag of the Button when 'view' is bound, and read the identifier in the Button click event.

3Source code implementation (Note: Click the icon to use the ImageViewReplacedThe implementation principle is the same)

3.1 ListItem)

View Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/relativeLayout1"
android:background="@drawable/cl_itemback"
android:layout_width="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" >
<TextView
android:layout_width="60px"
android:layout_height="wrap_content"
android:id="@+id/theme_id"
android:visibility="gone"/>
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout4" >
<TextView android:layout_marginLeft="10px" android:textSize="18px" android:textColor="#000000" android:text="" android:id="@+id/txt_themetitle" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:layout_marginLeft="10px" android:textSize="14px" android:layout_marginTop="5px" android:textColor="#000000" android:text="" android:id="@+id/txt_themedescribe" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<LinearLayout android:gravity="center_vertical" android:orientation="vertical" android:layout_width="wrap_content" android:layout_marginTop="15px" android:paddingRight="10px" android:layout_height="wrap_content" android:id="@+id/linearLayout4" android:layout_alignParentRight="true" >
<ImageView android:src="@drawable/cl_begin" android:id="@+id/imgv_exbut" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</RelativeLayout>

3.2 customCursorAdapter

 

/**/Public class CursorAdapter_ExecuteTheme extends CursorAdapter {private LayoutInflater mInflater; private Context mContext; TextView theme_id; public aggregate (Context context, Cursor c) {super (context, c ); mContext = context; mInflater = LayoutInflater. from (context) ;}@ Overridepublic void bindView (View view, Context context, Cursor cursor) {theme_id = (TextView) view. findViewById (R. id. theme_id); TextView txt_themetitle = (TextView) Comment _ themetitle); TextView txt_themedescribe = (TextView) Comment _ themedescribe); ImageView img_exbut = (ImageView) view. findViewById (R. id. imgv_exbut); theme_id.setText (cursor. getString (0); txt_themetitle.setText (cursor. getString (2); // pk as _ id, catalogFK, Name, 3 GuessCount, 4 State, 5 CurIndex, Describe from CLThemetxt_themedescribe.setText ("done (" + cursor. getString (5) + ") All (" + cursor. getString (3) + ")"); int state = cursor. getInt (4); img_exbut.setTag (cursor. getString (0); if (state = ProjectConstant. themeState_BEGGING) | (state = ProjectConstant. themeState_NONE) // The topic starts {if (state = ProjectConstant. themeState_BEGGING) img_exbut.setImageResource (R. drawable. cl_goon); img_exbut.setOnClickListener (new ImageView. onClickListener () {public void onClick (View v) {SoundManager. instance. playSound ("SOUND_WELCOME"); String id = v. getTag (). toString (); // obtain the current topic status // create the current test business logic and load the selected topic data ExerciseBussiness. selectTheme (id, ProjectConstant. themeType_Classic, mContext); Intent I = new Intent (Activity) mContext, ExerciseGuess. class); I. putExtra ("themefk", id); mContext. startActivity (I); (Activity) mContext ). finish () ;}) ;}else if (state = ProjectConstant. themeState_END) // redo {img_exbut.setImageResource (R. drawable. cl_redo); img_exbut.setOnClickListener (new ImageView. onClickListener () {public void onClick (View v) {SoundManager. instance. playSound ("SOUND_WELCOME"); String id = v. getTag (). toString (); ExerciseBussiness. reDoTheme (id, mContext); // re-question Intent I = new Intent (Activity) mContext, ExerciseGuess. class); I. putExtra ("themefk", id); mContext. startActivity (I); (Activity) mContext ). finish () ;}}) ;}@ Overridepublic View newView (Context arg0, Cursor arg1, ViewGroup arg2) {return mInflater. inflate (R. layout. sub_extheme_item, arg2, false );}}


3.3 interface Background call

 ListView ectheme_list =(ListView)findViewById(R.id.ectheme_list);            ThemeCatalogDataReadHelper model= new ThemeCatalogDataReadHelper(this);            Cursor cursor =model.GetEcList(this.getIntent().getStringExtra("catalogpk"));            CursorAdapter_ExecuteTheme adapter=new CursorAdapter_ExecuteTheme(this,cursor);            ectheme_list.setAdapter(adapter);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.