gpt4 book ai didi

安卓编程: I cant get data to reload into arrayadapter

转载 作者:行者123 更新时间:2023-11-30 01:58:10 26 4
gpt4 key购买 nike

我仍然被这个问题困住了,谁能帮忙。看来我的问题是我无法更新数据列表。我已经尝试了我在谷歌等网站上搜索过的所有解决方案。但有一半时间我什至不确定我做的是否正确。

我已经使用 onResume() 来调用 notifyDataSetChanged,但没有用。我尝试将刷新方法放入适配器中,然后在 OnResume() 中调用该方法。它再次不起作用。有人建议在 onResume 中清除适配器 (adapter.clear();),然后使用 addAll() 函数重新列出数据,但没有任何效果。

必须有一个简单的解决方案。我现在已经坚持了 2 天了。非常沮丧。

又是我的 Fragment 代码...

enter code here

public class SavedAppFragment extends ListFragment {

private static final String TAG = "AppClicked"; //DEBUGGER

private ArrayList<App> mSavedApps;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Populate the ArrayList
mSavedApps = SavedAppData.get(getActivity()).getApps();


AppAdapter adapter = new AppAdapter(mSavedApps);
setListAdapter(adapter);

}

//LIST ITEM CLICKED: /*Control what happens when list item is clicked: I.E. Load up a quiz while putting an EXTRA key containg the package name of the App to be launhced should the user get the question correct */ @Override public void onListItemClick(ListView l, View v, int position,long id) { //Return the crime for the list item that was clicked App c = ((AppAdapter) getListAdapter()).getItem(position); Log.d(TAG, "was clicked");

//Start the Activity that will list the detail of the app
Intent i = new Intent(getActivity(), Quiz_Activity.class);
String name = c.getPackage();
i.putExtra("packagename", name);
startActivity(i);
}

private class AppAdapter extends ArrayAdapter {

private ArrayList<App> mSavedApps;

public AppAdapter(ArrayList<App> apps) {

super(getActivity(), 0, apps);

}




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

//If we weren't given a view, inflate one
if (null == convertView) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_app, null);
//((AppAdapter) getListAdapter()).notifyDataSetChanged();
}

((AppAdapter) getListAdapter()).notifyDataSetChanged();
//Configure the view for this crime
App c = getItem(position);


TextView nameTextView = (TextView) convertView.findViewById(R.id.app_name);
nameTextView.setText(c.getName());
// nameTextView.setText(applicationInfo.loadLabel(packageManager));


TextView packageTextView = (TextView) convertView.findViewById(R.id.app_package);
packageTextView.setText(c.getPackage());



CheckBox appCheckBox = (CheckBox) convertView.findViewById(R.id.app_checked);
appCheckBox.setChecked(c.isChecked());


//Return the view object to the ListView
return convertView;


}

}

}

谢谢!!!

最佳答案

当你返回到 Activity B 时,之前的 Activity B 还没有被销毁。因此,它会跳过 onCreate。将您想要确保每次都发生的所有事情都移到 onResume 中。我认为您想在 onCreate 中将 Adapter 设为类变量(我将其称为 mAdapter),并添加直接从列表中获取数据的代码。如果您需要做某事,请在适配器中放置一个“刷新”功能。我假设您有一个自定义适配器,因为我从未听说过 AppAdapter。如果不这样做,则扩展 AppAdapter 并添加该功能。因此,您的 onCreate 应如下所示:

   mAdapter = new AppAdapter(mSavedApps);
setListAdapter(mAdapter);

您的 onRefresh 可以通过一些新的更新函数更新适配器中包含的数据,如下所示:

mAdapter.update(SavedAppData.get(getActivity()).getApps());

关于安卓编程: I cant get data to reload into arrayadapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31880866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com