回收视图不在第一个实例上显示数据

时间:2018-04-25 17:30:14

标签: java android

回收视图不显示数据显示空屏幕,当我返回并再次单击该图标时显示数据。不知道我为什么要面对这个问题。请检查下面的活动,适配器和片段代码。我是android的新手,我正在做我的顶点项目,如果你可以帮助我会很棒。

public class AppDetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("App Details");
    setContentView(R.layout.activity_app);
    AppDetailsFragment newFragment = new AppDetailsFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.recordAppFragmentContainer, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

public void refreshAppDuration(View v) {
    Intent intent = new Intent(this, AppDetailsActivity.class);
    startActivity(intent);
}

protected void onPause() {
    super.onPause();
}

protected void onStop() {
    super.onStop();
}

protected void onDestroy() {
    super.onDestroy();
}

public void onBackAppDuration(View v) {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

适配器

public class AppDetailsAdaptor extends RecyclerView.Adapter<AppDetailsAdaptor.ViewHolder> {
private List<AppUsage> mApps;
private Context mContext;

public AppDetailsAdaptor(Context context, List<AppUsage> apps) {
    mApps = apps;
    mContext = context;
}

private Context getContext() {
    return mContext;
}

@Override
public AppDetailsAdaptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View appUsageView = inflater.inflate(R.layout.item_app_details, parent, false);

    // Return a new holder instance
    AppDetailsAdaptor.ViewHolder viewHolder = new AppDetailsAdaptor.ViewHolder(appUsageView);
    return viewHolder;
}

@Override
public void onBindViewHolder(AppDetailsAdaptor.ViewHolder viewHolder, final int position) {
    // Get the data model based on position
    final AppUsage ch = mApps.get(position);

    // Set item views based on your views and data model
    viewHolder._appTextView.setText(ch.getAppName());
    viewHolder._durationTextView.setText(ch.getDurationInMinutes());
    viewHolder._dateTextView.setText(ch.getDate().substring(0,10));
    final String key = ch.getKey();
}


@Override
public int getItemCount() {
    return mApps.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView _appTextView;
    public TextView _durationTextView;
    public TextView _dateTextView;

        super(itemView);
        _appTextView = (TextView) itemView.findViewById(R.id.rvs_app_name);
        _durationTextView = (TextView) itemView.findViewById(R.id.rvs_app_duration);
        _dateTextView =(TextView) itemView.findViewById(R.id.rvs_app_date);
    }
}

}

片段

public class AppDetailsFragment extends Fragment {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
ArrayList<AppUsage> appList = new ArrayList<>();
ArrayList<AppUsage> appListTemp = new ArrayList<>();
ArrayList<AppUsage> appListAdaptor = new ArrayList<>();
ArrayList<AppUsage> appObject = new ArrayList<>();
ArrayList<AppUsage> appListDate = new ArrayList<>();
private AppDetailsFragment.OnFragmentInteractionListener mListener;
private RecyclerView rvApp;
private RecyclerView.Adapter appAdaptor;
private RecyclerView.LayoutManager eLayoutManager;
private EditText date;
private static final int   DIALOG_DATE_PICKER  = 100;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_app_list, container, false);
    rvApp = (RecyclerView) rootView.findViewById(R.id.rvAppList);
    rvApp.setHasFixedSize(true);
    eLayoutManager = new LinearLayoutManager(getActivity());
    rvApp.setLayoutManager(eLayoutManager);
    appList = getAllAppRecords();
    appAdaptor = new AppDetailsAdaptor(getActivity(), appList);
    rvApp.setAdapter(appAdaptor);
    return rootView;
}
public ArrayList<AppUsage> getAllAppRecords(){
    DatabaseReference ref = database.getReference();
    ref.child("AppUsage").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            appObject.clear();
            HashMap<String,Object> app = null;
            Iterator<DataSnapshot> items = dataSnapshot.getChildren().iterator();
            while(items.hasNext()){
                DataSnapshot item = items.next();
                Log.e("Listener",item.toString() );
                app =(HashMap<String, Object>) item.getValue();
                appObject.add(new AppUsage(app.get("appName").toString(),app.get("durationInMinutes").toString(),app.get("date").toString(),item.getKey()));
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return appObject;
}

0 个答案:

没有答案