我正在创建一个Android应用程序,我有一个片段,作为textview和一个按钮。当您单击该按钮时,您将进入另一个活动,这是一个填充它的表单,其中有一个添加按钮。单击添加按钮时,数据将保存到解析,现在问题是我想要检索数据并将其填充到listview并在片段中查看它。请帮忙。
答案 0 :(得分:2)
使用Arraylist和适配器,
您的代码应如下所示:
public class YourActivity extends Activity {
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this,
// you already have yours).
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
}
}