我的adapter
public class SingleAdapter extends BaseAdapter { Context context; private ArrayList<String> groups; public SingleAdapter(Context context, ArrayList<String> groups) { this.context = context; this.groups = groups; } @Override public int getCount() { return groups.size(); } @Override public Object getItem( int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId( int position) { // TODO Auto-generated method stub return 0; } @Override public View getView( final int position, View convertView, ViewGroup parent) { final SingleView singleView = new SingleView(context); singleView.setTitle(groups.get(position)); return singleView; } }
自定义控件
public class SingleView extends LinearLayout implements Checkable { private TextView mText; private CheckBox mCheckBox; public SingleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } public SingleView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public SingleView (Context context) { super(context); initView(context); } private void initView(Context context){ // 填充布局 LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.list_adapter ,this); mText = (TextView) v.findViewById(R.id.tv_zxing_section_sequence); mCheckBox = (CheckBox) v.findViewById(R.id.item_cb_section); } @Override public void setChecked( boolean checked) { mCheckBox.setChecked(checked); } @Override public boolean isChecked() { return mCheckBox.isChecked(); } @Override public void toggle() { mCheckBox.toggle(); } public void setTitle(String text){ mText.setText(text); } }自定义控件加载的布局文件list_adapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/item_cb_section" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="true" android:clickable="false" android:layout_alignParentTop="true" android:layout_marginLeft="5dp" /> <TextView android:id="@+id/tv_zxing_section_sequence" android:layout_width="50dp" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout>
activity里面的代码
public class ActivityOne extends Activity { private ArrayList<String> groups; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_one); listView = (ListView) findViewById(R.id.listView); groups = new ArrayList<String>(); groups.add( "11"); groups.add( "22"); groups.add( "33"); groups.add( "44"); groups.add( "55"); groups.add( "66"); groups.add( "77"); groups.add( "88"); groups.add( "99"); groups.add( "00"); SingleAdapter singleAdapter = new SingleAdapter(this,groups); listView.setAdapter(singleAdapter);}
}
如何需要获取已选中的项;则在actvity调用此方法
private void PickNum() { int position = listView.getCheckedItemPosition(); if (ListView. INVALID_POSITION != position) { Toast. makeText(this, groups.get(position), Toast.LENGTH_SHORT).show(); } }
- private void PickNum() {
- int position = listView.getCheckedItemPosition();
- if (ListView. INVALID_POSITION != position) {
- Toast. makeText(MainActivity.this, groups.get(position), 0).show();
- }
- }
本文介绍了一种在Android应用中实现自定义ListView的方法,通过创建自定义Adapter和View来显示带有复选框和文本的数据项,并提供了在Activity中设置ListView及获取选中项的示例代码。
248

被折叠的 条评论
为什么被折叠?



