Last active
May 30, 2019 06:02
-
-
Save ajaykumar2017/d8d309babcd05bf4b7e19aa0cf38ec4e to your computer and use it in GitHub Desktop.
Fragment Actions Show Posts and Create Post Activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To post something you need to create an activity from where you can send data to server however you can do the same from | |
your fragment but if you want to show posts in fragment, you have to do so. | |
Here I have used Volley Post Request so you should add Volley Libraray | |
implementation 'com.android.support:design:28.0.0' | |
implementation 'com.android.support:support-v4:28.0.0' | |
implementation 'com.android.support:cardview-v7:28.0.0' | |
implementation 'cc.cloudist.acplibrary:library:1.2.1' | |
implementation 'com.android.volley:volley:1.1.1' | |
implementation 'de.hdodenhof:circleimageview:3.0.0' | |
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0' | |
implementation 'com.android.support:recyclerview-v7:28.0.0' | |
implementation 'com.mindorks.android:prdownloader:0.4.0' | |
implementation 'com.google.code.gson:gson:2.8.5' | |
package com.tecent.student_assessment; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Color; | |
import android.net.Uri; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.theartofdev.edmodo.cropper.CropImage; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.util.HashMap; | |
import java.util.Map; | |
import cc.cloudist.acplibrary.ACProgressConstant; | |
import cc.cloudist.acplibrary.ACProgressFlower; | |
public class CreatePostQueryDoubts extends AppCompatActivity { | |
ImageView iv_cancel_post, iv_profile_image, iv_set_image; | |
TextView tv_username, tv_btn_post, path_image; | |
EditText et_post_text; | |
SharedPreferences sharedPreferences; | |
ACProgressFlower dialog; | |
RequestQueue requestQueue; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_create_post_query); | |
iv_cancel_post = findViewById(R.id.iv_cancel_post); | |
iv_profile_image = findViewById(R.id.iv_profile_image); | |
tv_username = findViewById(R.id.tv_username); | |
et_post_text = findViewById(R.id.et_post_text); | |
tv_btn_post = findViewById(R.id.tv_btn_post); | |
path_image = findViewById(R.id.path_image); | |
iv_set_image=findViewById(R.id.iv_set_image); | |
requestQueue = Volley.newRequestQueue(this); | |
sharedPreferences = this.getSharedPreferences("studentAssessment", Context.MODE_PRIVATE); | |
String userdp=sharedPreferences.getString("userdp",""); | |
requestQueue.add(ExtraFunctions.createImageRequestFromUrl(ExtraFunctions.serverurl+"userdp/"+userdp, iv_profile_image)); | |
String name = sharedPreferences.getString("name", ""); | |
String userid=sharedPreferences.getString("userid",""); | |
tv_username.setText(name); | |
//progress dialog | |
dialog = new ACProgressFlower.Builder(this) | |
.direction(ACProgressConstant.DIRECT_CLOCKWISE) | |
.themeColor(Color.WHITE).text("Uploading....") | |
.fadeColor(Color.BLACK).build(); | |
dialog.setCancelable(false); | |
iv_cancel_post.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
finish(); | |
} | |
}); | |
tv_btn_post.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (et_post_text.getText().toString().trim().length() < 5) { | |
Toast.makeText(CreatePostQueryDoubts.this, "Post should contain at least 5 characters", Toast.LENGTH_SHORT).show(); | |
} else { | |
dialog.show(); | |
if (ExtraFunctions.isNetworkStatusAvialable(CreatePostQueryDoubts.this)) { | |
if (iv_set_image.getDrawable() != null) | |
volleyImageRequest(); | |
else | |
volleyTestWithoutImage(); | |
} | |
else { | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, "No internet connection!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
}); | |
et_post_text.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
} | |
@Override | |
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
if (et_post_text.getText().toString().trim().length() >= 5) { | |
tv_btn_post.setBackgroundResource(R.color.green); | |
} else if (et_post_text.getText().toString().trim().length() < 5) { | |
tv_btn_post.setBackgroundResource(R.color.smalldarkgrey); | |
} | |
// Toast.makeText(CreatePostQueryDoubts.this, String.valueOf(et_post_text.getText().toString().trim().length()), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
} | |
}); | |
} | |
//image picker | |
public void openImagePicker(View view) { | |
CropImage.activity().setAspectRatio(1, 1) | |
.start(this); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && data != null) { | |
CropImage.ActivityResult result = CropImage.getActivityResult(data); | |
if (resultCode == RESULT_OK) { | |
Uri resultUri = result.getUri(); | |
final String path = resultUri.getPath(); | |
//now we have path of file we should compress image | |
iv_set_image.setImageBitmap(compressImage(path)); | |
String[] paths = path.split("/"); | |
path_image.setText(paths[paths.length-1]); | |
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { | |
//on error | |
dialog.dismiss(); | |
Toast.makeText(this, "An error occured! Please try again later.", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
public Bitmap compressImage(String path) { | |
byte[] imageData = null; | |
try { | |
final int THUMBNAIL_SIZE = 256; | |
FileInputStream fis = new FileInputStream(new File(path)); | |
Bitmap imageBitmap = BitmapFactory.decodeStream(fis); | |
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, | |
THUMBNAIL_SIZE, false); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); | |
imageData = baos.toByteArray(); | |
Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); | |
return bmp; | |
} catch (Exception ex) { | |
return null; | |
} | |
} | |
public void volleyImageRequest(){ | |
String url = ExtraFunctions.serverurl+"postdoubts.php"; | |
VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() { | |
@Override | |
public void onResponse(NetworkResponse response) { | |
String resultResponse = new String(response.data); | |
try { | |
JSONObject result = new JSONObject(resultResponse); | |
String status = result.getString("result"); | |
if (status.equals("successful")) | |
{ | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, "Post uploaded successfully", Toast.LENGTH_SHORT).show(); | |
finish(); | |
} | |
if (status.equals("error")){ | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (JSONException e) { | |
dialog.dismiss(); | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, "Volley Error", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
@Override | |
protected Map<String, String> getParams() { | |
Map<String, String> params = new HashMap<>(); | |
params.put("userid", sharedPreferences.getString("userid","")); | |
params.put("posttext", et_post_text.getText().toString().replace("'","\\'")); | |
params.put("email", sharedPreferences.getString("email","")); | |
params.put("name", sharedPreferences.getString("name","")); | |
return params; | |
} | |
@Override | |
protected Map<String, DataPart> getByteData() { | |
Map<String, DataPart> params = new HashMap<>(); | |
// file name could found file base or direct access from real path | |
// for now just get bitmap data from ImageView | |
params.put("doubtimage", new DataPart("file_image.jpg", AppHelper.getFileDataFromDrawable(getBaseContext(), iv_set_image.getDrawable()), "image/jpeg")); | |
//DataPart second parameter is byte[] | |
return params; | |
} | |
}; | |
VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(multipartRequest); | |
} | |
//Google volley | |
public void volleyTestWithoutImage() { | |
String url = ExtraFunctions.serverurl + "postdoubts1.php"; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
jsonParser(response); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, error.toString(), Toast.LENGTH_SHORT).show(); | |
// Toast.makeText(CreatePostQueryDoubts.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
protected Map<String, String> getParams() { | |
Map<String, String> MyData = new HashMap<String, String>(); | |
MyData.put("userid", sharedPreferences.getString("userid","")); | |
MyData.put("posttext", et_post_text.getText().toString().replace("'","\\'")); | |
MyData.put("email", sharedPreferences.getString("email","")); | |
MyData.put("name", sharedPreferences.getString("name","")); | |
return MyData; | |
} | |
}; | |
requestQueue.add(stringRequest); | |
} | |
public void jsonParser(String jsontext) { | |
try { | |
JSONObject emp = (new JSONObject(jsontext)); | |
String result = emp.getString("result"); | |
if (result.equals("successful")) { | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, "Post uploaded successfully", Toast.LENGTH_SHORT).show(); | |
finish(); | |
} | |
if (result.equals("error")) { | |
dialog.dismiss(); | |
Toast.makeText(CreatePostQueryDoubts.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception exception) { | |
dialog.dismiss(); | |
exception.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tecent.student_assessment; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.widget.SwipeRefreshLayout; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.LinearSmoothScroller; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.DisplayMetrics; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.downloader.Progress; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Objects; | |
import cc.cloudist.acplibrary.ACProgressConstant; | |
import cc.cloudist.acplibrary.ACProgressFlower; | |
public class HomeFragment extends Fragment { | |
TextView textView; | |
SharedPreferences sharedPreferences, sharedPreferencesLike; | |
SwipeRefreshLayout swipeRefreshLayout; | |
ProgressBar progressBar; | |
RequestQueue requestQueue; | |
RecyclerView mRecyclerViewPostHome; | |
ACProgressFlower dialog; | |
ArrayList<String> useridlist; | |
ArrayList<String> userdplist; | |
ArrayList<String> usernamelist; | |
ArrayList<String> userbranchlist; | |
ArrayList<String> posttimelist; | |
ArrayList<String> postidlist; | |
ArrayList<String> posttextlist; | |
ArrayList<String> postfilelist; | |
ArrayList<String> subjectlist; | |
String userid; | |
@Nullable | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.homefragment, container, false); | |
textView=view.findViewById(R.id.textView); | |
useridlist = new ArrayList<String>(); | |
userdplist = new ArrayList<String>(); | |
usernamelist = new ArrayList<String>(); | |
posttimelist = new ArrayList<String>(); | |
postidlist = new ArrayList<String>(); | |
userbranchlist = new ArrayList<String>(); | |
posttextlist = new ArrayList<String>(); | |
postfilelist = new ArrayList<String>(); | |
subjectlist=new ArrayList<String>(); | |
sharedPreferences=this.getActivity().getSharedPreferences("studentAssessment", Context.MODE_PRIVATE); | |
sharedPreferencesLike=this.getActivity().getSharedPreferences("postLikes", Context.MODE_PRIVATE); | |
String email=sharedPreferences.getString("email",""); | |
String passw=sharedPreferences.getString("passw",""); | |
String name=sharedPreferences.getString("name",""); | |
userid=sharedPreferences.getString("userid",""); | |
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout); | |
progressBar=view.findViewById(R.id.progress_bar); | |
dialog = new ACProgressFlower.Builder(getActivity()) | |
.direction(ACProgressConstant.DIRECT_CLOCKWISE) | |
.themeColor(Color.WHITE) | |
.fadeColor(Color.BLACK).build(); | |
dialog.setCancelable(false); | |
mRecyclerViewPostHome=view.findViewById(R.id.recycler_view_post_home); | |
mRecyclerViewPostHome.setHasFixedSize(false); | |
// mRecyclerViewPostHome.setLayoutManager(new LinearLayoutManager(getActivity())); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()) { | |
@Override | |
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { | |
LinearSmoothScroller smoothScroller = new LinearSmoothScroller(getActivity()) { | |
private static final float SPEED = 300f; | |
@Override | |
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { | |
return SPEED / displayMetrics.densityDpi; | |
} | |
}; | |
smoothScroller.setTargetPosition(position); | |
startSmoothScroll(smoothScroller); | |
} | |
}; | |
mRecyclerViewPostHome.setLayoutManager(layoutManager); | |
requestQueue = Volley.newRequestQueue(getActivity()); | |
try{ | |
if (ExtraFunctions.isNetworkStatusAvialable(getActivity())) { | |
volleyPostDataRequest(); | |
} else { | |
Toast.makeText(getActivity(), "No Internet Connection!", Toast.LENGTH_SHORT).show(); | |
} | |
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
if (ExtraFunctions.isNetworkStatusAvialable(getActivity())) | |
volleyPostDataRequest(); | |
else { | |
swipeRefreshLayout.setRefreshing(false); | |
progressBar.setVisibility(View.GONE); | |
Toast.makeText(getActivity(), "No Internet Connection!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
catch (Exception e){ | |
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
return view; | |
} | |
public void volleyPostDataRequest() { | |
try { | |
String url = ExtraFunctions.serverurl + "YourAdapterFile.php"; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
swipeRefreshLayout.setRefreshing(false); | |
progressBar.setVisibility(View.GONE); | |
try { | |
JSONObject emp = (new JSONObject(response)); | |
String result = emp.getString("result"); | |
if (result.equals("successful")) { | |
useridlist.clear(); | |
usernamelist.clear(); | |
userdplist.clear(); | |
posttimelist.clear(); | |
userbranchlist.clear(); | |
postidlist.clear(); | |
posttextlist.clear(); | |
postfilelist.clear(); | |
subjectlist.clear(); | |
JSONArray useridarray = emp.getJSONArray("userid"); | |
JSONArray usernamearray = emp.getJSONArray("name"); | |
JSONArray userbrancharray = emp.getJSONArray("branch"); | |
JSONArray userdparray = emp.getJSONArray("userdp"); | |
JSONArray posttimearray = emp.getJSONArray("posttime"); | |
JSONArray postdoubtidarray = emp.getJSONArray("postid"); | |
JSONArray posttextarray = emp.getJSONArray("posttext"); | |
JSONArray postfilearray = emp.getJSONArray("filename"); | |
JSONArray postsubjectarray = emp.getJSONArray("subject"); | |
if (useridarray != null) { | |
int len = useridarray.length(); | |
for (int i = 0; i < len; i++) { | |
useridlist.add(useridarray.get(i).toString()); | |
usernamelist.add(usernamearray.get(i).toString()); | |
userdplist.add(userdparray.get(i).toString()); | |
posttimelist.add(posttimearray.get(i).toString()); | |
userbranchlist.add(userbrancharray.get(i).toString()); | |
postidlist.add(postdoubtidarray.get(i).toString()); | |
posttextlist.add(posttextarray.get(i).toString()); | |
postfilelist.add(postfilearray.get(i).toString()); | |
subjectlist.add(postsubjectarray.get(i).toString()); | |
} | |
} | |
MyRecyclerHomePostsAdapter homePostsAdapter = new MyRecyclerHomePostsAdapter(sharedPreferencesLike, dialog, requestQueue, getActivity(), userid, useridlist, userdplist, usernamelist, | |
userbranchlist, posttimelist, postfilelist, postidlist, posttextlist, subjectlist); | |
mRecyclerViewPostHome.setAdapter(homePostsAdapter); | |
} | |
} catch (Exception exception) { | |
Toast.makeText(getActivity(), exception.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
swipeRefreshLayout.setRefreshing(false); | |
progressBar.setVisibility(View.GONE); | |
// Toast.makeText(getActivity(), "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
protected Map<String, String> getParams() { | |
Map<String, String> MyData = new HashMap<String, String>(); | |
return MyData; | |
} | |
}; | |
requestQueue.add(stringRequest); | |
} | |
catch(Exception e){ | |
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
// @Override | |
// public void onResume() { | |
// volleyPostDataRequest(); | |
// super.onResume(); | |
// } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v4.widget.SwipeRefreshLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/swipeRefreshLayout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_view_post_home" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/smalldarkgrey" /> | |
<ProgressBar | |
android:id="@+id/progress_bar" | |
android:layout_width="match_parent" | |
android:layout_height="100dp" | |
android:gravity="center_horizontal" | |
android:layout_marginTop="120dp" | |
android:layout_below="@+id/recycler_view_post_home" | |
android:indeterminateTint="@color/colorPrimary"/> | |
</LinearLayout> | |
</android.support.v4.widget.SwipeRefreshLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_marginBottom="10dp" | |
android:elevation="0dp" | |
app:cardElevation="0dp" | |
android:orientation="vertical" | |
android:background="?android:attr/selectableItemBackground" | |
android:id="@+id/ivcardview" | |
> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:paddingBottom="5dp" | |
android:paddingTop="10dp"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:paddingLeft="10dp" | |
android:paddingRight="10dp" | |
android:orientation="horizontal"> | |
<LinearLayout | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="0.2"> | |
<de.hdodenhof.circleimageview.CircleImageView | |
android:id="@+id/iv_profile_image" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:layout_marginTop="2dp" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" | |
android:src="@drawable/ic_boy" | |
app:civ_border_color="@color/smalldarkgrey" | |
app:civ_border_width="0.5dp" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="0.7" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/ivusername" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="User Name" | |
android:textSize="15sp" | |
android:textStyle="bold" | |
android:maxLines="1" | |
android:textColor="@color/verydarkgrey"/> | |
<TextView | |
android:id="@+id/iv_datetime_branch_subject" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Date and Time" | |
android:textSize="10sp" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="0.1" | |
android:id="@+id/ivmenubtn"> | |
<ImageView | |
android:id="@+id/iv_menu" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
android:background="?attr/selectableItemBackground" | |
android:src="@drawable/ic_more_vert_black_24dp" /> | |
</LinearLayout> | |
</LinearLayout> | |
<TextView | |
android:id="@+id/iv_post_text" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="10dp" | |
android:layout_marginBottom="5dp" | |
android:text="this is a post" | |
android:textSize="17sp" | |
android:textColor="@color/verydarkgrey" | |
android:paddingLeft="10dp" | |
android:paddingRight="10dp" | |
android:maxLines="5"/> | |
<ImageView | |
android:id="@+id/iv_post_image" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:adjustViewBounds="true" | |
android:src="@drawable/loader" | |
android:scaleType="fitCenter" | |
/> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="0.8dp" | |
android:layout_below="@+id/textView2" | |
android:background="@color/lightgrey1" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="30dp" | |
android:orientation="horizontal" | |
android:id="@+id/ivbuttons"> | |
<android.support.v7.widget.CardView | |
android:id="@+id/ivcvlike" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.33" | |
app:cardElevation="0dp"> | |
<ImageView | |
android:id="@+id/ivlike" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:src="@drawable/ic_001_thumb_up" | |
android:layout_gravity="center_horizontal" | |
android:padding="5dp" | |
android:foreground="?attr/selectableItemBackground" | |
android:clickable="true"/> | |
</android.support.v7.widget.CardView> | |
<android.support.v7.widget.CardView | |
android:id="@+id/ivcvreply" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.33" | |
app:cardElevation="0dp"> | |
<ImageView | |
android:id="@+id/ivreply" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:src="@drawable/ic_002_chat_comment_oval_speech_bubble_with_text_lines" | |
android:padding="5dp" | |
android:foreground="?attr/selectableItemBackground" | |
android:clickable="true"/> | |
</android.support.v7.widget.CardView> | |
<android.support.v7.widget.CardView | |
android:id="@+id/ivcvshare" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.33" | |
app:cardElevation="0dp"> | |
<ImageView | |
android:id="@+id/ivshare" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:src="@drawable/ic_003_share" | |
android:padding="5dp" | |
android:foreground="?attr/selectableItemBackground" | |
android:clickable="true"/> | |
</android.support.v7.widget.CardView> | |
</LinearLayout> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="0.8dp" | |
android:layout_below="@+id/textView2" | |
android:background="@color/lightgrey" /> | |
</LinearLayout> | |
</android.support.v7.widget.CardView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This contains Holder and contents of each RecyclerView Item which you want to show | |
package com.tecent.student_assessment; | |
import android.app.Dialog; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.graphics.Color; | |
import android.net.Uri; | |
import android.os.Handler; | |
import android.support.annotation.NonNull; | |
import android.support.graphics.drawable.VectorDrawableCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v4.graphics.drawable.DrawableCompat; | |
import android.support.v7.widget.PopupMenu; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.EditText; | |
import android.widget.ImageView; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.android.volley.toolbox.Volley; | |
import com.downloader.Error; | |
import com.downloader.OnDownloadListener; | |
import com.downloader.PRDownloader; | |
import org.json.JSONObject; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Map; | |
import cc.cloudist.acplibrary.ACProgressConstant; | |
import cc.cloudist.acplibrary.ACProgressFlower; | |
public class MyRecyclerHomePostsAdapter extends RecyclerView.Adapter<MyRecyclerHomePostsAdapter.HomePostsHolder> { | |
private Context mContext; | |
private ArrayList<String> museridlist; | |
private ArrayList<String> muserdplist; | |
private ArrayList<String> musernamelist; | |
private ArrayList<String> mposttimelist; | |
private ArrayList<String> mpostidlist; | |
private ArrayList<String> muserbranchlist; | |
private ArrayList<String> mposttextlist; | |
private ArrayList<String> mpostfilelist; | |
private ArrayList<String> msubjectlist; | |
String mMyuserid; | |
RequestQueue mRequestQueue; | |
ACProgressFlower mDialog; | |
SharedPreferences mSharedPreferencesLike; | |
public MyRecyclerHomePostsAdapter(SharedPreferences sharedPreferencesLike, ACProgressFlower dialog, RequestQueue requestQueue, Context context, String myuserid, ArrayList<String> useridlist, | |
ArrayList<String> userdplist, ArrayList<String> usernamelist, | |
ArrayList<String> userbranchlist, ArrayList<String> posttimelist, | |
ArrayList<String> postfilelist, ArrayList<String> postidlist, | |
ArrayList<String> posttextlist, ArrayList<String> subjectlist) { | |
mSharedPreferencesLike = sharedPreferencesLike; | |
mDialog = dialog; | |
mRequestQueue = requestQueue; | |
mContext = context; | |
mMyuserid = myuserid; | |
museridlist = useridlist; | |
muserdplist = userdplist; | |
musernamelist = usernamelist; | |
mposttimelist = posttimelist; | |
mpostidlist = postidlist; | |
muserbranchlist = userbranchlist; | |
mposttextlist = posttextlist; | |
mpostfilelist = postfilelist; | |
msubjectlist = subjectlist; | |
} | |
@NonNull | |
@Override | |
public HomePostsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { | |
View view = LayoutInflater.from(mContext).inflate(R.layout.indiview_posts, viewGroup, false); | |
return new HomePostsHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull final HomePostsHolder homePostsHolder, int position) { | |
final String mUserId = museridlist.get(position); | |
final String mUserdp = muserdplist.get(position); | |
final String mUserName = musernamelist.get(position); | |
String mPostDateTime = mposttimelist.get(position); | |
final String mBranch = muserbranchlist.get(position); | |
final String mPostId = mpostidlist.get(position); | |
final String mPostText = mposttextlist.get(position); | |
final String mPostFile = mpostfilelist.get(position); | |
final String mSubject = msubjectlist.get(position); | |
String viewLink = ""; | |
String type = ""; | |
mRequestQueue.add(ExtraFunctions.createImageRequestFromUrl(ExtraFunctions.serverurl + "userdp/" + mUserdp, homePostsHolder.iv_profile_image)); | |
homePostsHolder.iv_username.setText(mUserName); | |
homePostsHolder.ivdate_and_branch_subject.setText(mPostDateTime + " " + "\u2022" + " " + mBranch.toUpperCase() + " " + "\u2022" + " " + mSubject); | |
homePostsHolder.iv_post_text.setText(mPostText); | |
if (mSharedPreferencesLike.getString(mPostId, "").equals("liked")) { | |
// homePostsHolder.ivlike.setBackgroundResource(R.drawable.ic_thumb_up_blue); | |
DrawableCompat.setTint(homePostsHolder.ivlike.getDrawable(), ContextCompat.getColor(mContext, R.color.colorPrimary)); | |
} else { | |
DrawableCompat.setTint(homePostsHolder.ivlike.getDrawable(), ContextCompat.getColor(mContext, R.color.vectordrawablelike)); | |
} | |
if (!mPostFile.equals("")) { | |
if (mPostFile.substring(mPostFile.lastIndexOf('.') + 1).equals("pdf") || | |
mPostFile.substring(mPostFile.lastIndexOf('.') + 1).equals("PDF")) { | |
type = "pdf"; | |
// viewLink = "https://docs.google.com/viewer?url=" + ExtraFunctions.serverurl + "posts/" + mPostFile; | |
viewLink = ExtraFunctions.serverurl + "pdfViewer/web/viewer.html?file=" + "/project/posts/" + mPostFile; | |
mRequestQueue.add(ExtraFunctions.createImageRequestFromUrl(ExtraFunctions.serverurl + | |
"posts/pdfthumbnail/" + mPostFile.replace(mPostFile.substring(mPostFile.lastIndexOf('.') + 1), "") + "jpg", homePostsHolder.iv_post_image)); | |
} else { | |
type = "image"; | |
viewLink = ExtraFunctions.serverurl + "posts/" + mPostFile; | |
mRequestQueue.add(ExtraFunctions.createImageRequestFromUrl(ExtraFunctions.serverurl + "posts/" + mPostFile, homePostsHolder.iv_post_image)); | |
} | |
} else { | |
homePostsHolder.iv_post_image.setVisibility(View.GONE); | |
} | |
//Onclick show profile start | |
homePostsHolder.iv_username.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (mMyuserid.equals(mUserId)){ | |
Intent intProfile=new Intent(mContext,Profile.class); | |
intProfile.putExtra("profile","MyProfile"); | |
mContext.startActivity(intProfile); | |
}else{ | |
Intent intProfile=new Intent(mContext,Profile.class); | |
intProfile.putExtra("userid",mUserId); | |
intProfile.putExtra("username",mUserName); | |
intProfile.putExtra("userdp",mUserdp); | |
intProfile.putExtra("userbranch",mBranch); | |
intProfile.putExtra("profile","OtherProfile"); | |
mContext.startActivity(intProfile); | |
} | |
} | |
}); | |
homePostsHolder.iv_profile_image.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (mMyuserid.equals(mUserId)){ | |
Intent intProfile=new Intent(mContext,Profile.class); | |
intProfile.putExtra("profile","MyProfile"); | |
mContext.startActivity(intProfile); | |
}else{ | |
Intent intProfile=new Intent(mContext,Profile.class); | |
intProfile.putExtra("userid",mUserId); | |
intProfile.putExtra("username",mUserName); | |
intProfile.putExtra("userdp",mUserdp); | |
intProfile.putExtra("userbranch",mBranch); | |
intProfile.putExtra("profile","OtherProfile"); | |
mContext.startActivity(intProfile); | |
} | |
} | |
}); | |
homePostsHolder.ivdate_and_branch_subject.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (mMyuserid.equals(mUserId)){ | |
Intent intProfile=new Intent(mContext,Profile.class); | |
intProfile.putExtra("profile","MyProfile"); | |
mContext.startActivity(intProfile); | |
}else{ | |
Intent intProfile=new Intent(mContext,Profile.class); | |
intProfile.putExtra("userid",mUserId); | |
intProfile.putExtra("username",mUserName); | |
intProfile.putExtra("userdp",mUserdp); | |
intProfile.putExtra("userbranch",mBranch); | |
intProfile.putExtra("profile","OtherProfile"); | |
mContext.startActivity(intProfile); | |
} | |
} | |
}); | |
//Onclick show profile end | |
//onClick post image start | |
final String finalViewLink = viewLink; | |
final String finalType = type; | |
homePostsHolder.iv_post_image.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (finalType.equals("pdf")) { | |
// Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(finalViewLink)); | |
// mContext.startActivity(browserIntent); | |
Intent intent = new Intent(mContext, ImagePdfWebView.class); | |
intent.putExtra("viewLink", finalViewLink); | |
mContext.startActivity(intent); | |
} else { | |
Intent intent = new Intent(mContext, ImagePdfWebView.class); | |
intent.putExtra("viewLink", finalViewLink); | |
mContext.startActivity(intent); | |
} | |
} | |
}); | |
//onclick post image end | |
//menu part start | |
homePostsHolder.iv_menu_btn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
final Dialog dialogMenu = new Dialog(mContext); | |
// Include dialog.xml file | |
dialogMenu.setContentView(R.layout.menu_custom_dialog_home_posts); | |
// Set dialog title | |
dialogMenu.setTitle("Custom Dialog"); | |
TextView tv_delete_post = (TextView) dialogMenu.findViewById(R.id.tv_delete_post); | |
TextView tv_share_post = (TextView) dialogMenu.findViewById(R.id.tv_share_post); | |
TextView save_to_notes = (TextView) dialogMenu.findViewById(R.id.save_to_notes); | |
TextView tv_turn_on_post_notif = (TextView) dialogMenu.findViewById(R.id.tv_turn_on_post_notif); | |
TextView tv_report_post = (TextView) dialogMenu.findViewById(R.id.tv_report_post); | |
dialogMenu.show(); | |
if (!mUserId.equals(mMyuserid)) | |
tv_delete_post.setVisibility(View.GONE); | |
else { | |
save_to_notes.setVisibility(View.GONE); | |
tv_turn_on_post_notif.setVisibility(View.GONE); | |
tv_report_post.setVisibility(View.GONE); | |
} | |
tv_delete_post.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (ExtraFunctions.isNetworkStatusAvialable(mContext)) { | |
String url = ExtraFunctions.serverurl + "deleteHomePosts.php"; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
JSONObject emp = (new JSONObject(response)); | |
String result = emp.getString("result"); | |
if (result.equals("successful")) { | |
Toast.makeText(mContext, "Post Deleted successfully", Toast.LENGTH_SHORT).show(); | |
} | |
if (result.equals("error")) { | |
Toast.makeText(mContext, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show(); | |
// Toast.makeText(CreatePostQueryDoubts.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
protected Map<String, String> getParams() { | |
Map<String, String> MyData = new HashMap<String, String>(); | |
MyData.put("postid", mPostId); | |
return MyData; | |
} | |
}; | |
mRequestQueue.add(stringRequest); | |
} else { | |
Toast.makeText(mContext, "No Internet Connection!", Toast.LENGTH_SHORT).show(); | |
} | |
dialogMenu.dismiss(); | |
} | |
}); | |
tv_share_post.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (ExtraFunctions.isNetworkStatusAvialable(mContext)) { | |
mDialog.show(); | |
final String extrastring = "\n\nThis Post is posted by " + mUserName + " in \'Student Assessment\' app." + | |
"\nTo get such updates regularly download \'Student Assessment\' app now." + | |
"\nhttps://play.google.com/store/apps/details?id=com.tecent.student_assessment"; | |
mDialog.dismiss(); | |
Intent intentShareFile = new Intent(Intent.ACTION_SEND); | |
intentShareFile.setType("text/plain"); | |
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, | |
"Sharing File..."); | |
intentShareFile.putExtra(Intent.EXTRA_TEXT, mPostText + extrastring); | |
mContext.startActivity(Intent.createChooser(intentShareFile, "Share")); | |
} else { | |
Toast.makeText(mContext, "No Internet Connection!", Toast.LENGTH_SHORT).show(); | |
} | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
dialogMenu.dismiss(); | |
} | |
}, 500); | |
} | |
}); | |
tv_report_post.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
final Dialog dialogReport = new Dialog(mContext); | |
// Include dialog.xml file | |
dialogReport.setContentView(R.layout.report_post_home); | |
// Set dialog title | |
dialogReport.setTitle("Custom Dialog"); | |
final RadioGroup radioGroup = (RadioGroup) dialogReport.findViewById(R.id.radiogroup); | |
final RadioButton spamnpromotion = (RadioButton) dialogReport.findViewById(R.id.spamnpromotion); | |
final RadioButton violencenharassment = (RadioButton) dialogReport.findViewById(R.id.violencenharassment); | |
final RadioButton wrong = (RadioButton) dialogReport.findViewById(R.id.wrong); | |
final RadioButton copyrightviolation = (RadioButton) dialogReport.findViewById(R.id.copyrightviolation); | |
final RadioButton shouldnotbe = (RadioButton) dialogReport.findViewById(R.id.shouldnotbe); | |
final EditText et_explain = (EditText) dialogReport.findViewById(R.id.et_explain); | |
TextView tv_btn_cancel = (TextView) dialogReport.findViewById(R.id.tv_btn_cancel); | |
final TextView tv_btn_report = (TextView) dialogReport.findViewById(R.id.tv_btn_report); | |
int selectedIdRadio = radioGroup.getCheckedRadioButtonId(); | |
// find the radiobutton by returned id | |
final RadioButton radioButton = (RadioButton) dialogReport.findViewById(selectedIdRadio); | |
tv_btn_cancel.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
dialogReport.dismiss(); | |
} | |
}); | |
tv_btn_report.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
String reportValue = ""; | |
if (spamnpromotion.isChecked()) { | |
reportValue = "Spam/Promotion"; | |
} else if (violencenharassment.isChecked()) { | |
reportValue = "Violence/Harassment"; | |
} else if (wrong.isChecked()) { | |
reportValue = "Wrong Information"; | |
} else if (copyrightviolation.isChecked()) { | |
reportValue = "Copyright Violation"; | |
} else if (shouldnotbe.isChecked()) { | |
reportValue = "Should not be in Student Assessment App"; | |
} | |
final String explainValue = et_explain.getText().toString(); | |
if (!(radioGroup.getCheckedRadioButtonId() == -1)) { | |
String url = ExtraFunctions.serverurl + "reportPost.php"; | |
final String finalReportValue = reportValue; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
JSONObject emp = (new JSONObject(response)); | |
String result = emp.getString("result"); | |
if (result.equals("successful")) { | |
dialogReport.dismiss(); | |
Toast.makeText(mContext, "Post reported successsfully.", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(mContext, "An error occured.", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception e) { | |
Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
mDialog.dismiss(); | |
Toast.makeText(mContext, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
protected Map<String, String> getParams() { | |
Map<String, String> MyData = new HashMap<String, String>(); | |
MyData.put("postid", mPostId); | |
MyData.put("userid", mMyuserid); | |
MyData.put("reason", finalReportValue); | |
MyData.put("explanation", explainValue); | |
return MyData; | |
} | |
}; | |
mRequestQueue.add(stringRequest); | |
} else { | |
Toast.makeText(mContext, "Please select a reason.", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
dialogMenu.dismiss(); | |
dialogReport.show(); | |
} | |
}); | |
} | |
}); | |
//menu part end | |
//like part start | |
homePostsHolder.ivlike.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (ExtraFunctions.isNetworkStatusAvialable(mContext)) { | |
if (mSharedPreferencesLike.getString(mPostId, "").equals("liked")) { | |
String url = ExtraFunctions.serverurl + "unLikePosts.php"; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
JSONObject emp = (new JSONObject(response)); | |
String result = emp.getString("result"); | |
if (result.equals("successful")) { | |
SharedPreferences.Editor speLike = mSharedPreferencesLike.edit(); | |
speLike.putString(mPostId, ""); | |
speLike.apply(); | |
DrawableCompat.setTint(homePostsHolder.ivlike.getDrawable(), ContextCompat.getColor(mContext, R.color.vectordrawablelike)); | |
homePostsHolder.ivlike.setPadding(1, 0, 1, 0); | |
Toast.makeText(mContext, "Post Unliked", Toast.LENGTH_SHORT).show(); | |
} | |
if (result.equals("error")) { | |
Toast.makeText(mContext, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show(); | |
// Toast.makeText(CreatePostQueryDoubts.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
protected Map<String, String> getParams() { | |
Map<String, String> MyData = new HashMap<String, String>(); | |
MyData.put("postid", mPostId); | |
MyData.put("userid", mMyuserid); | |
return MyData; | |
} | |
}; | |
mRequestQueue.add(stringRequest); | |
} else { | |
String url = ExtraFunctions.serverurl + "likePosts.php"; | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
JSONObject emp = (new JSONObject(response)); | |
String result = emp.getString("result"); | |
if (result.equals("alreadyLiked")) { | |
Toast.makeText(mContext, "Post already Liked by you", Toast.LENGTH_SHORT).show(); | |
} | |
if (result.equals("successful")) { | |
SharedPreferences.Editor speLike = mSharedPreferencesLike.edit(); | |
speLike.putString(mPostId, "liked"); | |
speLike.apply(); | |
DrawableCompat.setTint(homePostsHolder.ivlike.getDrawable(), ContextCompat.getColor(mContext, R.color.colorPrimary)); | |
homePostsHolder.ivlike.setPadding(1, 0, 1, 0); | |
Toast.makeText(mContext, "Post Liked successfully", Toast.LENGTH_SHORT).show(); | |
} | |
if (result.equals("error")) { | |
Toast.makeText(mContext, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show(); | |
// Toast.makeText(CreatePostQueryDoubts.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show(); | |
} | |
}) { | |
protected Map<String, String> getParams() { | |
Map<String, String> MyData = new HashMap<String, String>(); | |
MyData.put("postid", mPostId); | |
MyData.put("userid", mMyuserid); | |
return MyData; | |
} | |
}; | |
mRequestQueue.add(stringRequest); | |
} | |
} else { | |
Toast.makeText(mContext, "No Internet Connection!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
//like part end | |
//comment part start | |
homePostsHolder.ivreply.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Intent postIntent=new Intent(mContext,CommentsPostHome.class); | |
postIntent.putExtra("postid",mPostId); | |
mContext.startActivity(postIntent); | |
} | |
}); | |
//comment part end | |
homePostsHolder.ivshare.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (ExtraFunctions.isNetworkStatusAvialable(mContext)) { | |
mDialog.show(); | |
final String extrastring = "\n\nThis Post is posted by " + mUserName + " in \'Student Assessment\' app." + | |
"\nTo get such updates regularly download \'Student Assessment\' app now." + | |
"\nhttps://play.google.com/store/apps/details?id=com.tecent.student_assessment"; | |
if (mPostFile.equals("")) { | |
mDialog.dismiss(); | |
Intent intentShareFile = new Intent(Intent.ACTION_SEND); | |
intentShareFile.setType("text/plain"); | |
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, | |
"Sharing File..."); | |
intentShareFile.putExtra(Intent.EXTRA_TEXT, mPostText + extrastring); | |
mContext.startActivity(Intent.createChooser(intentShareFile, "Share")); | |
} else { | |
File f; | |
if (mPostFile.substring(mPostFile.lastIndexOf('.') + 1).equals("pdf") || | |
mPostFile.substring(mPostFile.lastIndexOf('.') + 1).equals("PDF")) { | |
f = new File(ExtraFunctions.serverurl + | |
"posts/pdfthumbnail/" + mPostFile.replace(mPostFile.substring(mPostFile.lastIndexOf('.') + 1), "") + "jpg"); | |
} else { | |
f = new File(ExtraFunctions.serverurl + "posts/" + mPostFile); | |
} | |
if (f.exists()) { | |
mDialog.dismiss(); | |
Intent intentShareFile = new Intent(Intent.ACTION_SEND); | |
intentShareFile.setType("image/*"); | |
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" | |
+ ExtraFunctions.rootdir + "posts/" + mPostFile)); | |
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, | |
"New post from \'Hints\' app."); | |
intentShareFile.putExtra(Intent.EXTRA_TEXT, mPostText + extrastring); | |
mContext.startActivity(Intent.createChooser(intentShareFile, "Share")); | |
} else { | |
String dUrl; | |
final String fDnld; | |
if (mPostFile.substring(mPostFile.lastIndexOf('.') + 1).equals("pdf") || | |
mPostFile.substring(mPostFile.lastIndexOf('.') + 1).equals("PDF")) { | |
dUrl = ExtraFunctions.serverurl + | |
"posts/pdfthumbnail/" + mPostFile.replace(mPostFile.substring(mPostFile.lastIndexOf('.') + 1), "") + "jpg"; | |
fDnld = mPostFile.replace(mPostFile.substring(mPostFile.lastIndexOf('.') + 1), "") + "jpg"; | |
} else { | |
dUrl = ExtraFunctions.serverurl + "posts/" + mPostFile; | |
fDnld = mPostFile; | |
} | |
PRDownloader.download(dUrl, | |
ExtraFunctions.rootdir + "posts/", | |
fDnld) | |
.build() | |
.start(new OnDownloadListener() { | |
@Override | |
public void onDownloadComplete() { | |
mDialog.dismiss(); | |
Intent intentShareFile = new Intent(Intent.ACTION_SEND); | |
intentShareFile.setType("image/*"); | |
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" | |
+ ExtraFunctions.rootdir + "posts/" + fDnld)); | |
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, | |
"Sharing File..."); | |
intentShareFile.putExtra(Intent.EXTRA_TEXT, mPostText + extrastring); | |
mContext.startActivity(Intent.createChooser(intentShareFile, "Share")); | |
} | |
@Override | |
public void onError(Error error) { | |
mDialog.dismiss(); | |
} | |
}); | |
} | |
} | |
} else { | |
Toast.makeText(mContext, "No Internet Connection!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return museridlist.size(); | |
} | |
public class HomePostsHolder extends RecyclerView.ViewHolder { | |
public ImageView iv_profile_image, iv_post_image, iv_menu_btn, ivlike, ivreply, ivshare; | |
public TextView iv_username, ivdate_and_branch_subject, iv_post_text; | |
public HomePostsHolder(@NonNull View itemView) { | |
super(itemView); | |
iv_profile_image = itemView.findViewById(R.id.iv_profile_image); | |
iv_post_image = itemView.findViewById(R.id.iv_post_image); | |
iv_username = itemView.findViewById(R.id.ivusername); | |
ivdate_and_branch_subject = itemView.findViewById(R.id.iv_datetime_branch_subject); | |
iv_post_text = itemView.findViewById(R.id.iv_post_text); | |
iv_menu_btn = itemView.findViewById(R.id.iv_menu); | |
ivlike = itemView.findViewById(R.id.ivlike); | |
ivreply = itemView.findViewById(R.id.ivreply); | |
ivshare = itemView.findViewById(R.id.ivshare); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment