gpt4 book ai didi

java - Android ListView 数据在重新创建时不会保留

转载 作者:行者123 更新时间:2023-12-01 14:53:47 24 4
gpt4 key购买 nike

我有一个 ListView ,它使用自定义数组适配器,该适配器加载带有文本+单选按钮的布局。用户一次只能选择列表中的一项,因此基本上任何时候都只能“选中”一个单选按钮。除非重新创建 Activity 时(例如旋转屏幕时),否则这种方法效果很好。我不明白为什么要这样做,所以也许你们能想到一些办法?

这是具有 ListView 的 Activity 的代码:

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;
}


}

最佳答案

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

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

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

关于java - Android ListView 数据在重新创建时不会保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com