如何在ListView单选中预选值?实际上我正在创建应用程序,用户必须选择他的工作类型,如农民等。
在我的数据库中,用户通过此列表视图选择了农民我给他选择更改是类型,这就是为什么我必须先显示他先选择的内容。
这是我的代码。
public class UserTypeSettings extends AppCompatActivity {
private List<UserTypePrivacySettings> userTypePrivacySettingsList;
private ListView userTypeListView;
private int listpos;
public int getListpos() {
return listpos;
}
public void setListpos(int listpos) {
this.listpos = listpos;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_type_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setActionbar();
initViews();
}
private void initViews() {
userTypeListView = (ListView) findViewById(R.id.userTypeListView);
userTypeListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
userTypePrivacySettingsList = new Parse(UserTypeSettings.this).parseUserType();
userTypeListView.setAdapter(new SingleChoiceArrayadapter(this, userTypePrivacySettingsList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
setListpos(position);
return super.getView(position, convertView, parent);
}
});
userTypeListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
userTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("check user stype", userTypePrivacySettingsList.get(position).getUser_type());
}
});
}}
这是我的适配器类
public class SingleChoiceArrayadapter extends ArrayAdapter<UserTypePrivacySettings> {
public SingleChoiceArrayadapter(Context context, List<UserTypePrivacySettings> usertypes) {
super(context, 0, usertypes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_single_choice, parent, false);
}
UserTypePrivacySettings user = getItem(position);
final CheckedTextView textView = (CheckedTextView) super.getView(position, convertView, parent);
textView.setText(user.getUser_type());
textView.setTextColor(getResources().getColor(R.color.kisanColorGreen));
return convertView;
}
@Override
public long getItemId(int position) {
return getItem(position).getUser_type().hashCode();
}
@Override
public boolean hasStableIds() {
return true;
}
}
答案 0 :(得分:0)
以下是我在简单适配器上设置预选项目的方法。但它应该像你现在一样工作。如果没有让我知道。
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice, android.R.id.text1, arrayList);
int selectedPosition;
userTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedPosition = position;
}
});
userTypeListView.performItemClick(userTypeListView.getAdapter().getView(position, null, null), position, position);