我有一个从外部 xml 响应中获取的字符串数组。该字符串数组可以在 ListView 中显示。我想附加一个带有新 xml 数据的字符串数组,并在每次用户到达 ListView 中字符串数组的末尾元素时将其添加到 ListView 中。我怎样才能做到这一点?我对这个主题进行了很多研究。我几乎找不到与此上下文相关的任何示例。任何帮助都感激不尽。谢谢你。
ArrayList<String> get_msgList =MsgHandler.getMsgsList();
String[] msgList=new String[get_msgList.size()];
msgList=get_msgList.toArray(msgList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, get_msgList );
myListView.setAdapter(adapter);
这就是我获取字符串数组并将其添加到 ListView 的方式。
最佳答案
-- 更新,在查看更多文档后我认为你仍然可以使用 ArrayAdapter --
因此,如果您查看 listview 的文档你看
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>
</resources>
并加载您使用的那些:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));
这里的问题是您必须处理弄清楚它们何时到达列表末尾以及如何在不阻塞 UI 的情况下加载更多数据。
因此,为了让自己省去很多麻烦,我建议使用 Commonsware Endless Adapter 这样做:
import android.app.ListActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.commonsware.cwac.endless.EndlessAdapter;
import java.util.ArrayList;
public class YourListActivity extends ListActivity {
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new YourAdapter(countries));
}
class YourAdapter extends EndlessAdapter {
public YourAdapter(String[] list) {
super(new ArrayAdapter<String>(YourAdapter.this,
R.layout.row,
android.R.id.text1,
list));
}
@Override
protected View getPendingView(ViewGroup parent) {
View row=getLayoutInflater().inflate(R.layout.row, null);
return row.findViewById(android.R.id.text1);
}
@Override
protected boolean cacheInBackground() {
SystemClock.sleep(10000); // pretend to do work
//put logic here to fetch the data
//this happens in the background so you can't do anything with the UI
//return whether or not you have more data, true if you do false otherwise.
boolean haveMoreData = true;//this up to you to figure out
return haveMoreData;
}
@Override
protected void appendCachedData() {
//here is where you do the work to actually add the data obtained in cacheInBackground to the ArrayAdapter
ArrayAdapter<String> a=(ArrayAdapter<String>)getWrappedAdapter();
for (int i=0;i<25;i++) {
a.add(a.getCount());
}
}
}
}
}
这是另一个看起来与您自己的问题相似的问题: Android Endless List
-- 更新:如何将多个字符串数组添加到适配器 -- 有多种方法可以做到这一点,最简单的方法可能是使用适配器添加方法..
看看这个:http://developer.android.com/reference/android/widget/ArrayAdapter.html 并查看添加方法。 当前您有一个名为 adapter 的变量:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, get_msgList);
每当您想向该适配器添加更多数据时,只需调用:
adapter.add("Some new data");
因此,如果您有另一个 String[] 数据数组,并且想将所有数据添加到当前适配器,只需使用:
String[] someNewData = getStringArray();//no idea how you are getting the array, this is just an example
for (String s : someNewData) {
adapter.add(s);
}
添加新数据后,您可能需要调用 adapter.notifyDataSetChanged 但我不确定(它可能会发生在您身上)。我会在没有它的情况下先尝试一下,看看是否一切正常,如果不添加它。
现在您可能想要做的一件事是使适配器变量成为类的实例变量,以便您可以在类中的任何地方使用它。
关于android - 将动态字符串数组添加到android中的动态 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9448483/