0

我有一个使用自定义数组适配器的列表视图,它加载带有文本+单选按钮的布局。用户一次只能选择列表中的一个项目 - 所以基本上任何时候都应该“选中”一个单选按钮。除非重新创建活动(例如旋转屏幕),否则此方法非常有效。我不明白它为什么会这样,所以也许你们都可以想到一些东西?

这是具有列表视图的活动的代码:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.widget.AdapterView;

public class TimeForm extends Activity implements AdapterView.OnItemClickListener {

    private MyArrayAdapter maa;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_form);
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);

        ListView lv = (ListView) findViewById(R.id.listView1);
        lv.setItemsCanFocus(false);

        String listitems[] = new String[16];

        listitems[0] = "Other";

        for(int i = 1; i < 16; i++)
        {
            listitems[i] = "Job " + i;
        }

        maa = new MyArrayAdapter(this, R.layout.layout_list, listitems);

        if(savedInstanceState != null)
            maa.selIndex = savedInstanceState.getInt("selIndex");

        lv.setAdapter(maa);
        lv.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        EditText et = (EditText) this.findViewById(R.id.editText1);

        if(position == 0)
        {
            et.setVisibility(View.VISIBLE);
        } else {
            et.setVisibility(View.GONE);
        }

        if(maa.selItem != null)
        {
            RadioButton rOld = (RadioButton) maa.selItem.findViewById(R.id.radioButton1);
            rOld.setChecked(false);
        }

        RadioButton r = (RadioButton) view.findViewById(R.id.radioButton1);
        r.setChecked(true);

        maa.selIndex = position;
        maa.selItem = view;
    }

    public void onSaveInstanceState(Bundle b)
    {
        b.putInt("selIndex", maa.selIndex);

        super.onSaveInstanceState(b);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_time_form, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

MyArrayAdapter 类的代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<String> {

    private Context context;
    private String[] strings;
    private int layoutID;

    public int selIndex;
    public View selItem;

    public MyArrayAdapter(Context c, int id, String[] values)
    {
        super(c, id, values);

        selIndex = -1;
        selItem = null;

        this.strings = values;
        this.context = c;
        layoutID = id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rView = inflater.inflate(layoutID, parent, false);

        TextView tv = (TextView) rView.findViewById(R.id.textView1);
        tv.setText(strings[position]);

        if(position == selIndex)
        {
            RadioButton r = (RadioButton) rView.findViewById(R.id.radioButton1);
            r.setChecked(true);
            selItem = rView;
        }


        return rView;
    }


}
4

2 回答 2

1

嗯,首先,关于“重新创建”视图。查看http://developer.android.com/guide/topics/resources/runtime-changes.html

例如,您可以定义public void onConfigurationChanged(Configuration newConfig)以确保您的活动不会在轮换时重新创建。

也就是说,如果您想保留这些值,则必须实际执行此操作,然后将任何选择应用于传递给 ArrayAdapter 的集合。

于 2013-01-25T00:42:31.350 回答
0

我想到了。我不确定它是否是最好的解决方案,但它可以工作并且不会导致任何明显的性能损失。^_^

在 TimeForm 活动的 onItemClick() 方法中,我在底部添加了这个:

ListView lv = (ListView) findViewById(R.id.listView1);
lv.invalidateViews();

这会强制列表视图重绘所有子视图。感谢所有花时间帮助我解决这个问题的人。

于 2013-01-25T18:24:15.267 回答