GridView没有在android中更新?

时间:2016-11-15 07:46:28

标签: android android-fragments gridview

enter image description here

这是我的主屏幕。以上3个方框是Twowayview项目。点击它们gridview显示一些图像,第一次手动加载但当我点击twowayview项目更改值gridview没有更新,我调试我的程序我认为只有适配器有问题。

这是我的代码 ChapterActivity.java

package com.focusmedica.bphandoab1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import org.lucasr.twowayview.TwoWayView;

import java.util.ArrayList;

/**
 * Created by ashish on 11/15/2016.
 */

public class ChapterActivity extends AppCompatActivity {
    ArrayList<DIctionaryContent> AppDetails=new ArrayList<>();
    ArrayList<DIctionaryContent> head=new ArrayList<>();
    TwoWayView twvCategory;
    TextView tvHeading;
    Fragment fragmentCategories;
    MyDataBase handler;
    DIctionaryContent content;
    String url,heading;
    LazyAdapter adapter;
    int position=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chapter);
    twvCategory=(TwoWayView)findViewById(R.id.twvCategory);
    tvHeading=(TextView)findViewById(R.id.tvHeading);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentCategories=fragmentManager.findFragmentById(R.id.fragmentCategories);
    fragmentTransaction.commit();

    handler = new MyDataBase(ChapterActivity.this);

    ArrayList<DIctionaryContent> chapterList = handler.getContent();
    adapter=new LazyAdapter(ChapterActivity.this,chapterList);
    twvCategory.setAdapter(adapter);

    AppDetails=handler.getDetails();
    content=AppDetails.get(0);
    url=content.getDlink();

    head=handler.getDesc(position+1);
    content=head.get(0);
    heading=content.getTitle();
    tvHeading.setText(heading);

    twvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position1, long id) {
            head=handler.getDesc(position1+1);
            content=head.get(0);
            heading=content.getTitle();
            tvHeading.setText(heading);

            Category category=new Category();
            Bundle bundle=new Bundle();
            bundle.putString("heading",heading);
            bundle.putInt("position",position1+1);
            bundle.putString("url",url);
            bundle.putBoolean("flag",true);
            category.setArguments(bundle);

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentCategories, category);
            fragmentTransaction.commit();
        }
    });
  }
}

Category.java

package com.focusmedica.bphandoab1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;

/**
 * Created by ashish on 11/15/2016.
 */

public class Category extends Fragment{
    ArrayList<DIctionaryContent> AppDetails=new ArrayList<>();
    ArrayList<DIctionaryContent> head=new ArrayList<>();
    ArrayList<DIctionaryContent> video_chapter=new ArrayList<>();
    GridView gvChapter;
    MyDataBase handler;
    DIctionaryContent content;
    Chapter_content_Adapter adapter;
    int position=0;
    String heading,url;
    boolean flag=false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main, container, false);
    gvChapter=(GridView)rootView.findViewById(R.id.gvChapter);

    handler=new MyDataBase(getActivity());

    Bundle bundle=getArguments();
    if(getArguments()!=null) {
        flag = bundle.getBoolean("flag");
        position = bundle.getInt("position");
        heading = bundle.getString("heading");
        url = bundle.getString("url");
    }

    if(flag==true){
        video_chapter = handler.getContent_Aspects(position);
        adapter = new Chapter_content_Adapter(getActivity(), video_chapter, url, heading, position);
        gvChapter.setAdapter(adapter);
    }else{
        AppDetails=handler.getDetails();
        content=AppDetails.get(0);
        url=content.getDlink();

        head=handler.getDesc(position+1);
        content=head.get(0);
        heading=content.getTitle();

        video_chapter = handler.getContent_Aspects(position+1);
        adapter = new Chapter_content_Adapter(getActivity(), video_chapter,url,heading,position+1);
        gvChapter.setAdapter(adapter);
    }
    return rootView;
  }
}

Chapter_content_Adapter

package com.focusmedica.bphandoab1;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ashish on 11/15/2016.
 */
 public class Chapter_content_Adapter extends BaseAdapter {
    ArrayList<DIctionaryContent> listcontent=new ArrayList<>();
    Context context;
    DIctionaryContent content;
    String folder2,folder1,heading,title;
    int listPos,id;
    String url;
    boolean mIsPremium=false;
    boolean[] isPremium;


public Chapter_content_Adapter(Context context, ArrayList<DIctionaryContent> listcontent, String url, String heading, int listPos){
    this.context = context;
    this.listcontent.clear();
    this.listcontent = listcontent;
    this.url=url;
    this.heading=heading;
    this.listPos=listPos;
    isPremium=new boolean[listcontent.size()];
}

@Override
public int getCount() {
    return listcontent.size();
}

@Override
public Object getItem(int position) {
    return listcontent.get(position);
}


@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return getCount();
}

@Override
public int getItemViewType(int position) {
    return position;
}

class ViewHolder {
    private TextView tv;
    ImageView img,ivDownload;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder viewHolder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.gridview_content, null);
        viewHolder = new ViewHolder();
        viewHolder.tv=(TextView) view.findViewById(R.id.tvGridViewTitle);
        viewHolder.img=(ImageView) view.findViewById(R.id.ivGridImage);
        viewHolder.ivDownload=(ImageView) view.findViewById(R.id.ivDownload);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }
    content = listcontent.get(position);
    id=content.getID();
    title=content.getTitle();
    viewHolder.tv.setText(title);

    folder1=content.getTitle();
    folder2 = folder1.replaceAll(" ", "%20");

    Glide.with(context).load(url+"/"+listPos+"/"+id+"_"+folder2+ "/Video.png").into(viewHolder.img);
    return view;
}

/*public void refresh(List<DIctionaryContent> list){
    listcontent.clear();
    listcontent.addAll(list);
    notifyDataSetChanged();
  }*/
}

0 个答案:

没有答案