我有一个listView
有 3 个textViews
字段和一个check box
in ever row.<br
> 如果选择或选中一个特定checkBox
的,那么如何访问与textViews
选中对应的该行中的详细信息(或相应的值)check box
?
问问题
1694 次
3 回答
1
这个解决方案对我有用。
在 BaseAdapter 类下的 Your View getView() 中添加以下代码。
public static boolean checked=false;
holder.your_checkBox.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
if(cb.isChecked())
{
checked=true;
String selected_txt = object_of_ArrayList.get(position).get(ArrayList.Value);
// For Example//
String name=friendList.get(position).get(FriendList.FRIEND_NAME);
}
}
});
如果你想在另一个活动中使用这个项目文本,那么你想让字符串像这样静态
public static String name;
如果您想在另一个活动中访问此名称
String str_name=your_adapterName.string;
于 2015-08-18T10:06:43.033 回答
0
You have to save the checkbox state it is checked or not maintained in boolean array .
//define your custom adapter
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
{
// boolean array for storing
//the state of each CheckBox
boolean[] checkBoxState;
ViewHolder viewHolder;
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, Object>> players) {
//let android do the initializing :)
super(context, textViewResourceId, players);
//create the boolean array with
//initial state as false
checkBoxState=new boolean[players.size()];
}
//class for caching the views in a row
private class ViewHolder
{
ImageView photo;
TextView name,team;
CheckBox checkBox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=inflater.inflate(R.layout.players_layout, null);
viewHolder=new ViewHolder();
//cache the views
viewHolder.photo=(ImageView) convertView.findViewById(R.id.photo);
viewHolder.name=(TextView) convertView.findViewById(R.id.name);
viewHolder.team=(TextView) convertView.findViewById(R.id.team);
viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox);
//link the cached views to the convertview
convertView.setTag( viewHolder);
}
else
viewHolder=(ViewHolder) convertView.getTag();
int photoId=(Integer) players.get(position).get("photo");
//VITAL PART!!! Set the state of the
//CheckBox using the boolean array
viewHolder.checkBox.setChecked(checkBoxState[position]);
//for managing the state of the boolean
//array according to the state of the
//CheckBox
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked())
checkBoxState[position]=true;
//set the data to be displayed
viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId));
viewHolder.name.setText(players.get(position).get("name").toString());
viewHolder.team.setText(players.get(position).get("team").toString());
else
checkBoxState[position]=false;
}
});
//return the view to be displayed
return convertView;
}
}
the state of the CheckBox is set to its correct state using the boolean array.
When the user clicks on a CheckBox,the resulting state of that CheckBox is cached in the boolean array.This cached state is the one that is used to set the CheckBox to its correct state
public class CustomListViewWithCheckBox extends ListActivity {
//ArrayList that will hold the original Data
ArrayList<HashMap<String, Object>> players;
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the LayoutInflater for inflating the customomView
//this will be used in the custom adapter
inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//these arrays are just the data that
//I'll be using to populate the ArrayList
//You can use our own methods to get the data
String names[]={"Sachin","Ricky","Lara","Dravid",
"Dale Steyn"};
String teams[]={"India","Aus","West indies",
"India","South Africa"};
Integer[] photos={R.drawable.sachin,R.drawable.ricky,
R.drawable.lara,R.drawable.dravid,
R.drawable.dale_steyn};
players=new ArrayList<HashMap<String,Object>>();
//temporary HashMap for populating the
//Items in the ListView
HashMap<String , Object> temp;
//total number of rows in the ListView
int noOfPlayers=names.length;
//now populate the ArrayList players
for(int i=0;i<noOfPlayers;i++)
{
temp=new HashMap<String, Object>();
temp.put("name", names[i]);
temp.put("team", teams[i]);
temp.put("photo", photos[i]);
//add the row to the ArrayList
players.add(temp);
}
/*create the adapter
*first param-the context
*second param-the id of the layout file
you will be using to fill a row
*third param-the set of values that
will populate the ListView */
final CustomAdapter adapter=new CustomAdapter(this, R.layout.players_layout,players);
//finally,set the adapter to the default ListView
setListAdapter(adapter);
}
于 2013-05-29T07:29:38.333 回答
0
你可以这样使用
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class AndroidListViewActivity extends Activity {
ListView myList;
Button getChoice;
String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView)findViewById(R.id.list);
getChoice = (Button)findViewById(R.id.getchoice);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
listContent);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String selected = "";
int cntChoice = myList.getCount();
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++){
if(sparseBooleanArray.get(i)) {
selected += myList.getItemAtPosition(i).toString() + "\n";
}
}
Toast.makeText(AndroidListViewActivity.this,
selected,
Toast.LENGTH_LONG).show();
}});
}
}
于 2013-05-29T07:18:52.893 回答