我有一個活動與刷新按鈕吧。 此活動內有一個片段,它調用asyncTask以獲取json格式的數據。 第一次創建活動,工作正常,listview顯示一切正常。ListView不更新與更新的適配器
當我在活動欄中按下刷新按鈕時,我將活動調用到片段方法「myOnResume」,該方法再次調用相同的異步任務。
我檢查了由任務返回的json是否已更新,但listview不更新(使用與第一次'showListaGrupos'相同的方法)。
我試圖添加句子「adapter.notifyDataSetChanged();」但仍然是問題。 Listview不會更新,並顯示與第一次創建活動時相同的內容。
你能幫我嗎?
片段代碼:
public class GruposFragment extends Fragment {
View rootView;
private Vector<GruposClass> vectorGrupos;
private ListView lvShowGroups;
public GruposFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_grupos, container, false);
ActionBar ab = this.getActivity().getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
lvShowGroups = (ListView)rootView.findViewById(R.id.lvGroups);
String id="1";
GruposTask getGrupos = new GruposTask(GruposFragment.this, getActivity());
getGrupos.execute(id);
return rootView;
}
//called from onPostExecute
public void showListaGrupos(String result) {
vectorGrupos=new Vector<GruposClass>();
//create the vector with json returned.
for(...){
vectorGrupos.add(new GruposClass(x,y,z...));
}
if(adapter==null){
adapter = new adapterGroupsClasses(getActivity());
}else{
adapter.changeListVals();
}
adapter.notifyDataSetChanged();
lvShowGroups.setAdapter(adapter);
lvShowGroups.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
...
}
});
}
class adapterGroupsClasses extends ArrayAdapter<GruposClass> {
Activity context;
adapterGroupsClasses(Activity context) {
super(context, R.layout.listitem_groupsline, vectorGrupos);
this.context = context;
}
public void changeListVals(){
this.clear();
this.addAll(vectorGrupos);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View item = convertView;
ViewHolder holder;
if(item == null)
{
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.listitem_groupsline, null);
holder = new ViewHolder();
holder.id = (TextView)item.findViewById(R.id.TvIdGroup);
holder.nombre = (TextView)item.findViewById(R.id.TvName);
holder.descripcion = (TextView)item.findViewById(R.id.TvDescription);
item.setTag(holder);
}
else
{
holder = (ViewHolder)item.getTag();
}
holder.id.setText(vectorGrupos.get(position).getId());
holder.nombre.setText(vectorGrupos.get(position).getNombre());
holder.descripcion.setText(vectorGrupos.get(position).getDescripcion());
return(item);
}
}
static class ViewHolder {
TextView id;
TextView nombre;
TextView descripcion;
}
//called from Activity bar button refresh
public void myOnResume() {
GruposTask getGrupos = new GruposTask(GruposFragment.this, getActivity());
getGrupos.execute(id);
}
}
類的載體的是:
public class GruposClass {
private String id;
private String nombre;
private String descripcion;
public GruposClass(String i, String n, String d){
id=i;
nombre = n;
descripcion = d;
}
public String getNombre(){
return nombre;
}
public String getDescripcion(){
return descripcion;
}
public String getId(){
return id;
}
}
,活性是:
public class Grupos extends FragmentActivity {
GruposFragment mainFragment;
Bundle instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
instance=savedInstanceState;
super.onCreate(instance);
setContentView(R.layout.activity_grupos);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new GruposFragment()).commit();
}
mainFragment = (GruposFragment)getFragmentManager().findFragmentById(R.id.listGroup);
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
createMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.grupos, menu);
return true;
}
private void createMenu(Menu menu) {
MenuItem itemRefresh = menu.add(0,1,1,"refresh");
{
itemRefresh.setIcon(R.drawable.refresh);
itemRefresh.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (id == 1) {
mainFragment.myOnResume();
return true;
}
return super.onOptionsItemSelected(item);
}
}
碎片的佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/textViewGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/cabeceraGrupos" />
<ListView android:id="@+id/lvGroups"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
(該列表視圖是自定義列表視圖)
預先感謝您!
[刷新按鈕的點擊列表]的可能重複(http://stackoverflow.com/questions/24379848/refresh-list-on-click-of-button) –
我不在我的代碼中使用Base適配器 – carlosdiazp
您可以嘗試添加vectorGrupos = new Vector();這是全球和內部showListaGrupos清除它,然後分配值 –
Android