【发布时间】:2014-06-24 08:44:54
【问题描述】:
我在栏中有一个带有刷新按钮的活动。 在这个活动中,有一个片段调用 asyncTask 以获取 json 格式的数据。 第一次创建活动时,工作正常,列表视图显示一切正常。
当我按下活动栏中的刷新按钮时,我在活动中调用片段方法“myOnResume”,该方法再次调用相同的异步任务。
我检查了task返回的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);
}
}
Vector的类是:
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>
(listView是自定义的Listview)
提前谢谢你!
【问题讨论】:
-
我的代码中没有使用 Base 适配器
-
可以尝试添加vectorGrupos=new Vector
();这是全局的,在 showListaGrupos 内部清除它然后赋值 -
@SreejithSP 完全不一样
-
我很难过..我能找到的唯一其他问题是 getView 方法缺少 @Override。并检查本教程..vogella.com/tutorials/AndroidListView/article.html#arrayAdapter 抱歉不能提供更多帮助
标签: android listview android-fragments android-listview adapter