简体   繁体   中英

List view does not update at all

Hi i am using view pager indicator with three fragments and in one of the i have a list view that show the content of my SQLite Database but when a add data to my table the list view doesn't show he new data that i have added here is my code:

public class caleryhistory extends SherlockListFragment {
    List<calery_lagari> Calery_lagari;
    calery_lagari_SQLiteData data;
    ArrayAdapter<calery_lagari> adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.calery_history, null);


        data = new calery_lagari_SQLiteData(getActivity());
        data.open();
        Calery_lagari = data.findall();
        adapter = new ArrayAdapter<calery_lagari>(getActivity(),
                R.layout.listback_layout, Calery_lagari);
        adapter.notifyDataSetChanged();

        setListAdapter(adapter);

        adapter.notifyDataSetChanged();

        return v;

    }

so any help? Thanks in advance! :)

Update

import java.util.List;

import mr.chag.va.lagar.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;


public class caleryhistory extends SherlockListFragment {
    List<calery_lagari> Calery_lagari;
    calery_lagari_SQLiteData data;
    ArrayAdapter<calery_lagari> adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.calery_history, null);




        return v;

    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        data = new calery_lagari_SQLiteData(getActivity());
        data.open();
        Calery_lagari = data.findall();
        adapter = new ArrayAdapter<calery_lagari>(getActivity(),
                R.layout.listback_layout, Calery_lagari);
        adapter.notifyDataSetChanged();
         ListView listView = (ListView)view.findViewById(android.R.id.list);
         listView.setAdapter(adapter);


        adapter.notifyDataSetChanged();
        super.onViewCreated(view, savedInstanceState);
    }

}

my layout

<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clear"
        android:text="clear all" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

The best way when extending SherlockListFragments is to have onCreateView return the the inflated view, then override onViewCreated . And put your code that changes your view in there.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b){
     return inflater.inflate(R.layout.calery_history, null);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) { 
     data = new calery_lagari_SQLiteData(getActivity());
     data.open();
     Calery_lagari = data.findall();
     adapter = new ArrayAdapter<calery_lagari>(getActivity(), view.R.layout.listback_layout, Calery_lagari);
     adapter.notifyDataSetChanged();

     setListAdapter(adapter);

     adapter.notifyDataSetChanged();

}

First problem is that you set adapter in onCreateView. In onCreateView list view is not created yet. Second you need to set adapter to correct list view. Because you override oncreateview you can't use setListAdapter because it's referring to list view from default viet. Try this:

       @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.calery_history, null);
}

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    data = new calery_lagari_SQLiteData(getActivity());
    data.open();
    Calery_lagari = data.findall();
    adapter = new ArrayAdapter<calery_lagari>(getActivity(),
            view.R.layout.listback_layout, Calery_lagari);

    ListView listView = (ListView)view.findViewById(R.id.YOUR_LISTVIEW_ID);
    listView.setAdapter(adapter);

    adapter.notifyDataSetChanged();

}

You cannot call setListAdapter at this point, as the view is not yet set.

You need to wait until onViewCreated .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM