大佬教程收集整理的这篇文章主要介绍了android – 如何使用复选框和自定义适配器从列表视图中获取选定的列表项?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的xml文件中,我的底部有一个Button.我想要的是让用户选择ListView中的行数,并且当他/她点击Button获取所选项目的位置,以便我可以获取特定行的对象进行进一步的计算.
package com.windrealm.android; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.onClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.button; import android.widget.checkBox; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class PlanetsActivity extends Activity { private ListView mainListView; private Planet[] planets; private ArrayAdapter<Planet> listadapter; private Button check; private Context context; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.main); context = PlanetsActivity.this; check = (Button) findViewById(R.id.check); // Find the ListView resource. mainListView = (ListView) findViewById(R.id.mainListView); // When item is tapped,toggle checked properties of checkBox and // Planet. mainListView .setOnItemClickListener(new AdapterView.onItemClickListener() { @Override public void onItemClick(AdapterView<?> parent,View item,int position,long id) { Planet planet = listadapter.getItem(position); planet.togglechecked(); PlanetViewHolder viewHolder = (PlanetViewHolder) item .getTag(); viewHolder.getcheckBox().setchecked(planet.ischecked()); } }); // Create and populate planets. planets = (Planet[]) getLastNonConfigurationInstance(); if (planets == null) { planets = new Planet[] { new Planet("Mercury"),new Planet("Venus"),new Planet("Earth"),new Planet("Mars"),new Planet("Jupiter"),new Planet("Saturn"),new Planet("Uranus"),new Planet("Neptune"),new Planet("Ceres"),new Planet("Pluto"),new Planet("Haumea"),new Planet("Makemake"),new Planet("Eris") }; } ArrayList<Planet> planetList = new ArrayList<Planet>(); planetList.addAll(Arrays.asList(planets)); // Set our custom array adapter as the ListView's adapter. listadapter = new PlanetArrayAdapter(this,planetList); mainListView.setAdapter(listadapter); check.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { for (int i = 0; i < listadapter.getCount(); i++) { Planet planet = listadapter.getItem(i); if (planet.ischecked()) { Toast.makeText(context,planet.getName() + " is checked!!",Toast.LENGTH_SHORT).show(); } } } }); } /** Holds planet data. */ private static class Planet { private String name = ""; private Boolean checked = false; public Planet() { } public Planet(String Name) { this.name = name; } public Planet(String name,Boolean checked) { this.name = name; this.checked = checked; } public String getName() { return name; } public void setName(String Name) { this.name = name; } public Boolean ischecked() { return checked; } public void setchecked(Boolean checked) { this.checked = checked; } public String toString() { return name; } public void togglechecked() { checked = !checked; } } /** Holds child views for one row. */ private static class PlanetViewHolder { private checkBox checkBox; private TextView textView; public PlanetViewHolder() { } public PlanetViewHolder(TextView textView,checkBox checkBox) { this.checkBox = checkBox; this.textView = textView; } public checkBox getcheckBox() { return checkBox; } public void setcheckBox(checkBox checkBox) { this.checkBox = checkBox; } public TextView getTextView() { return textView; } public void setTextView(TextView textView) { this.textView = textView; } } /** Custom adapter for displaying an array of Planet objects. */ private static class PlanetArrayAdapter extends ArrayAdapter<Planet> { private LayoutInflater inflater; public PlanetArrayAdapter(Context context,List<Planet> planetList) { super(context,R.layout.simplerow,R.id.rowTextView,planetList); // Cache the LayoutInflate to avoid asking for a new one each time. inflater = LayoutInflater.from(context); } @Override public View getView(int position,View convertView,ViewGroup parent) { // Planet to display Planet planet = (Planet) this.getItem(position); // The child views in each row. checkBox checkBox; TextView textView; // Create a new row view if (convertView == null) { convertView = inflater.inflate(R.layout.simplerow,null); // Find the child views. textView = (TextView) convertView .findViewById(R.id.rowTextView); checkBox = (checkBox) convertView.findViewById(R.id.checkBox01); // Optimization: Tag the row with it's child views,so we don't // have to // call findViewById() later when we reuse the row. convertView.setTag(new PlanetViewHolder(textView,checkBox)); // If checkBox is toggled,update the planet it is tagged with. checkBox.setOnClickListener(new View.onClickListener() { public void onClick(View v) { checkBox cb = (checkBox) v; Planet planet = (Planet) cb.getTag(); planet.setchecked(cb.ischecked()); } }); } // Reuse exisTing row view else { // Because we use a ViewHolder,we avoid having to call // findViewById(). PlanetViewHolder viewHolder = (PlanetViewHolder) convertView .getTag(); checkBox = viewHolder.getcheckBox(); textView = viewHolder.getTextView(); } // Tag the checkBox with the Planet it is displaying,so that we can // access the planet in onClick() when the checkBox is toggled. checkBox.setTag(planet); // Display planet data checkBox.setchecked(planet.ischecked()); textView.setText(planet.getName()); return convertView; } } public Object onRetainNonConfigurationInstance() { return planets; } }
以上是大佬教程为你收集整理的android – 如何使用复选框和自定义适配器从列表视图中获取选定的列表项?全部内容,希望文章能够帮你解决android – 如何使用复选框和自定义适配器从列表视图中获取选定的列表项?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。