查看寻呼机循环,其中arraylist对象错误未知

时间:2015-10-13 02:10:24

标签: java android

我刚刚完成了一个小代码,但它有一个bug,我不知道为什么会出错。我的代码



package com.creativei.viewpagerloop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	private static final String LOG_TAG = "ViewPagerLoop";
	private Button btn;
	static ArrayList<data> arraydt = new ArrayList<data>();
	ArrayList<data> str = new ArrayList<data>();

	int Cur = 1;
	static int lastPage = 1;
	Button btnUp, btnDown;
	static int nextId;
	int id = 2;
	private ViewPager pager;
	private Fragment fragment1;
	private ArrayList<data> arraydt1 = new ArrayList<data>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		str.add(new data("0", "hello0"));
		str.add(new data("1", "hello1"));
		str.add(new data("2", "hello2"));
		str.add(new data("3", "hello3"));
		str.add(new data("4", "hello4"));
		str.add(new data("5", "hello5"));
		str.add(new data("6", "hello6"));
		str.add(new data("7", "hello7"));
		str.add(new data("8", "hello8"));
		str.add(new data("9", "hello9"));

		arraydt.add(new data("1", "hello0"));
		arraydt.add(new data("2", "hello1"));
		arraydt.add(new data("3", "hello2"));

		pager = (ViewPager) findViewById(R.id.pager);
		TextView counter = (TextView) findViewById(R.id.counter);
		btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplication(), "Ok", Toast.LENGTH_SHORT)
						.show();
			}
		});
		SimpleViewPagerAdapter adapter = new SimpleViewPagerAdapter(this,
				getSupportFragmentManager(), pager, counter, arraydt);
		pager.setAdapter(adapter);
		pager.setOnPageChangeListener(adapter);
		pager.setCurrentItem(2, false);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			private Object fragment;

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub

				if (position == 0) {
					pager.setCurrentItem(arraydt.size(), false);
					nextId = 1;
					Log.d("position0", "00000");
				} else if (position == arraydt.size() + 1) {
					pager.setCurrentItem(1, false);
					nextId = 3;
					Log.d("position" + position, "" + position);
				}

				// if (nextId == 0)
				if (nextId == 1) {

					if (position == 2) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+12 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(2, str.get(id));
						Log.d("ArrayDT", "   0: " + arraydt.get(0).getText()
								+ "   1: " + arraydt.get(1).getText() + "  2: "
								+ arraydt.get(2).getText());
					} else if (position == 3) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-13 ", "" + id);
						Log.d("Id", str.get(id).getText());
					}
				} else if (nextId == 2) {
					if (position == 3) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+23 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(0, str.get(id));
						Log.d("ArrayDT", "   0: " + arraydt.get(0).getText()
								+ "   1: " + arraydt.get(1).getText() + "  2: "
								+ arraydt.get(2).getText());
					} else if (position == 1) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-21 ", "" + id);
						Log.d("Id", str.get(id).getText());
					}
				} else if (nextId == 3) {
					if (position == 1) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+31 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(1, str.get(id));
						Log.d("ArrayDT", "   0: " + arraydt.get(0).getText()
								+ "   1: " + arraydt.get(1).getText() + "  2: "
								+ arraydt.get(2).getText());
					} else if (position == 2) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
					}
				}
				nextId = pager.getCurrentItem();
				Log.d("CURRENTUITEM", "" + pager.getCurrentItem());

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public static class SimpleFragment extends Fragment {
		private int position;
		private TextView tv;
		private int id1;
		private LinearLayout llayout;

		public SimpleFragment() {
			// this.position=postiton;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			// String content1 = getArguments().getString("content");
			id1 = getArguments().getInt("position");

			
			// int id = Integer.parseInt(content1);
			// Log.d("Content", content1);
			llayout = (LinearLayout) rootView.findViewById(R.id.Llayout);
			llayout.removeAllViews();
			tv = new TextView(getActivity());

			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
			Log.d("Oncreate", "" + id1 + "  "
					+ arraydt.get(id1).getText().toString());
			tv.setText(arraydt.get(id1).getText().toString() + " " + (id1));
			Log.d("TextView", ""
					+ tv.getText().toString());
			llayout.addView(tv);

			return rootView;

		}

		@Override
		public void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			llayout.removeAllViews();
			id1 = getArguments().getInt("position");
			tv = new TextView(getActivity());
			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
			tv.setText(arraydt.get(id1).getText().toString() + " " + (id1));
			Log.d("TV", tv.getText().toString() + " "
					+ arraydt.get(0).getText().toString() + " "
					+ arraydt.get(1).getText().toString() + " "
					+ arraydt.get(2).getText().toString());
			Log.d("TV", tv.getText().toString() + " "
					+ arraydt.get(id1).getText().toString());
			llayout.addView(tv);
			Log.d("ONRESUM", "ONRESUM");
		}
	}

	public static class SimpleViewPagerAdapter extends
			FragmentStatePagerAdapter implements OnPageChangeListener {
		private Map<Integer, String> mFragmentTags;
		private ArrayList<data> arraydt;
		private String[] content;
		private ViewPager pager;
		private TextView counter;
		private Context context;
		private FragmentManager mFragmentManager;

		public SimpleViewPagerAdapter(Context context, FragmentManager fm,
				ViewPager pager, TextView counter, ArrayList<data> arraydt) {

			super(fm);
			mFragmentManager = fm;
			this.pager = pager;
			this.counter = counter;
			this.arraydt = arraydt;
			mFragmentTags = new HashMap<Integer, String>();
			this.context = context;

		}

		@Override
		public Object instantiateItem(View container, int position) {
			Object ojb = super.instantiateItem(container, position);
			if (ojb instanceof Fragment) {
				Fragment f = (Fragment) ojb;
				String tag = f.getTag();
				mFragmentTags.put(position, tag);
			}

			return ojb;
		}

		public Fragment getFragment(int position) {
			String tag = mFragmentTags.get(position);
			if (tag == null) {
				return null;
			}
			return mFragmentManager.findFragmentByTag(tag);
		}

		@Override
		public Fragment getItem(int position) {
			SimpleFragment fragment = new SimpleFragment();

			Bundle bundle = new Bundle();
			int index;
			if (position == 0) {
				index = arraydt.size() - 1;
				Log.d("OK", "OK0");
			} else if (position == arraydt.size() + 1) {
				index = 0;
				Log.d("OK", "OK4");
			} else {
				index = position - 1;
				Log.d("OK", "OK" + index);
			}

			bundle.putInt("position", index);
			fragment.setArguments(bundle);

			return fragment;
		}

		@Override
		public int getCount() {
			return arraydt.size() + 2;
		}

		@Override
		public void onPageSelected(int position) {
			Log.d("nextId", "" + position);
			nextId = position;

		}

		private String makeCounterText(int pageNo) {
			return "Page " + pageNo + " of " + arraydt.size();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			Toast.makeText(context, "arg0: " + arg0, Toast.LENGTH_LONG).show();
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

	}
}
&#13;
&#13;
&#13;

