Android Retrofit: How to display JSON data into one cardview only?

时间:2019-01-07 13:55:58

标签: android android-recyclerview retrofit2 android-cardview

I have one last problem in displaying my output. What I want is that per grammar error there will be a message and list of suggestion per cardview.

This my code in the result adapter:

public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ResultViewHolder>{

Context mContext;
List<Photo> photoList = new ArrayList<>();
ArrayList<Match> matchList = new ArrayList<>();
ArrayList<Replacement> replacementList = new ArrayList<>();
ArrayList<Object> data = new ArrayList<>();

int VIEW_TYPE_MATCH = 1;
int VIEW_TYPE_REPLACEMENT = 2;
public ResultAdapter (Context mContext, ArrayList<Match> matchList){
    this.mContext = mContext;

    for (int a =0; a < matchList.size(); a++){

        for (int b = 0; b < matchList.get(a).getReplacements().size(); b++){
            this.data.add(matchList.get(a));
            this.data.add(matchList.get(a).getReplacements().get(b));
        }


        Log.i("Adapter", "Replacement " + matchList.get(a).getReplacements().size());
    }


    Log.i("Adapter", "MatchList " + matchList.size());

}


@Override
public ResultViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.result_card, viewGroup, false);
    getViewType(i);
    return new ResultViewHolder(view);
}

@Override
public void onBindViewHolder(ResultViewHolder resultViewHolder, int i) {

    Object value = data.get(i);
    if (value instanceof Match) {
        Match match = (Match) value;
        resultViewHolder.mBadErrorTextView.setText(match.getMessage());
    } else {
        Replacement replacement = (Replacement) value;
        resultViewHolder.mBetterErrorTextView.setText(replacement.getValue());
    }
}

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

public class ResultViewHolder extends RecyclerView.ViewHolder{

    @BindView(R.id.ErrorBadTextView)
    TextView mBadErrorTextView;

    @BindView(R.id.ErrorBetterTextView)
    TextView mBetterErrorTextView;


    public ResultViewHolder(@NonNull View itemView) {
        super(itemView);

        ButterKnife.bind(this, itemView);
    }

}

private int getViewType(int position){

    Object value = data.get(position);
    if (value instanceof Match){
        return VIEW_TYPE_MATCH;
    }else {
        return VIEW_TYPE_REPLACEMENT;
    }
}

}

Fragment:

public class Tab1Fragment_GrammarChecker extends Fragment {

private static final String TAG = "Tab1Fragment";

@BindView(R.id.InputTextEditText)
EditText mInputGrammarEditText;

// @BindView(R.id.MatchRecyclerView) // RecyclerView mMatchRecyclerView;

@BindView(R.id.ErrorsRecyclerView)
RecyclerView mErrorsRecyclerView;

List<Match> matches1 = new ArrayList<>();
public ArrayList<Match> matchArrayList = new ArrayList<>();
public ArrayList<Match> matchData = new ArrayList<>();

public ArrayList<Match> matches = new ArrayList<>();
public List<Replacement> replacementsList = new ArrayList<>();

public static String userInput;
public String apiKey = "";
public String language = "en-US";

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1_grammar_checker, container, false);

    ButterKnife.bind(this, view);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    mErrorsRecyclerView.setLayoutManager(layoutManager);
    mErrorsRecyclerView.setItemAnimator(new DefaultItemAnimator());

    return view;
}

@OnClick(R.id.checkGrammarButton)
public void setOnClick(View view){
    userInput = mInputGrammarEditText.getText().toString().trim();
    if(TextUtils.isEmpty(userInput)){
        matches.clear();
        Snackbar snackbar = Snackbar
                .make(view, R.string.fieldEmptyMessage, Snackbar.LENGTH_LONG);

        snackbar.show();
        return;
    }
    if (userInput.length() >= 51){
        matches.clear();
        Snackbar snackbar = Snackbar
                .make(view, R.string.fieldEmptyMessage, Snackbar.LENGTH_LONG);

        snackbar.show();
        return;
    }
    loadJson();
}

public void loadJson(){
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://api.grammarbot.io")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    Service serviceAPI = retrofit.create(Service.class);
    Call<GrammarBotMain> loadErrorsCall = serviceAPI.readErrorsGrammar(apiKey, userInput, language);

    final ProgressDialog progressDialog;
    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMax(100);
    progressDialog.setMessage(getResources().getString(R.string.progressDialogLoadingResultMessage));
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();

    loadErrorsCall.enqueue(new Callback<GrammarBotMain>() {
        @Override
        public void onResponse(Call<GrammarBotMain> call, Response<GrammarBotMain> response) {
            progressDialog.dismiss();
            GrammarBotMain grammarBotMain = response.body();

            matches.clear();
            matches = grammarBotMain.getMatches();

            if (matches.size() == 0){

                final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
                Snackbar snackbar = Snackbar
                        .make(getView(), R.string.noError, Snackbar.LENGTH_LONG);

                snackbar.show();
                return;
            }
            final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);

            mErrorsRecyclerView.setAdapter(new ResultAdapter(getContext(), matches));
            Log.i("Fragment", "SUCCESS " + matches.size());
        }

        @Override
        public void onFailure(Call<GrammarBotMain> call, Throwable t) {
            progressDialog.dismiss();
            Log.e("Error: ", t.getMessage());
        }
    });
}
}

Output that I want:

Cardview1:(First error) Message Suggestions: s1, s2, s3, s4

Cardview2: (For another error) Message Suggestions: s1, s2, s3, s4

... and so on

This is what my output right now looks like: enter image description here

enter image description here

enter image description here

0 个答案:

没有答案