错误无法使用 list.setAdapter(适配器)解析符号列表
Error cannot resolve simbol list with list.setAdapter(adapter)
这是我的代码,它以 100% 的速度运行:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.t1, container, false);
final ListView list = (ListView) inflatedView.findViewById(R.id.list);
class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
MainActivity.Adapter adapter = new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
list.setAdapter(adapter);
list.setOnItemClickListener
...
}
}
但现在我也需要在另一个位置调用 json class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
....
class json .....
}
@Override
public void onRefresh() {
new json();// i can't recall in this way, but i need it
}
问题出在列表上,因为我无法从 onCreateView 中退出,因为如果我这样做,列表不确定。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
....
new json();
}
class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
MainActivity.Adapter adapter = new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
list.setAdapter(adapter);//this is an error now
现在的错误是"cannot resolve simbol 'list'"。
快速解决方案是将 list
设为 class 变量。
public class MainActivty extends Activity {
private ListView mList;
private class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
mList.setAdapter(adapter);
...
}
}
但更好的解决方案是将 result
从 onPostExecute
传回 MainActivity 以更新 UI。
public class MainActivty extends Activity {
private ListView mList;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.t1, container, false);
mList = (ListView) inflatedView.findViewById(R.id.list);
...
}
private void updateUI(final List<MovieModel> result> {
MainActivity.Adapter adapter
= new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
mList.setAdapter(adapter);
...
}
private class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
updateUI(result);
...
}
}
通过这种方式,您可以在 AsyncTask
中保留所有数据处理,在 Activity
中保留所有 UI 处理。
这是我的代码,它以 100% 的速度运行:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.t1, container, false);
final ListView list = (ListView) inflatedView.findViewById(R.id.list);
class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
MainActivity.Adapter adapter = new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
list.setAdapter(adapter);
list.setOnItemClickListener
...
}
}
但现在我也需要在另一个位置调用 json class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
....
class json .....
}
@Override
public void onRefresh() {
new json();// i can't recall in this way, but i need it
}
问题出在列表上,因为我无法从 onCreateView 中退出,因为如果我这样做,列表不确定。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
....
new json();
}
class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
MainActivity.Adapter adapter = new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
list.setAdapter(adapter);//this is an error now
现在的错误是"cannot resolve simbol 'list'"。
快速解决方案是将 list
设为 class 变量。
public class MainActivty extends Activity {
private ListView mList;
private class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
mList.setAdapter(adapter);
...
}
}
但更好的解决方案是将 result
从 onPostExecute
传回 MainActivity 以更新 UI。
public class MainActivty extends Activity {
private ListView mList;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.t1, container, false);
mList = (ListView) inflatedView.findViewById(R.id.list);
...
}
private void updateUI(final List<MovieModel> result> {
MainActivity.Adapter adapter
= new MainActivity.Adapter(getActivity().getApplicationContext(), R.layout.row, result);
mList.setAdapter(adapter);
...
}
private class json extends AsyncTask<String,String, List<MovieModel> > {
...
@Override
protected void onPostExecute(final List<MovieModel> result) {
...
updateUI(result);
...
}
}
通过这种方式,您可以在 AsyncTask
中保留所有数据处理,在 Activity
中保留所有 UI 处理。