Java tutorial
package net.jongrakko.zipsuri.fragment; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import com.like.LikeButton; import com.like.OnLikeListener; import com.yalantis.ucrop.UCrop; import net.jongrakko.zipsuri.R; import net.jongrakko.zipsuri.ZipsuriApplication; import net.jongrakko.zipsuri.activity.PhotoActivity; import net.jongrakko.zipsuri.activity.PostDetailActivity; import net.jongrakko.zipsuri.activity.PublicQuestionActivity; import net.jongrakko.zipsuri.manager.ProgressManger; import net.jongrakko.zipsuri.manager.ServerManager; import net.jongrakko.zipsuri.model.PublicAnswerModel; import net.jongrakko.zipsuri.model.PublicQuestionModel; import net.jongrakko.zipsuri.view.simpleimagedownloadview.SimpleImageDownloadView; import org.w3c.dom.Text; import java.io.File; import java.net.MalformedURLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class MyPublicQuestionFragment extends Fragment { private static final int PICK_PHOTO_FOR_AVATAR = 100; private static final int TAKE_PHOTO_FOR_AVATAR = 104; private View rootView; private PublicQuestionListAdapter mPublicQuestionListAdapter; private List<PublicQuestionModel> mPublicQuestionModels; private int[] licenseButtonIds = { R.id.buttonLicense0, R.id.buttonLicense1, R.id.buttonLicense2, R.id.buttonLicense3, R.id.buttonLicense4, R.id.buttonLicense5, R.id.buttonLicense6, R.id.buttonLicense7, R.id.buttonLicense8, R.id.buttonLicense9, R.id.buttonLicense10, R.id.buttonLicense11, R.id.buttonLicense12, R.id.buttonLicense13, R.id.buttonLicense14, R.id.buttonLicense15 }; @Override public void onResume() { super.onResume(); RecyclerView mRecyclerViewQuestion = (RecyclerView) rootView.findViewById(R.id.recyclerViewQuestion); mRecyclerViewQuestion.setLayoutManager(new LinearLayoutManager(getContext())); this.mPublicQuestionListAdapter = new PublicQuestionListAdapter(getContext()); mRecyclerViewQuestion.setAdapter(this.mPublicQuestionListAdapter); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (rootView == null) { rootView = inflater.inflate(R.layout.fragment_my_public_question, container, false); rootView.setBackgroundResource(R.color.colorBackgroundDark); ((TextView) rootView.findViewById(R.id.textKnowledgePoint)) .setText(ZipsuriApplication.getUserModel().getKnowledgePoint() * 10 + ""); RecyclerView mRecyclerViewQuestion = (RecyclerView) rootView.findViewById(R.id.recyclerViewQuestion); mRecyclerViewQuestion.setLayoutManager(new LinearLayoutManager(getContext())); this.mPublicQuestionListAdapter = new PublicQuestionListAdapter(getContext()); mRecyclerViewQuestion.setAdapter(this.mPublicQuestionListAdapter); if (!ZipsuriApplication.getUserModel().getIsExpert()) rootView.findViewById(R.id.layoutScore).setVisibility(View.GONE); } return rootView; } public void setData(List<PublicQuestionModel> mPublicQuestionModels) { this.mPublicQuestionModels = mPublicQuestionModels; setLayout(); } private void setLayout() { mPublicQuestionListAdapter.setData(mPublicQuestionModels); mPublicQuestionListAdapter.notifyDataSetChanged(); } private class PublicQuestionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private final int TYPE_HEAD = 1000; private final int TYPE_NORMAL = 1001; private List<PublicQuestionModel> mPublicQuestionModels; private Context context; private SimpleDateFormat format; private PublicQuestionListAdapter(Context context) { this.context = context; this.format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } @Override public int getItemViewType(int position) { return TYPE_NORMAL; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new QuestionListHolder( LayoutInflater.from(context).inflate(R.layout.adapter_question, parent, false)); } @Override public void onBindViewHolder(final RecyclerView.ViewHolder defaultHolder, int position) { final PublicQuestionModel mPublicQuestionModel; mPublicQuestionModel = mPublicQuestionModels.get(position); final QuestionListHolder holder = (QuestionListHolder) defaultHolder; holder.mLayoutAnswer.removeAllViews(); holder.mTextViewName.setText(mPublicQuestionModel.getUserId()); holder.mTextViewRegDate.setText(format.format(mPublicQuestionModel.getRegDate())); holder.mTextViewContent.setText(mPublicQuestionModel.getContent()); holder.mTextViewAnswerSize.setText(Long.toString(mPublicQuestionModel.getAnswerSize())); holder.mButtonSubmitAnswer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String answerContent = holder.mEditTextAnswer.getText().toString(); if (answerContent.equals("")) { ProgressManger.showToast("? ."); return; } final PublicAnswerModel answerModel = new PublicAnswerModel(); answerModel.setContent(answerContent); answerModel.setQuestionId(mPublicQuestionModel.getQuestionId()); ServerManager.uploadPublicAnswer(new ServerManager.OnServerListener<Void>() { @Override protected void onSuccess(Void data) { holder.mEditTextAnswer.setText(""); ProgressManger.showToast(" ? "); holder.mLayoutAnswer.addView(createAnswerView(answerModel, false)); getParentActivity().refreshData(); } }, answerModel); } }); holder.mButtonSubmitAnswerPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PublicAnswerModel mAnswerModelPhoto = new PublicAnswerModel(); mAnswerModelPhoto.setQuestionId(mPublicQuestionModel.getQuestionId()); showPhotoDialog(mAnswerModelPhoto); } }); holder.mLayoutViewAnswer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (holder.mLayoutAnswer.getChildCount() > 0) { holder.mLayoutAnswer.removeAllViews(); } else { ServerManager.getPublicAnswer( new ServerManager.OnServerListener<List<PublicAnswerModel>>(getContext()) { @Override protected void onSuccess(List<PublicAnswerModel> data) { for (PublicAnswerModel answerModel : data) { boolean isMyQuestion = ZipsuriApplication.getUserModel().getUserId() .equals(mPublicQuestionModel.getUserId()) ? true : false; holder.mLayoutAnswer .addView(createAnswerView(answerModel, isMyQuestion)); } } }, mPublicQuestionModel); } } }); } @Override public int getItemCount() { if (mPublicQuestionModels == null) { return 0; } return mPublicQuestionModels.size(); } private void setData(List<PublicQuestionModel> mPublicQuestionModels) { this.mPublicQuestionModels = mPublicQuestionModels; notifyDataSetChanged(); } private List<PublicQuestionModel> getData() { return mPublicQuestionModels; } private View createAnswerView(final PublicAnswerModel mAnswerModel, boolean isMyQuestion) { View mViewAnswer = LayoutInflater.from(getContext()).inflate(R.layout.adapter_public_answer, null, false); SimpleImageDownloadView imageView = (SimpleImageDownloadView) mViewAnswer .findViewById(R.id.imageViewAnswerImage); LikeButton mLikeButton = (LikeButton) mViewAnswer.findViewById(R.id.star_button); final TextView mLikeText = (TextView) mViewAnswer.findViewById(R.id.star_text); Log.d("URL", mAnswerModel.getImage0Url()); Log.d("URL", "eyyseysyesey"); if (mAnswerModel.getImage0Url() != null && !mAnswerModel.getImage0Url().equals("")) { Log.d("URL", mAnswerModel.getImage0Url()); imageView.setVisibility(View.VISIBLE); try { imageView.setImageURL(mAnswerModel.getImage0Url(), 4); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList<String> urls = new ArrayList<String>(); urls.add(mAnswerModel.getImage0Url()); Intent intent = new Intent(getContext(), PhotoActivity.class); intent.putExtra("photo_array", urls); intent.putExtra(PhotoActivity.INTENT_PHOTO, mAnswerModel.getImage0()); startActivity(intent); } }); } catch (MalformedURLException e) { e.printStackTrace(); } } else { imageView.setVisibility(View.GONE); } /// adopted is currently disabled. /* if(mAnswerModel.getIsAdopted()) { ((TextView) mViewAnswer.findViewById(R.id.textViewName)).setText(" [?? ] "+ mAnswerModel.getUserId()); }else*/ ((TextView) mViewAnswer.findViewById(R.id.textViewName)).setText(mAnswerModel.getUserId()); /// mLikeText.setText(mAnswerModel.getLike_count() + ""); ((TextView) mViewAnswer.findViewById(R.id.textViewAnswer)).setText(mAnswerModel.getContent()); mLikeButton.setOnLikeListener(new OnLikeListener() { @Override public void liked(LikeButton likeButton) { ServerManager.likePublicAnswer(new ServerManager.OnServerListener<Void>() { @Override protected void onSuccess(Void data) { mLikeText.setText((Integer.parseInt(mLikeText.getText().toString()) + 1) + ""); } }, mAnswerModel); } @Override public void unLiked(LikeButton likeButton) { ServerManager.unLikePublicAnswer(new ServerManager.OnServerListener<Void>() { @Override protected void onSuccess(Void data) { mLikeText.setText((Integer.parseInt(mLikeText.getText().toString()) - 1) + ""); } }, mAnswerModel); } }); return mViewAnswer; } public class QuestionListHolder extends RecyclerView.ViewHolder { public ViewGroup mLayoutAnswer; public ViewGroup mLayoutViewAnswer; public TextView mTextViewName; public TextView mTextViewRegDate; public TextView mTextViewContent; public TextView mTextViewAnswerSize; public Button mButtonSubmitAnswer; public ImageButton mButtonSubmitAnswerPhoto; public EditText mEditTextAnswer; public Button mbuttonAdoptAnswer; public QuestionListHolder(View itemView) { super(itemView); mLayoutAnswer = (ViewGroup) itemView.findViewById(R.id.layoutAnswer); mTextViewName = (TextView) itemView.findViewById(R.id.textViewName); mTextViewRegDate = (TextView) itemView.findViewById(R.id.textViewRegDate); mTextViewContent = (TextView) itemView.findViewById(R.id.textViewContent); mLayoutViewAnswer = (ViewGroup) itemView.findViewById(R.id.layoutViewAnswer); mTextViewAnswerSize = (TextView) itemView.findViewById(R.id.textViewAnswerSize); mButtonSubmitAnswer = (Button) itemView.findViewById(R.id.buttonSubmitAnswer); mButtonSubmitAnswerPhoto = (ImageButton) itemView.findViewById(R.id.buttonSubmitAnswerPhoto); mEditTextAnswer = (EditText) itemView.findViewById(R.id.editTextAnswer); mbuttonAdoptAnswer = (Button) itemView.findViewById(R.id.button_adopt_answer); } } public class SubmitQuestionHolder extends RecyclerView.ViewHolder { public EditText mEditTextQuestion; public Button mButtonSubmitQuestion; public SubmitQuestionHolder(View itemView) { super(itemView); mEditTextQuestion = (EditText) itemView.findViewById(R.id.editTextQuestion); mButtonSubmitQuestion = (Button) itemView.findViewById(R.id.buttonSubmitQuestion); } } } public PublicQuestionActivity getParentActivity() { return (PublicQuestionActivity) super.getActivity(); } private PublicAnswerModel mAnswerModelPhoto; //<--Start of Pick Image private void pickImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR); } File takeFile; private void takeImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takeFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Zisuri_tmp_" + System.currentTimeMillis() + ".jpg"); Uri saveUri = Uri.fromFile(takeFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, saveUri); // set the image file name startActivityForResult(intent, TAKE_PHOTO_FOR_AVATAR); } private void showPhotoDialog(PublicAnswerModel mAnswerModel) { mAnswerModelPhoto = mAnswerModel; new AlertDialog.Builder(getContext()).setTitle("?") .setItems(new String[] { " ?", "" }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: takeImage(); break; case 1: pickImage(); break; } } }).show(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && requestCode == UCrop.REQUEST_CROP) { File cropFile = new File(UCrop.getOutput(data).getPath()); mAnswerModelPhoto.setImage0(cropFile.getAbsolutePath()); ServerManager.uploadPublicAnswer(new ServerManager.OnServerListener<Void>() { @Override protected void onSuccess(Void data) { getParentActivity().refreshData(); ProgressManger.showToast(" !"); } }, mAnswerModelPhoto); } else if (resultCode == Activity.RESULT_OK && requestCode == PICK_PHOTO_FOR_AVATAR) { UCrop.of(data.getData(), Uri.fromFile( new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg"))) .withMaxResultSize(1920, 1920).start(getContext(), this); } else if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PHOTO_FOR_AVATAR) { UCrop.of(Uri.fromFile(takeFile), Uri.fromFile( new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg"))) .withMaxResultSize(1920, 1920).start(getContext(), this); } else if (resultCode == UCrop.RESULT_ERROR) { final Throwable cropError = UCrop.getError(data); Log.d("mox", "croperror" + cropError.toString()); } } }