gpt4 book ai didi

android - fragment 中的 ArrayAdapter

转载 作者:行者123 更新时间:2023-11-29 16:05:44 25 4
gpt4 key购买 nike

我是 Android 开发的初学者!我正在尝试插入 Fragment 解析数据试图修复错误,但我有一个错误“输出无法解析为变量”

  @Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
ProgressDialog mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setCancelable(false);
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Загрузка данных");
mProgressDialog.show();
new ParseSite().execute("http://www.babyblog.ru/magazine/");
View view = inflater.inflate(R.layout.magazine, container, false);
ListView listview = (ListView) view.findViewById(R.id.listViewData);
listview.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1 , output));
return view;
}
private class ParseSite extends AsyncTask<String, Void, List<String>> {
protected List<String> doInBackground(String... arg) {
List<String> output = new ArrayList<String>();
try
{
HtmlHelper hh = new HtmlHelper(new URL(arg[0]));
List<TagNode> links = hh.getLinksByClass("razdel-name");

for (Iterator<TagNode> iterator = links.iterator(); iterator.hasNext();)
{
TagNode divElement = (TagNode) iterator.next();
output.add(divElement.getText().toString());
}
}
catch(Exception e)
{
e.printStackTrace();
}
return output;
}
}

最佳答案

尚 undefined variable “输出”。

 listview.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1 , output));

该变量不在范围内。

您尝试在 fragment 的 onCreate 中使用它,但它是在 ASyncTask 类中声明的。

您需要阅读有关 ASyncTasks 以及如何使用它们的信息。

http://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads

作为捷径,试试这个:

public class YourFragment extends Fragment {

ListView listview;

@Override
public View onCreateView(
// other stuff
listview = (ListView) view.findViewById(R.id.listViewData);
// remove the setAdapater line

}

private class ParseSite extends AsyncTask<String, Void, List<String>> {

// other stuff

protected void onPostExecute(List<String> result) {
listview.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1 , output));
}
}

关于android - fragment 中的 ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18686124/

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