单击按钮时,我需要从网格视图中获取复选框状态
我尝试了很多东西,但状态总是“假”
这是我的代码
这是适配器
public class CustomSuggestFriends extends ArrayAdapter<Items_FriendsRequest> {
Context context;
dbManage objDB;
Items_FriendsRequest Items_SuggestFriends;
List<Items_FriendsRequest>items;
int Position;
SharedPreferences SharedP;
String user_id="1002", secret_id = "2143054018";
String u_id="1025", ut_ = "1";
public CustomSuggestFriends(Context context, int textViewResourceId,
List<Items_FriendsRequest> objects) {
super(context, textViewResourceId, objects);
this.context = context;
}
private class viewHolder {
private ImageView userImage;
private TextView userName;
private CheckBox checkbox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolder holder = null;
ImageLoader_Crop imageLoader_Crop;
Items_SuggestFriends = getItem(position);
Position = position;
objDB = new dbManage(getContext());
items = objDB.select_SuggestFriends();
objDB.CloseDataBase();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
imageLoader_Crop = new ImageLoader_Crop(context.getApplicationContext());
if (convertView == null) {
convertView = inflater.inflate(R.layout.items_suggestdriends, null);
holder = new viewHolder();
holder.userImage = (ImageView) convertView
.findViewById(R.id.Items_SuggestFriends_userImage);
holder.userName = (TextView) convertView
.findViewById(R.id.Items_SuggestFriends_NameTXT);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.Items_SuggestFriends_checkBox);
holder.checkbox.setTag(position);
holder.checkbox.setChecked(items.get(position).isSelected());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
items.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
Log.v("Changed",items.get(getPosition).getId_()+"");
}
});
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.userName.setText(Items_SuggestFriends.getName_());
String SuggestF = Items_SuggestFriends.getSuggestFriendsSEND();
if (SuggestF.equals("0")) {
holder.checkbox.setVisibility(View.VISIBLE);
} else if (SuggestF.equals("1")) {
holder.checkbox.setVisibility(View.GONE);
}
String imageURL = "";
imageURL = functionspackage.Constants.server_file + "1/s/"
+ Items_SuggestFriends.getId_() + "."
+ Items_SuggestFriends.getRand_() + ".jpg";
holder.userImage.setTag(imageURL);
imageLoader_Crop.DisplayImage(imageURL, context, holder.userImage);
return convertView;
}
}
这是我的课,它有一个名为发送的按钮,在这个按钮中我需要获取被检查内容的 ID
public class SuggestFriends extends Activity {
int count;
dbManage objDB;
SharedPreferences SharedP;
String user_id, secret_id = "";
public static String u_id, ut_ = "";
Button send, cancel;
List<Items_FriendsRequest> SuggestFriendsItems;
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.displayphoto_from_album);
initialise_View();
在这里我需要获取值,但它总是错误的!!!!
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
count = gridView.getCount();
objDB = new dbManage(SuggestFriends.this);
SuggestFriendsItems = objDB.select_SuggestFriends();
objDB.CloseDataBase();
SparseBooleanArray sparseBooleanArray = gridView
.getCheckedItemPositions();
// add the id of the ones that been checked . . to string with
// (,)
for (int i = 0; i < count; i++) {
if(sparseBooleanArray.valueAt(i) == true) {
Log.e(sparseBooleanArray.get(i)+"",SuggestFriendsItems.get(i).getId_()+"");
} else if (!sparseBooleanArray.get(i)) {
Log.e(sparseBooleanArray.get(i) +"",SuggestFriendsItems.get(i).getId_()+"");
gridView.getItemAtPosition(i);
}
}
}
});
SharedP = getSharedPreferences(functionspackage.Constants.SharedP_name,
0);
user_id = SharedP.getString(functionspackage.Constants.SharedP_user_id,
null);
secret_id = SharedP.getString(
functionspackage.Constants.SharedP_secret_id, null);
if (getIntent().hasExtra(functionspackage.Constants.Extra_Uid)) {
u_id = getIntent().getStringExtra(
functionspackage.Constants.Extra_Uid);
}
if (getIntent().hasExtra(functionspackage.Constants.Extra_ut)) {
ut_ = getIntent().getStringExtra(
functionspackage.Constants.Extra_ut);
}
SuggestFriends_AsyncTask Async = new SuggestFriends_AsyncTask();
Async.execute(user_id, secret_id, u_id, ut_);
}
// ///////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////
String response = "";
class SuggestFriends_AsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
response = "";
response = functionspackage.methodes.HTTP_fileInf_OPhotos(
functionspackage.Constants.server_Web_Profiles
+ "/suggest/" + params[3] + "/" + params[2],
params[0], params[1], 0);
functionspackage.methodes.install_JSON_SuggestFriends(
SuggestFriends.this, response);
objDB = new dbManage(SuggestFriends.this);
SuggestFriendsItems = objDB.select_SuggestFriends();
objDB.CloseDataBase();
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
CustomSuggestFriends adapterSuggestFriends = new CustomSuggestFriends(
SuggestFriends.this, R.layout.items_suggestdriends,
SuggestFriendsItems);
gridView.setAdapter(adapterSuggestFriends);
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
}
}
// ///////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////
private void initialise_View() {
gridView = (GridView) findViewById(R.id.DisplayPhoto_gridView);
gridView.setNumColumns(3);
gridView.setBackgroundColor(Color.BLACK);
send = (Button) findViewById(R.id.SendSuggestBotton);
cancel = (Button) findViewById(R.id.CancelsuggestFrindes);
}
}
这是日志