简体   繁体   English

列表视图根本不更新

[英]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: 嗨我正在使用带有三个片段的查看寻呼机指示器,其中一个我有一个列表视图,显示我的SQLite数据库的内容但是当我向我的表添加数据时,列表视图没有显示我添加的新数据这是我的代码:

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 . 扩展SherlockListFragments的最佳方法是让onCreateView返回膨胀的视图,然后覆盖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. 第一个问题是你在onCreateView中设置适配器。 In onCreateView list view is not created yet. 在onCreateView列表视图中尚未创建。 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. 因为您覆盖oncreateview,所以不能使用setListAdapter,因为它指的是默认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. 此时您无法调用setListAdapter ,因为尚未设置视图。

You need to wait until onViewCreated . 你需要等到onViewCreated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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