Android:ListView没有刷新notifyDataSetChanged();

时间:2011-12-03 18:44:17

标签: android listview baseadapter

我在主要活动中有一个自定义BaseAdapter和一个添加按钮。该按钮打开一个带有文本框的对话框,您可以通过该方式向列表中添加新元素。问题是列表没有刷新。在onActivityResult()函数中,我打印列表中的元素数量,每次在对话框中点击“确定”时,数字都会增加,所以我知道这只是刷新不起作用。我的BaseAdapter和我的活动:

class ListaOrase extends BaseAdapter{
    private Activity context;
    ArrayList<String> orase;

    public ListaOrase(Activity context){
        this.context=context;
        orase=new ArrayList<String>();
    }
    public void add(String string){
        orase.add(string);
        this.notifyDataSetChanged();
    }

    public View getView (int position, View convertView, ViewGroup list)  {
        View element;
        if (convertView == null)
        {
         LayoutInflater inflater = context.getLayoutInflater();
         element = inflater.inflate(R.layout.lista, null);
        }
        else element = convertView;
        TextView elementLista=(TextView)element.findViewById(R.id.elementLista);    
        elementLista.setText(orase.get(position));
        return element;
    }

}

public class WeatherAppActivity extends ListActivity {

    Button buton;
    ListaOrase lista;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lista=new ListaOrase(this);
        buton=(Button)findViewById(R.id.buton);


        lista.add("Bucuresti");
        lista.add("Sibiu");
        setListAdapter(lista);

    }

    public void add(View view){
        Intent intent=new Intent();
        intent.setClass(this, Adauga.class);
        startActivityForResult(intent, 0);
    }

    public void onActivityResult (int requestCode, int responseCode, Intent data){
        System.out.println("Apelata");
        if(responseCode==1){
            lista.add(data.getStringExtra("oras")); // e chiar getText()
            System.out.println(lista.getCount());
            lista.notifyDataSetChanged();
        }
    }
}

正如您所看到的,我正在尝试刷新(notifyDataSetChanged();)在添加新元素(在BaseAdapter扩展类中)和方法onActivityResult时,在对话框将新元素传递给主Activity之后。我再说一遍,元素IS被添加到列表中,因为计数增加,它只是不刷新。

感谢您的回答!

1 个答案:

答案 0 :(得分:7)

它没有刷新是正常的,你正在向“lista”添加一个项目但是适配器保留了它自己的列表副本,所以或者你再次设置适配器中的列表然后你调用notifyDataChanged或者你添加适配器的新项目。

无论如何,我看到几个奇怪的东西,我可以使用数组适配器来简化所有事情,你不需要实现添加等。我写了一些简单的代码:

public class WeatherAppActivity extends ListActivity {

Button buton;
ItemsAdapter lista;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    List<String> initialList = new ArrayList<String>();
    initialList.add("Bucuresti");
    initialList.add("Sibiu");

    lista=new ItemsAdapter(this, initialList);
    buton=(Button)findViewById(R.id.button1);
    buton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             lista.add(""+System.currentTimeMillis()); // e chiar getText()
             lista.notifyDataSetChanged();

        }
    });

    setListAdapter(lista);

}

class ItemsAdapter extends ArrayAdapter<String> {

    public ItemsAdapter(Context context, List<String> list) {
        super(context, R.layout.lista, list);
    }

    @Override
    public View getView(final int position, View row, final ViewGroup parent) {
        final String item = getItem(position);

        ItemWrapper wrapper = null;
        if (row == null) {
            row = getLayoutInflater().inflate(R.layout.lista, parent, false);
            wrapper = new ItemWrapper(row);

            row.setTag(wrapper);
        } else {
            wrapper = (ItemWrapper) row.getTag();
        }
        wrapper.refreshData(item);

        return row;
    }

    class ItemWrapper {

        TextView text;

        public ItemWrapper(View row) {
            text = (TextView) row.findViewById(R.id.elementLista);
        }

        public void refreshData(String item) {
            text.setText(item);
        }

    }
    }    

}

这些是我用过的xml:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="63dp"
    android:text="Button" />

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >
</ListView>

</RelativeLayout>

lista.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/elementLista"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

这是使用baseadapter的适配器版本:

class ItemsBaseAdapter extends BaseAdapter {

private List<String> items;
private Context mContext;

public ItemsBaseAdapter(Context context, List<String> list) {
    items = list;
    mContext = context;
}

public void addItem(String str) {
    items.add(str);
}

@Override
public View getView(final int position, View row, final ViewGroup parent) {
    final String item = (String) getItem(position);

    ItemWrapper wrapper = null;
    if (row == null) {
        row = getLayoutInflater().inflate(R.layout.lista, parent, false);
        wrapper = new ItemWrapper(row);

        row.setTag(wrapper);
    } else {
        wrapper = (ItemWrapper) row.getTag();
    }
    wrapper.refreshData(item);

    return row;
}

class ItemWrapper {

    TextView text;

    public ItemWrapper(View row) {
        text = (TextView) row.findViewById(R.id.elementLista);
    }

    public void refreshData(String item) {
        text.setText(item);
    }

}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
}

这是列表项的版本,左侧还包含一个imageview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView 
    android:layout_height="wrap_content" 
    android:src="@android:drawable/btn_star_big_on" 
    android:scaleType="fitCenter" 
    android:layout_width="wrap_content" 
    />

<TextView
    android:id="@+id/elementLista"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" 
    />

</LinearLayout>