-1

Hi I am having problems with a switch case I have made. I want to open a new activity from a fragment.

This is my Switch:

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        //When the change password button is pressed...
        case R.id.btn_chg_password:
            showDialog();
            break;
        //When the Logout but is pressed...
        case R.id.btn_logout:
            logout();
            break;
        //When the write diary button is pressed...
        case R.id.btn_write:
            goToWriteDiary();
            break;
        //When the Read Diary button is pressed...
        case R.id.btn_read:
            Intent intent=new Intent(getActivity(), ReadFragment.class);
            startActivity(intent);
            break;
    }
}

And here is the Activity I wish to open with the Button = btn_read

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.Arrays;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ReadFragment extends AppCompatActivity
{
private RecyclerView recyclerView;
private ArrayList<Diary> data;
private DataAdapter adapter;
private SharedPreferences pref;

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

private void initViews()
{
    recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);

    String unique_id = pref.getString(Constants.UNIQUE_ID,"");
    loadJSON(unique_id);
}

private void loadJSON(String unique_id)
{
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    ServerRequest request = new ServerRequest();

    Diary diary = new Diary();
    diary.setUnique_id(unique_id);
    request.setOperation(Constants.CREATE_DIARY_OPERATION);

    RequestInterface requestInterface = retrofit.create(RequestInterface.class);
    Call<DiaryResponse> call = requestInterface.getDiary();
    call.enqueue(new Callback<DiaryResponse>()
    {
        @Override
        public void onResponse(Call<DiaryResponse> call, Response<DiaryResponse> response)
        {
            DiaryResponse diaryResponse = response.body();
            data = new ArrayList<>(Arrays.asList(diaryResponse.getDiary()));
            adapter = new DataAdapter(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<DiaryResponse> call, Throwable t)
        {
            Log.d("Error",t.getMessage());
        }
    });
}
}

Any help would be appreciated

Sallow
  • 41
  • 6

2 Answers2

0

EventBus is really helpful here. You can refer to my existing example to solve your problem.

Community
  • 1
  • 1
Hitesh Pamnani
  • 177
  • 3
  • 14
0

Start Activity code is seem to perfect.

May be Null Pointer Exception at below line in code of ReadFragment Activity.

 String unique_id = pref.getString(Constants.UNIQUE_ID,"");

pref is not initialize, so initialize in onCreate Method.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_read);
    pref= getSharedPreferences("your_pref_name", Context.MODE_PRIVATE);
    initViews();
}
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Thanks for the help, I have added this but it hasn't made a difference. I think there may be a problem with the php code. – Sallow Apr 08 '17 at 14:04