java - 我更新 RecyclerView 和 TextView 本身后,TextView 不出现

标签 java android

我有一个用于写笔记的应用程序。在MainActivity中有一个TextView,我希望它在用户添加注释后隐藏,并且我希望它在用户删除所有注释后可见。事实上,在我添加注释后,TextView 就消失了,但是当我删除所有注释时,直到我进入另一个 Activity 然后返回到 MainActivtiy,或者当我关闭应用程序然后返回到它时,TextView 才会出现。

* MainActivity.java:

package com.twitter.i_droidi.notah;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import io.realm.OrderedRealmCollection;
import io.realm.Realm;
import io.realm.RealmChangeListener;

public class MainActivity extends AppCompatActivity {

    private NotesAdapter adapter;
    private Realm realm;
    protected RecyclerView recyclerView;
    private RealmChangeListener realmChangeListener;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.realm = RealmController.with(this).getRealm();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        textView = (TextView) findViewById(R.id.textEmpty);

        updateView();

        setupRecycler();

        realmChangeListener = new RealmChangeListener() {
            @Override
            public void onChange(Object element) {
                RealmController.with(MainActivity.this).refresh();
            }
        };

        setRealmAdapter(RealmController.with(this).getNotes());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent createNote = new Intent(MainActivity.this, CreateNote.class);
                startActivity(createNote);
            }
        });
    }

    public void setRealmAdapter(OrderedRealmCollection<Notes> notes)
    {
        RealmNotesAdapter realmNotesAdapter = new RealmNotesAdapter(this.getApplicationContext(), notes);
        adapter.setRealmAdapter(realmNotesAdapter);
        adapter.notifyDataSetChanged();
    }

    private void setupRecycler()
    {
        recyclerView.setHasFixedSize(true);

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new NotesAdapter(this);
        recyclerView.setAdapter(adapter);
    }

    protected void updateView()
    {
        if(RealmController.with(MainActivity.this).hasNotes())
        {
            recyclerView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.GONE);
        }
        else
        {
            if(recyclerView != null)
            {
                recyclerView.setVisibility(View.GONE);
                textView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())
        {
            case R.id.night_mode:
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                recreate();
                return true;

            case R.id.about:
                Intent intent = new Intent(MainActivity.this, About.class);
                startActivity(intent);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

* NotesAdapter.java:

package com.twitter.i_droidi.notah;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import io.realm.Realm;
import io.realm.RealmResults;

public class NotesAdapter extends RealmRecyclerViewAdapter<Notes> {

    protected final Context context;
    protected Realm realm;

    public NotesAdapter(Context context)
    {
        this.context = context;
    }

    @Override
    public NotesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
        return new NotesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        realm = RealmController.getInstance().getRealm();

        final Notes note = getItem(position);
        final NotesViewHolder myHolder = (NotesViewHolder) holder;

        //myHolder.noteTitle.setText(note.getTitle());
        myHolder.noteBody.setText(note.getBody());
        myHolder.card.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                final RealmResults<Notes> results = realm.where(Notes.class).findAll();

                /*Notes note = results.get(position);
                final String title = note.getTitle();*/

                AlertDialog.Builder deleteDialog = new AlertDialog.Builder(context);
                deleteDialog.setTitle(R.string.delete_note_title);
                deleteDialog.setMessage(R.string.delete_note_msg);
                deleteDialog.setIcon(android.R.drawable.ic_delete);
                deleteDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        realm.beginTransaction();
                        results.deleteFromRealm(position);
                        realm.commitTransaction();

                        MainActivity obj = new MainActivity();
                        obj.updateView();

                        notifyDataSetChanged();

                        Toast noteDeleted = Toast.makeText(context, R.string.note_deleted, Toast.LENGTH_SHORT);
                        noteDeleted.show();

                    }
                });
                deleteDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        // DO NOT DO ANYTHING.
                    }
                });
                deleteDialog.show();

                return false;
            }
        });

        myHolder.card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, ViewNote.class);
                intent.putExtra("id", position);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {

        if(getRealmAdapter() != null)
        {
            return getRealmAdapter().getCount();
        }

        return 0;
    }

    private static class NotesViewHolder extends RecyclerView.ViewHolder
    {
        protected CardView card;
        //protected TextView noteTitle;
        protected TextView noteBody;

        public NotesViewHolder(View view)
        {
            super(view);

            card = (CardView) view.findViewById(R.id.card_view);
            //noteTitle = (TextView) view.findViewById(R.id.note_title);
            noteBody = (TextView) view.findViewById(R.id.note_body);
        }
    }
}

* Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

<RelativeLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:elevation="6dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <FrameLayout
        android:id="@+id/framemain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="2.5dp"
            android:paddingRight="2.5dp"
            android:adjustViewBounds="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:text="Add some notes."
            android:textSize="22sp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textEmpty" />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="16dp"
                android:clickable="true"
                android:src="@android:drawable/ic_menu_add"
                android:tag="bg_tint_accent_color_selector_lighter"
                app:elevation="6dp"
                app:pressedTranslationZ="12dp" />

    </FrameLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

最佳答案

与您类似的解决方案是 here 。尝试自己运行这个项目

关于java - 我更新 RecyclerView 和 TextView 本身后,TextView 不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105953/

相关文章:

java - spring MVC helloworld 应用程序显示 CannotLoadBeanClassException

java - 多线程环境下如何解决内存泄漏?

java - 下载网页。 wget 正常,java 失败

android - ProgressDialog 不会立即出现

java - 如何从 jni 获取字符串资源 ID?

java - 我可以在 Ant 脚本中声明和初始化变量吗?

java - Eclipse 遗漏的其他 Java 建议

android - 丢失已发布应用程序的证书

java - 将对象从类传递到 Activity

安卓通知点击 : Fragment transition when app not in foreground