android - ListView 不会自行更新

标签 android listview android-fragments

我在一个 fragment 上显示了一些数据,每次我点击一个按钮时,它应该添加到第二个 fragment 的 listView 中。 出于某种原因,当我单击按钮时,listView 没有改变。

fragment 1:

nextButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) 
                {            
                    HistoryFragment.addToHistory( myObject );
                }});

历史 fragment ( ListView ):

public class HistoryFragment extends ListFragment
{ 
    private static List<Password> historyList = new ArrayList<MyObject>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        historyList = myManager.getMyObjects ();
    }

    public static void addToHistory( MyObject myObject )
    {
        historyList.add( myObject );
    }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup         container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_history, container, false);
    MyObjectAdapter adapter = new MyObjectAdapter( historyList );
    setListAdapter(adapter);
    return view;
}
    ....
    ....
    ....
private class MyObjectAdapter extends BaseAdapter
    {
        private final ArrayList<MyObject> data; 

        public MyObjectAdapter( List<MyObject> historyList ) 
        {
            data = new ArrayList<MyObject>();
            data.addAll( historyList );
        }

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

        @Override
        public MyObject getItem( int position ) 
        {            
            return data.get( position );
        }

        @Override
        public long getItemId(int position) {
            // TODO implement you own logic with ID
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final View result;

            notifyDataSetChanged();

            if (convertView == null) 
            {
                result = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
            } 
            else 
            {
                result = convertView;
            }

            MyObject item = getItem( position );

            ( (TextView) result.findViewById(R.id.historyObjectTextView) ).setText(item.toString());

            return result;
       }
    }

主要 fragment (1):

  public class MainFragment extends Fragment 
 {
 Map<String,MyObject> objects  = new HashMap<String,MyObject>();
 List< MyObject > objectsList = new ArrayList< MyObject >();
 TextView nextDisplay;

@Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
objects = myManager.getInstance().getObjects();
objectsList = MyObject.getObjectsList();
}

@Override
 public View onCreateView( LayoutInflater inflater, ViewGroup                     container, Bundle savedInstanceState ) 
 {
    View view = inflater.inflate(R.layout.fragment_main, container,   false);

nextDisplay = ( TextView ) view.findViewById( R.id.displayTextView );
nextDisplay.setText( objectsList.get( 0 ).getObjectString() );

Button nextButton = ( Button ) view.findViewById( R.id.nextObjectButton );
nextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) 
        {            
            if( objectsList.size() > 0 )
            {
                objects.get(objectsList.get( 0 ).getObjectString()).setTried( true );
                long currentTime = System.currentTimeMillis();
                objects.get(objectsList.get( 0 ).getObjectString()).setTime( currentTime );


                HistoryFragment.addToHistory( objectsList.get( 0 ) );
                objectsList.remove( 0 );

                if( objectsList.size() > 0  && !objectsList.get( 0 ).getTried() )
                {
                    nextCodeDisplay.setText( objectsList.get( 0 ).getObjectString() );
                }
            }           
        }});
return view;

最佳答案

像这样。

private List<Password> historyList = new ArrayList<MyObject>();
private MyObjectAdapter adapter
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_history, container, false);
    adapter = new MyObjectAdapter( historyList );
    setListAdapter(adapter);
    return view;
}

private static List<Password> historyList = new ArrayList<MyObject>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    historyList = myManager.getMyObjects ();
}

public void addToHistory( MyObject myObject )
{
    historyList.add( myObject );
    adapter.notifyDataSetChanged();
}

关于android - ListView 不会自行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31418019/

相关文章:

android - 使用 applyBatch 插入数千个联系人条目很慢

android - 获取选定的 ListView 项的 ID(使用 SQLite 数据库填充的数据)

javascript - 如何在WinJS中为ListView制作排序功能?

listview - Flutter中SliverPersistentHeader和TabBarView如何搭配使用?

android - 获取从 fragment 到 Activity 的变量引用

Android:可滚动的标签

javascript - 如何在 MyAdapter 的 ListView 中添加广告

android - 我可以开始多少级 Activity/fragment ?

Android Viewpager.setCurrentitem() 和 FragmentPagerAdapter instantiateItem() fragment 初始化不匹配

android - 如何获取复杂 ListView 行 View 中的 View ?