activity_main.xml中

&#13;
&#13;
package com.creativei.viewpagerloop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	private static final String LOG_TAG = "ViewPagerLoop";
	private Button btn;
	static ArrayList<data> arraydt = new ArrayList<data>();
	ArrayList<data> str = new ArrayList<data>();

	int Cur = 1;
	static int lastPage = 1;
	Button btnUp, btnDown;
	static int nextId;
	int id = 2;
	private ViewPager pager;
	private Fragment fragment1;
	private ArrayList<data> arraydt1 = new ArrayList<data>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		str.add(new data("0", "hello0"));
		str.add(new data("1", "hello1"));
		str.add(new data("2", "hello2"));
		str.add(new data("3", "hello3"));
		str.add(new data("4", "hello4"));
		str.add(new data("5", "hello5"));
		str.add(new data("6", "hello6"));
		str.add(new data("7", "hello7"));
		str.add(new data("8", "hello8"));
		str.add(new data("9", "hello9"));

		arraydt.add(new data("1", "hello0"));
		arraydt.add(new data("2", "hello100"));
		arraydt.add(new data("3", "hello2"));

		pager = (ViewPager) findViewById(R.id.pager);
		TextView counter = (TextView) findViewById(R.id.counter);
		btn = (Button) findViewById(R.id.button1);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplication(), "Ok", Toast.LENGTH_SHORT)
						.show();
			}
		});
		SimpleViewPagerAdapter adapter = new SimpleViewPagerAdapter(this,
				getSupportFragmentManager(), pager, counter, arraydt);
		pager.setAdapter(adapter);
		pager.setOnPageChangeListener(adapter);
		pager.setCurrentItem(2, false);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			private Object fragment;

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub

				if (position == 0) {
					pager.setCurrentItem(arraydt.size(), false);
					nextId = 1;
					Log.d("position0", "00000");
				} else if (position == arraydt.size() + 1) {
					pager.setCurrentItem(1, false);
					nextId = 3;
					Log.d("position" + position, "" + position);
				}

				// if (nextId == 0)
				if (nextId == 1) {

					if (position == 2) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+12 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(2, str.get(id));
						Log.d("ArrayDT", "   0: " + arraydt.get(0).getText()
								+ "   1: " + arraydt.get(1).getText() + "  2: "
								+ arraydt.get(2).getText());
					} else if (position == 3) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-13 ", "" + id);
						Log.d("Id", str.get(id).getText());
					}
				} else if (nextId == 2) {
					if (position == 3) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+23 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(0, str.get(id));
						Log.d("ArrayDT", "   0: " + arraydt.get(0).getText()
								+ "   1: " + arraydt.get(1).getText() + "  2: "
								+ arraydt.get(2).getText());
					} else if (position == 1) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
						Log.d("-21 ", "" + id);
						Log.d("Id", str.get(id).getText());
					}
				} else if (nextId == 3) {
					if (position == 1) {
						id++;
						if (id == str.size()) {
							id = 0;
						}
						Log.d("+31 ", "" + id);
						Log.d("Id", str.get(id).getText());
						arraydt.set(1, str.get(id));
						Log.d("ArrayDT", "   0: " + arraydt.get(0).getText()
								+ "   1: " + arraydt.get(1).getText() + "  2: "
								+ arraydt.get(2).getText());
					} else if (position == 2) {
						id--;
						if (id < 0) {
							id = str.size() - 1;
						}
					}
				}
				nextId = pager.getCurrentItem();
				Log.d("CURRENTUITEM", "" + pager.getCurrentItem());

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public static class SimpleFragment extends Fragment {
		private int position;
		private TextView tv;
		private int id1;
		private LinearLayout llayout;

		public SimpleFragment() {
			// this.position=postiton;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {

			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			// String content1 = getArguments().getString("content");
			id1 = getArguments().getInt("position");

			
			// int id = Integer.parseInt(content1);
			// Log.d("Content", content1);
			llayout = (LinearLayout) rootView.findViewById(R.id.Llayout);
			llayout.removeAllViews();
			tv = new TextView(getActivity());

			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
			Log.d("Oncreate", "" + id1 + "  "
					+ arraydt.get(id1).getText().toString());
			tv.setText(arraydt.get(id1).getText().toString() + " " + (id1));
			Log.d("TextView", ""
					+ tv.getText().toString());
			llayout.addView(tv);

			return rootView;

		}

		@Override
		public void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			llayout.removeAllViews();
			id1 = getArguments().getInt("position");
			tv = new TextView(getActivity());
			tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
			tv.setText(arraydt.get(id1).getText().toString() + " " + (id1));
			Log.d("TV", tv.getText().toString() + " "
					+ arraydt.get(0).getText().toString() + " "
					+ arraydt.get(1).getText().toString() + " "
					+ arraydt.get(2).getText().toString());
			Log.d("TV", tv.getText().toString() + " "
					+ arraydt.get(id1).getText().toString());
			llayout.addView(tv);
			Log.d("ONRESUM", "ONRESUM");
		}
	}

	public static class SimpleViewPagerAdapter extends
			FragmentStatePagerAdapter implements OnPageChangeListener {
		private Map<Integer, String> mFragmentTags;
		private ArrayList<data> arraydt;
		private String[] content;
		private ViewPager pager;
		private TextView counter;
		private Context context;
		private FragmentManager mFragmentManager;

		public SimpleViewPagerAdapter(Context context, FragmentManager fm,
				ViewPager pager, TextView counter, ArrayList<data> arraydt) {

			super(fm);
			mFragmentManager = fm;
			this.pager = pager;
			this.counter = counter;
			this.arraydt = arraydt;
			mFragmentTags = new HashMap<Integer, String>();
			this.context = context;

		}

		@Override
		public Object instantiateItem(View container, int position) {
			Object ojb = super.instantiateItem(container, position);
			if (ojb instanceof Fragment) {
				Fragment f = (Fragment) ojb;
				String tag = f.getTag();
				mFragmentTags.put(position, tag);
			}

			return ojb;
		}

		public Fragment getFragment(int position) {
			String tag = mFragmentTags.get(position);
			if (tag == null) {
				return null;
			}
			return mFragmentManager.findFragmentByTag(tag);
		}

		@Override
		public Fragment getItem(int position) {
			SimpleFragment fragment = new SimpleFragment();

			Bundle bundle = new Bundle();
			int index;
			if (position == 0) {
				index = arraydt.size() - 1;
				Log.d("OK", "OK0");
			} else if (position == arraydt.size() + 1) {
				index = 0;
				Log.d("OK", "OK4");
			} else {
				index = position - 1;
				Log.d("OK", "OK" + index);
			}

			bundle.putInt("position", index);
			fragment.setArguments(bundle);

			return fragment;
		}

		@Override
		public int getCount() {
			return arraydt.size() + 2;
		}

		@Override
		public void onPageSelected(int position) {
			Log.d("nextId", "" + position);
			nextId = position;

		}

		private String makeCounterText(int pageNo) {
			return "Page " + pageNo + " of " + arraydt.size();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			Toast.makeText(context, "arg0: " + arg0, Toast.LENGTH_LONG).show();
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

	}
}
&#13;
&#13;
&#13;

fragment_main.xml

&#13;
&#13;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.creativei.viewpagerloop.SimpleFragment" >

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/Llayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>
&#13;
&#13;
&#13;

无法重复查看寻呼机的第二个元素。我在闪电之前和之后注销了文本视图值,我看到它仍然是真的,但是当它在文本视图中显示时,值是不同的值。哪里有错误?

0 个答案:

没有答案