我有2個片段,都與陣列適配器一起使用以顯示其類別。第一個片段是顯示項目類別,第二個片段是顯示作業類別。但是,它會在兩個片段中顯示作業類別。我不知道哪部分出了問題。Android ArrayAdapter getView無法獲得正確的位置
這是我的ViewPager,當用戶選擇相應的選項卡時顯示片段。 setCategory()將在從數據庫接收到數據時調用。
public class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Category> categoryList = new ArrayList();
private DisplayItemFragment itemFragment;
private DisplayJobFragment jobFragment;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.d("categoryList", categoryList.size()+" ");
if (position ==0) {
itemFragment = new DisplayItemFragment(categoryList);
return itemFragment;
}else{
jobFragment = new DisplayJobFragment(categoryList);
return jobFragment;
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position){
//
//Your tab titles
//
case 0:return "Item";
case 1:return "Job";
default:return null;
}
}
public void setCategory(List<Category> list){
List<Category> tempList = new ArrayList();
for(int i=0;i<list.size();i++){
if(list.get(i).getType().equals("item")){
tempList.add(list.get(i));
}
}
itemFragment.update(tempList);
Log.d("this.categoryListitem",tempList.size()+"");
tempList.clear();
for(int i=0;i<list.size();i++){
if(list.get(i).getType().equals("job")){
tempList.add(list.get(i));
}
}
jobFragment.update(tempList);
Log.d("this.categoryListjob",tempList.size()+"");
}
}
這是片段顯示項目類別
public class DisplayItemFragment extends Fragment {
private View rootView;
private GridView gridView;
private List<Category> categoryList;
private DisplayFragmentPresenter presenter;
private CategoryGridAdapter categoryAdapter;
private TextView text;
public DisplayItemFragment(List<Category> categoryList) {
this.categoryList=categoryList;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_display, container, false);
text = (TextView) rootView.findViewById(R.id.test);
categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, categoryList);
gridView = (GridView) rootView.findViewById(R.id.gridview);
gridView.setAdapter(categoryAdapter);
gridView.setTextFilterEnabled(true);
//Log.d("category",categoryList.get(0).getName());
return rootView;
}
public void update(List<Category> list) {
this.categoryList.clear();
for(int i=0;i<list.size();i++){
if(list.get(i).getType().equals("item")){
this.categoryList.add(list.get(i));
}
}
text.setText("item refresh");
for(int i=0;i<this.categoryList.size();i++){
Log.d("categoryListitem",categoryList.get(i).getName());
}
}
這是爲了顯示作業類別
public class DisplayJobFragment extends Fragment {
private View rootView;
private GridView gridView;
private List<Category> categoryList;
private DisplayFragmentPresenter presenter;
private TextView text;
private CategoryGridAdapter categoryAdapter;
public DisplayJobFragment(List<Category> categoryList) {
this.categoryList=categoryList;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_display, container, false);
text = (TextView) rootView.findViewById(R.id.test);
categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, categoryList);
gridView = (GridView) rootView.findViewById(R.id.gridview);
gridView.setAdapter(categoryAdapter);
gridView.setTextFilterEnabled(true);
//Log.d("category",categoryList.get(0).getName());
return rootView;
}
public void update(List<Category> list) {
this.categoryList.clear();
for(int i=0;i<list.size();i++){
if(list.get(i).getType().equals("job")){
this.categoryList.add(list.get(i));
}
}
text.setText("job refresh");
//categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, this.categoryList);
for(int i=0;i<this.categoryList.size();i++){
Log.d("categoryListjob",categoryList.get(i).getName());
}
}
}
兩個片段將被稱爲CategoryGridAdapter對GridView控件顯示類別中的片段
public class CategoryGridAdapter extends ArrayAdapter<Category> {
private List<Category> categoryList;
private Context context;
public CategoryGridAdapter(Context context, int resource, List<Category> categoryList) {
super(context, resource, categoryList);
this.context = context;
this.categoryList = categoryList;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public int getCount() {
return ((null != categoryList) ?
categoryList.size() : 0);
}
@Override
public Category getItem(int position) {
return ((null != categoryList) ?
categoryList.get(position) : null);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (null == view) {
view = layoutInflater.inflate(R.layout.grid_category, null);
}
final Category category = categoryList.get(position);
Log.d("categorygridlist",category.getName()+" "+position+ " " + categoryList.size());
if (category != null) {
final CardView categoryGridLayout = (CardView) view.findViewById(R.id.category_gridlayout);
final TextView categoryName = (TextView) view.findViewById(R.id.category_name);
final ImageView categoryIcon = (ImageView) view.findViewById(R.id.category_icon);
categoryName.setText(category.getName());
categoryName.setSelected(true);
try {
byte[] decodedString = Base64.decode(category.getImage(), Base64.DEFAULT);
BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
options.inPurgeable = true; // inPurgeable is used to free up memory while required
Bitmap decodedByte1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);//Decode image, "thumbnail" is the object of image file
Bitmap decodedByte = Bitmap.createScaledBitmap(decodedByte1, 50 , 50 , true);// convert decoded bitmap into well scalled Bitmap format.
categoryIcon.setImageBitmap(decodedByte);
} catch (Exception e) {
e.printStackTrace();
}
categoryGridLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "worked", Toast.LENGTH_LONG).show();
}
});
}
return view;
}
當用戶選擇作業選項卡時,用戶選擇視圖頁面上的項目選項卡和作業類別時,我希望它會在視圖頁面顯示項目類別,但它只顯示作業類別。任何人都有解決方案嗎?由於
@Mike M.我已經使用Log.d來檢查tempList是否正確更新 – phoon
是的,我沒有看到你的'update()'方法,所以我刪除了我的評論。 –
哦,我現在看到它。這兩個'Fragment'都使用'ViewPagerAdapter'中定義的'List categoryList',並傳入其構造函數。不要這樣做。只需在每個中創建一個新的'ArrayList ',並從構造函數中刪除該參數(不管怎樣,它不應該在那裏)。 –