2016-06-29 14 views
1

メモを書き込むためのアプリがあります。 MainActivityにTextViewがあります。ユーザーがメモを追加した後に非表示にしておき、ユーザーがすべてのメモを削除した後に表示するようにします。実際、メモを追加した後、TextViewは消えましたが、メモをすべて削除すると、別のアクティビティを入力してMainActivtiyに戻るまで、またはアプリケーションを閉じてから戻ってくるまで、TextViewは表示されません。RecyclerViewとTextView自体を更新した後に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> 
+0

私を助けるために誰かがここにありますか? – Androider

+0

うん。 'RealmController.with'を使うよう指示したチュートリアルに従わないでください。悪いチュートリアルです。 – EpicPandaForce

答えて

0

ソリューあなたと同じように、hereです。あなた自身のためにこのプロジェクトを実行しようとしてください

+0

あなたの答えをありがとう。私はそれを試みたが、それは私を助けなかった。 – Androider

関連する問題