我有三个选项卡,每个选项卡都应显示从服务器获取的特定数据。我只创建了一个片段,根据打开的选项卡显示不同的信息,但是当应用程序启动时,只有一个选项卡显示数据,一段时间后,当我创建新的NamesAdapter时,它会在该行崩溃。
public class TabFragment extends Fragment {
private static final String TAG = "TabFragment";
int page;
private ArrayList<Names> namesList;
NamesAdapter adapter;
ListView listView;
public TabFragment() {
// Required empty public constructor
}
public static TabFragment newInstance(int page) {
TabFragment fragment = new TabFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
namesList = new ArrayList<>();
listView = (ListView) getActivity().findViewById(R.id.listView);
// Inflate the layout for this fragment
new MyTask().execute();
return inflater.inflate(R.layout.fragment_tab, container, false);
}
private class MyTask extends AsyncTask<String, String, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
String content = HttpManager.getData("http://union.ge/catalog/1455299520.json");
Log.e("doInBackground", "was called");
//return content;
try {
JSONArray json= new JSONArray(content);
// get json objects one by one
for (int i = 0; i < json.length(); i++){
Names names = new Names();
// this json object now has key-value pairs and
JSONObject jRealObject = json.getJSONObject(i).getJSONObject("name");
//Log.e("jrealobject", jRealObject.toString());
names.setEn(jRealObject.getString("en"));
names.setGe(jRealObject.getString("ge"));
names.setRu(jRealObject.getString("ru"));
//migebuli monacemebi shevinaxot chvens array-shi
namesList.add(names);
}
return true;
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
// on postExecute method runs on the UI thread
protected void onPostExecute(Boolean b) {
super.onPostExecute(b);
if(b){
adapter = new NamesAdapter(getContext(), R.layout.list_item, namesList, page);
Log.e(TAG, String.valueOf(page));
listView = (ListView) getActivity().findViewById(R.id.listView);
listView.setAdapter(adapter);
} else {
Toast.makeText(getActivity(), "data was not parsed", Toast.LENGTH_SHORT).show();
}
}
}
}
`
public class TabsAdapter extends FragmentPagerAdapter {
final static int PAGE_COUNT = 3;
private static final String TAG = "TabsAdapter";
private String tabTitles[] = new String[] { "EN", "GE", "RU" };
private Context context;
public TabsAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TabFragment.newInstance(0);
case 1:
return TabFragment.newInstance(1);
case 2:
return TabFragment.newInstance(2);
default:
return null;
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
FragmentManager manager = ((Fragment)object).getFragmentManager();
android.support.v4.app.FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment)object);
trans.commit();
}
}
和Names类的适配器
public class NamesAdapter extends ArrayAdapter<Names> {
private static final String TAG = "NamesAdapter";
ArrayList<Names> items;
Context context;
int resource;
int page;
LayoutInflater vi;
public NamesAdapter(Context context, int resource, ArrayList<Names> items, int pageNumber) {
super(context, resource, items);
this.context = context;
this.resource = resource;
this.items = items;
this.page = pageNumber;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
// convertView = vi.inflate(resource, null);
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
switch (page) {
case 0:
viewHolder.textView.setText(items.get(position).getEn());
break;
case 1:
viewHolder.textView.setText(items.get(position).getGe());
break;
case 2:
viewHolder.textView.setText(items.get(position).getRu());
}
return convertView;
}
static class ViewHolder {
public TextView textView;
}
}
答案 0 :(得分:0)
试试这个:
MyTask.class中的更改如下:
在doInBackground()
中,替换
namesList.add(名称);
与
publishProgress(名称);
并覆盖此方法
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
namesList.add(values[0])
}
创建适配器的方式不正确。见:
public class TabFragment extends Fragment {
private static final String TAG = "TabFragment";
private static final String PAGE_NO = "page_no";
int page;
private ArrayList<Names> namesList;
NamesAdapter adapter;
ListView listView;
public TabFragment() {
// Required empty public constructor
}
public static TabFragment newInstance(int page) {
TabFragment fragment = new TabFragment();
Bundle args = new Bundle();
args.putInt(PAGE_NO, page);
fragment .setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
namesList = new ArrayList<>();
listView = (ListView) getActivity().findViewById(R.id.listView);
// Inflate the layout for this fragment
page = getArguments().getInt(PAGE_NO);
adapter = new NamesAdapter(getContext(), R.layout.list_item, namesList, page);
Log.e(TAG, String.valueOf(page));
listView = (ListView) getActivity().findViewById(R.id.listView);
listView.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_tab, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
// Start the task when view is created.
new MyTask().execute();
}
private class MyTask extends AsyncTask<String, String, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
String content = HttpManager.getData("http://union.ge/catalog/1455299520.json");
Log.e("doInBackground", "was called");
//return content;
try {
JSONArray json= new JSONArray(content);
// get json objects one by one
for (int i = 0; i < json.length(); i++){
Names names = new Names();
// this json object now has key-value pairs and
JSONObject jRealObject = json.getJSONObject(i).getJSONObject("name");
//Log.e("jrealobject", jRealObject.toString());
names.setEn(jRealObject.getString("en"));
names.setGe(jRealObject.getString("ge"));
names.setRu(jRealObject.getString("ru"));
//migebuli monacemebi shevinaxot chvens array-shi
publishProgress(names);
}
return true;
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
namesList.add(values[0]);
adapter.notifyDataSetChanged();
}
// on postExecute method runs on the UI thread
protected void onPostExecute(Boolean b) {
super.onPostExecute(b);
if(!b){
Toast.makeText(getActivity(), "data was not parsed", Toast.LENGTH_SHORT).show();
}
}
}
}