活动启动时SetChecked RadioButton的SingleChoice ListView

时间:2015-04-27 16:47:41

标签: java android listview android-listview

我一直致力于创建一个由自定义ArrayAdapter填充的单一选项ListView(可行),除了我需要在活动启动时将ListView中的预定RadioButton设置为setChecked(true)

我使用List<Server>服务器对象填充我的ListView,其中包含一个布尔值&#39; default_server&#39;用于确定哪一行/ RadioButton应为setChecked(true)

在活动正确启动后选择各种ListView项目会根据需要在单一选择模式下将特定的RadioButton标记为setChecked(true)

我的代码:

ServerActivity.java

public class ServersActivity extends FragmentActivity
//FragmentActivity needed to display dialog fragment when ListView item clicked
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.server_list);
    servers = getServers();

    lv = (ListView) findViewById(android.R.id.list);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    adapter = new ServerActivityArrayAdapter(this, R.layout.server_list_item, servers);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // show dialog if not long clicked
            if (!longClicked) {
                lv.setItemChecked(position, true);
                showServerDialog(position);
            }
        }
    });
}

ServerActivityArrayAdapter.java

public class ServerActivityArrayAdapter extends ArrayAdapter<Server> {

private int layout;
private int mSelectedPosition = -1;
private RadioButton mSelectedRB;

private static LayoutInflater inflater;

public ServerActivityArrayAdapter(Context context, int layout, List<Server> servers) {
    super(context, layout, servers);
    ListArrays listArrays = new ListArrays(context);
    this.layout = layout;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private static class ServerViewHolder {
    private TextView textView;
    private RadioButton radioButton;

    public ServerViewHolder() {
        // EMPTY DEFAULT CONSTRUCTOR
    }
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final Server server = this.getItem(position);
    final ServerViewHolder viewHolder;

    if (convertView == null) {
        convertView = inflater.inflate(layout, parent, false);
        viewHolder = new ServerViewHolder();
        viewHolder.textView = (TextView) convertView.findViewById(R.id.tvServerName);
        viewHolder.radioButton = (RadioButton) convertView.findViewById(R.id.rbDefault);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ServerViewHolder) convertView.getTag();
    }

    // TODO: SET THE DEFAULT SERVER

    viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position != mSelectedPosition && mSelectedRB != null) {
                mSelectedRB.setChecked(false);
            }
            mSelectedPosition = position;
            notifyDataSetChanged();
            mSelectedRB = (RadioButton) v;
            server.setDefaultServer(mSelectedRB.isChecked());
        }
    });

    if (mSelectedPosition != position) {
        viewHolder.radioButton.setChecked(false);
    } else {
        viewHolder.radioButton.setChecked(true);
        if (mSelectedRB != null && viewHolder.radioButton != mSelectedRB) {
            mSelectedRB = viewHolder.radioButton;
        }
    }

    viewHolder.textView.setText(server.getName());
    return convertView;
    }
}

同样,我的List<Server>服务器对象正在填充ListView,后续对话框弹出窗口(当单击一行时)正确,并且每行上的RadioButtons当前在单选项模式下运行。我在ServerActivityArrayAdapter的setOnClickListener中使用server.setDefaultServer(mSelectedRB.isChecked());来更新哪个服务器有default_server(true)

到目前为止我在ServerActivityArrayAdapter中尝试的所有内容似乎都打破了RadioButton的Single Choice模式要求。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我认为你应该使用共享偏好。

Save the state of radiobutton

我认为这就是你要找的东西。

答案 1 :(得分:0)

我确实像Mridul建议的那样回到我的项目中实施共享首选项,但解决方案确实不需要它们。

基本上,我更新了ServerActivityArrayAdapter类的构造函数,让我在创建时从ServerActivity类传递默认服务器的ID。此外,我设置了mSelectionPostion = defServer,因此当所有内容都膨胀时,我的ListView中的正确RadioButton会被检查。

public ServerActivityArrayAdapter(Context context, int layout, List<Server> servers, int defServer) {
    super(context, layout, servers);
    ListArrays listArrays = new ListArrays(context);
    this.layout = layout;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mSelectedPosition = defServer;
}