import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.parse.ParseRelation;
import com.parse.ParseUser;
public class FriendsListAdapter extends BaseAdapter{
ViewHolder holder;
Context context;
List<FriendsList> friendsList;
ParseUser user = ParseUser.getCurrentUser();
ParseRelation<ParseUser> friendsRelation;
public FriendsListAdapter(Context context) {
this.context = context;
friendsList = new ArrayList<FriendsList>();
friendsRelation = user.getRelation("Friends");
}
public void add(FriendsList object,int position){
friendsList.add(position,object);
}
public void remove(int position){
friendsList.remove(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return friendsList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return friendsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(v == null){
v = mInflater.inflate(R.layout.friends_list_item,null);
holder = new ViewHolder(v);
v.setTag(holder);
}else{
holder = (ViewHolder)v.getTag();
}
Typeface roboto = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
holder.listText.setTypeface(roboto);
holder.listText.setText(friendsList.get(position).username);
Glide.with(context).load(friendsList.get(position).profilePicture).into(holder.profile_picture);
return v;
}
static class ViewHolder{
TextView listText;
CheckedTextView friendsBox;
ImageView profile_picture;
ViewHolder(View v){
listText = (TextView)v.findViewById(R.id.friend_username);
friendsBox = (CheckedTextView)v.findViewById(R.id.isFriend);
profile_picture = (ImageView)v.findViewById(R.id.friendsProfilePicture);
}
}
public void Toast(String text,int length){
Toast.makeText(context, text, length).show();
}
}
我希望能够在listview适配器中使用setItemChecked方法,这样我可以解析一些数据并根据布尔值检查框。如何在我的适配器中实现它?
以下是我尝试做的一个例子。
friendsList = (ListView)findViewById(R.id.friendsList);
friendsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
friendsList.setItemChecked(0, true);
friendsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedTextView cv =(CheckedTextView)view.findViewById(R.id.isFriend);
cv.toggle();
}
});
答案 0 :(得分:0)
getView()
的{{1}}方法:
Adapter
您也可以使用OnClickListener而不是OnCheckedChangeListener:
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.product_checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The checkbox is checked, then set the list item as selected
((ListView) parent).setItemChecked(position, true);
} else {
// The checkbox is unchecked, then set the list item as unselected
((ListView) parent).setItemChecked(position, false);
}
}
});
请务必在checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the checkbox state
boolean isChecked = checkBox.isChecked();
if (isChecked) {
// The checkbox is checked, then set the list item as selected
((ListView) parent).setItemChecked(position, true);
} else {
// The checkbox is unchecked, then set the list item as unselected
((ListView) parent).setItemChecked(position, false);
}
}
});
方法中将final
和int position
变量设置为ViewGroup parent
。