Android 使用从SharedReferences键获取的文本将TextView添加到LinearLayout
我正在尝试从SharedReferences对象创建密钥列表。因为用户可以添加新的pairs键值,所以我需要动态地这样做 以下是我的活动的onCreate函数:Android 使用从SharedReferences键获取的文本将TextView添加到LinearLayout,android,view,android-linearlayout,Android,View,Android Linearlayout,我正在尝试从SharedReferences对象创建密钥列表。因为用户可以添加新的pairs键值,所以我需要动态地这样做 以下是我的活动的onCreate函数: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater inflater = this.getLayoutInflater(); Linear
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.activity_show_saved, null);
this.add_here = (LinearLayout)layout.findViewById(R.id.saved_list);
// Loading shared preferences
SharedPreferences sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
Map<String, ?> kv = sp.getAll();
int i = 0;
// Iterating through shared preferences
for(Map.Entry<String, ?> entry : kv.entrySet()) {
Toast.makeText(this, entry.getKey(), Toast.LENGTH_SHORT).show();
TextView to_add = new TextView(this);
to_add.setText(entry.getKey());
to_add.setId(i++);
this.add_here.addView(to_add, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
this.setContentView(layout);
}
不幸的是,我的屏幕是空白的。问题在于添加文本视图,因为while中的Toast正确地显示了保存的键,正如预期的那样。我刚刚完成了类似的动态列表,但我的文本视图是独立布局的一部分。 也许这会对你有好处 此代码适用于我:
final ScrollView ll = (ScrollView) getLayoutInflater().inflate(
R.layout.dropdown_list, null);
final LinearLayout container = ((LinearLayout) ll
.findViewById(R.id.dropdown_list_container));
if (myObjectsCount > 0) {
for (final MyObject ci : myObjects()) {
LinearLayout item = ((LinearLayout) getLayoutInflater()
.inflate(R.layout.list_item, null));
TextView tv = ((TextView) item
.findViewById(R.id.list_item_text));
tv.setText("your text here");
container.addView(item);
}
}
以及文本视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/buttons"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/list_item_text"
style="@android:style/Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical" />
</LinearLayout>
正如有人在评论中指出的那样,这不是正确的做法。下面是使用ListView获得的结果。看一看
public class ShowSaved extends Activity implements OnItemClickListener {
// Layout
private ListView add_here;
private TextView empty_view;
// String Array
private ArrayList<String> string = new ArrayList<String>();
// SharedPreferences object and editor
private SharedPreferences sp;
// Array adapter for ListView add_here
private ArrayAdapter<String> saved;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_show_saved);
this.add_here = (ListView)findViewById(R.id.saved_list);
this.empty_view = (TextView)findViewById(R.id.empty_text);
// Loading shared preferences and editor
this.sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
// Creating the Adapter with all strings in an array
this.saved = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, string);
this.saved.setNotifyOnChange(true);
// Retrieving all pairs in SharedPreferences sp
Map<String, ?> kv = sp.getAll();
// Iterating through shared preferences
for(Map.Entry<String, ?> entry : kv.entrySet()) {
saved.add(entry.getKey());
}
// Setting Adapter to ListView and empty View
this.add_here.setEmptyView(empty_view);
this.add_here.setAdapter(this.saved);
// Setting Listener for ListView add_here
this.add_here.setOnItemClickListener(this);
}
public类ShowSaved扩展活动实现了mclicklinklistener{
//布局
私有ListView在此处添加_;
私有文本视图-空视图;
//字符串数组
私有ArrayList字符串=新ArrayList();
//SharedReferences对象和编辑器
私人共享参考sp;
//ListView的数组适配器在此处添加
已保存私有阵列适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
此.setContentView(R.layout.activity\u show\u已保存);
this.add_here=(ListView)findViewById(R.id.saved_list);
this.empty_view=(TextView)findViewById(R.id.empty_text);
//加载共享首选项和编辑器
this.sp=this.getSharedReferences(“保存的序列”,模式为私有);
//使用数组中的所有字符串创建适配器
this.saved=newarrayadapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,string);
this.saved.setNotifyOnChange(true);
//正在检索SharedReferences sp中的所有对
Map kv=sp.getAll();
//遍历共享首选项
对于(Map.Entry:kv.entrySet()){
saved.add(entry.getKey());
}
//将适配器设置为ListView和空视图
this.add_here.setEmptyView(empty_视图);
this.add_here.setAdapter(this.saved);
//设置ListView的侦听器在此处添加
this.add_here.setOnItemClickListener(this);
}
注意:此代码无法自行编译,因为它缺少OnItemClickListener接口所需的onItemClick函数。问题:为什么不使用
ListView
?您导入的是什么LayoutParams类?@Sam,我导入的是android.widget.LinearLayout.LayoutParams
。我会尝试一下ListView,但我想为了理解为什么这不起作用。在循环之前调用setContentView
是否有任何效果?
public class ShowSaved extends Activity implements OnItemClickListener {
// Layout
private ListView add_here;
private TextView empty_view;
// String Array
private ArrayList<String> string = new ArrayList<String>();
// SharedPreferences object and editor
private SharedPreferences sp;
// Array adapter for ListView add_here
private ArrayAdapter<String> saved;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_show_saved);
this.add_here = (ListView)findViewById(R.id.saved_list);
this.empty_view = (TextView)findViewById(R.id.empty_text);
// Loading shared preferences and editor
this.sp = this.getSharedPreferences("saved_sequences", MODE_PRIVATE);
// Creating the Adapter with all strings in an array
this.saved = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, string);
this.saved.setNotifyOnChange(true);
// Retrieving all pairs in SharedPreferences sp
Map<String, ?> kv = sp.getAll();
// Iterating through shared preferences
for(Map.Entry<String, ?> entry : kv.entrySet()) {
saved.add(entry.getKey());
}
// Setting Adapter to ListView and empty View
this.add_here.setEmptyView(empty_view);
this.add_here.setAdapter(this.saved);
// Setting Listener for ListView add_here
this.add_here.setOnItemClickListener(this);
}