SHARE
TWEET

Untitled

a guest Aug 13th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.animation.Animator;
  2. import android.animation.AnimatorListenerAdapter;
  3. import android.annotation.TargetApi;
  4. import android.app.ListActivity;
  5. import android.app.ProgressDialog;
  6. import android.content.Intent;
  7. import android.os.AsyncTask;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.support.annotation.Nullable;
  11. import android.support.v4.app.Fragment;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.AbsListView;
  17. import android.widget.AdapterView;
  18. import android.widget.ListView;
  19. import android.widget.ProgressBar;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import org.json.JSONArray;
  24. import org.json.JSONException;
  25. import org.json.JSONObject;
  26.  
  27. import java.util.HashMap;
  28.  
  29. /**
  30.  * Created by Arshad on 09-06-2017.
  31.  */
  32.  
  33. public class Tab1 extends Fragment {
  34.     SharedPreference pref;
  35.     ListView listView;
  36.     ListItemAdapter listItemAdapter;
  37.     //String[] names = {"Hrithik","Ranbir","AkshayKumar","Amir Khan","Shahidi Kapoor"};
  38.     //int[] images = {R.drawable.index1,R.drawable.index2,R.drawable.index3,R.drawable.index4,R.drawable.index5};
  39.     //int[] eventImages = {R.drawable.ev1,R.drawable.ev2,R.drawable.ev3,R.drawable.ev4,R.drawable.ev5};
  40.  
  41.     public String JSON_STRING;
  42.     int offset = 0;
  43.  
  44.     int[] eventId;
  45.     String[] startTime;
  46.     String[] eventImage;
  47.     String[] artistName;
  48.     String[] propic;
  49.     String[] eventName;
  50.     boolean[] follow;
  51.  
  52.     private View mProgressView;
  53.     private View mLoginFormView;
  54.  
  55.     ProgressBar loadingProgress;
  56.  
  57.     private boolean loading = true;
  58.  
  59.  
  60.  
  61.  
  62.  
  63.     @Nullable
  64.     @Override
  65.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  66.         return inflater.inflate(R.layout.tab1,container,false);
  67.     }
  68.  
  69.     @Override
  70.     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  71.         super.onViewCreated(view, savedInstanceState);
  72.         pref = new SharedPreference(getActivity());
  73.         listView = (ListView)view.findViewById(R.id.listView);
  74.         loadingProgress = new ProgressBar(getContext());
  75.  
  76.         listView.addFooterView(loadingProgress);
  77.  
  78.         mProgressView = view.findViewById(R.id.login_progress);
  79.         mLoginFormView = view.findViewById(R.id.listView);
  80.  
  81.         getContents();
  82.  
  83.         listView.setOnScrollListener(new AbsListView.OnScrollListener() {
  84.             @Override
  85.             public void onScrollStateChanged(AbsListView absListView, int i) {
  86.  
  87.             }
  88.  
  89.             @Override
  90.             public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  91.                 if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount != 0){
  92.                     if(loading==true){
  93.                         loading = false;
  94.                         getContents();
  95.                     }
  96.                 }
  97.  
  98.  
  99.             }
  100.  
  101.         });
  102.  
  103.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  104.             @Override
  105.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  106.                 Toast.makeText(getActivity (), "you can view the profile of :"+eventName[position], Toast.LENGTH_SHORT).show();
  107.  
  108.                 String eventId = ((TextView) view.findViewById(R.id.eventId)).getText().toString();
  109.                 Intent intent = new Intent(getContext(),ViewEventActivity.class);
  110.                 intent.putExtra("eventId",eventId);
  111.                 startActivity(intent);
  112.  
  113.  
  114.             }
  115.         });
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.     }
  123.     public void getContents(){
  124.         class GetContents extends AsyncTask<Void,Void,String>{
  125.             //ProgressDialog loading;
  126.             @Override
  127.             protected void onPreExecute() {
  128.                 //loading = ProgressDialog.show(getActivity(),"Loading..","Please wait",false,false);
  129.                 showProgress(true);
  130.  
  131.                 super.onPreExecute();
  132.             }
  133.  
  134.             @Override
  135.             protected String doInBackground(Void... voids) {
  136.                 RequestHandler rh = new RequestHandler();
  137.                 String s = rh.sendGetRequest(Config.KEY_URL_GET_TIMELINE+offset,pref.getApiKey());
  138.                 offset = offset+7;
  139.                 return s;
  140.             }
  141.  
  142.             @Override
  143.             protected void onPostExecute(String s) {
  144.                 if(isAdded()){
  145.                     getResources().getString(R.string.app_name);
  146.                 }
  147.                 super.onPostExecute(s);
  148.                 //loading.dismiss();
  149.                 showProgress(false);
  150.                 JSON_STRING = s;
  151.                 Log.v("RESPONSE:","response for timeline:"+JSON_STRING.toString());
  152.                 showResults();
  153.             }
  154.         }
  155.         GetContents g = new GetContents();
  156.         g.execute();
  157.     }
  158.  
  159.     public void showResults(){
  160.         try{
  161.             JSONObject json = new JSONObject(JSON_STRING);
  162.             boolean error = json.getBoolean("error");
  163.             if(error==false){
  164.                 //listView.removeFooterView(loadingProgress);
  165.                 JSONArray items = json.getJSONArray("events");
  166.                 eventId = new int[items.length()];
  167.                 eventName = new String[items.length()];
  168.                 startTime = new String[items.length()];
  169.                 eventImage = new String[items.length()];
  170.                 artistName = new String[items.length()];
  171.                 propic = new String[items.length()];
  172.                 follow = new boolean[items.length()];
  173.                 for(int i=0;i<items.length();i++){
  174.                     JSONObject c = items.getJSONObject(i);
  175.                     eventId[i] = c.getInt("eventId");
  176.                     eventName[i] = c.getString("eventName");
  177.                     startTime[i] = c.getString("startTime");
  178.                     eventImage[i] = c.getString("image");
  179.                     artistName[i] = c.getString("artistName");
  180.                     propic[i] = c.getString("proPic");
  181.                     follow[i] = c.getBoolean("follow");
  182.                 }
  183.                 listItemAdapter = new ListItemAdapter(getActivity(),eventId,eventName,startTime,eventImage,artistName,propic,follow);
  184.                 listView.setAdapter(listItemAdapter);
  185.  
  186.                 listItemAdapter.notifyDataSetChanged();
  187.             }else{
  188.                 Toast.makeText(getActivity(), "Sorry, there is technical problem, please contact system admins", Toast.LENGTH_SHORT).show();
  189.             }
  190.  
  191.         }catch(JSONException e){
  192.             e.printStackTrace();
  193.         }
  194.     }
  195.  
  196.  
  197.     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  198.     private void showProgress(final boolean show) {
  199.         // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  200.         // for very easy animations. If available, use these APIs to fade-in
  201.         // the progress spinner.
  202.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  203.             int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  204.  
  205.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  206.             mLoginFormView.animate().setDuration(shortAnimTime).alpha(
  207.                     show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
  208.                 @Override
  209.                 public void onAnimationEnd(Animator animation) {
  210.                     mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  211.                 }
  212.             });
  213.  
  214.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  215.             mProgressView.animate().setDuration(shortAnimTime).alpha(
  216.                     show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
  217.                 @Override
  218.                 public void onAnimationEnd(Animator animation) {
  219.                     mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  220.                 }
  221.             });
  222.         } else {
  223.             // The ViewPropertyAnimator APIs are not available, so simply show
  224.             // and hide the relevant UI components.
  225.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  226.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  227.         }
  228.     }
  229.  
  230. }
  231.    
  232. import android.app.Activity;
  233. import android.content.Context;
  234. import android.graphics.Bitmap;
  235. import android.graphics.BitmapFactory;
  236. import android.os.AsyncTask;
  237. import android.util.Log;
  238. import android.view.LayoutInflater;
  239. import android.view.View;
  240. import android.view.ViewGroup;
  241. import android.widget.BaseAdapter;
  242. import android.widget.Button;
  243. import android.widget.ImageView;
  244. import android.widget.TextView;
  245. import android.widget.Toast;
  246.  
  247. import com.squareup.picasso.Picasso;
  248.  
  249. import org.json.JSONException;
  250. import org.json.JSONObject;
  251.  
  252. import java.io.InputStream;
  253. import java.util.HashMap;
  254.  
  255.  
  256. /**
  257.  * Created by Arshad on 25-05-2017.
  258.  */
  259.  
  260. public class ListItemAdapter extends BaseAdapter {
  261.  
  262.     int[] eventId;
  263.     String[] eventNames;
  264.     String[] startTime;
  265.     String[] eventImage;
  266.     String[] artistName;
  267.     String[] propic;
  268.     boolean[] follow;
  269.  
  270.  
  271.  
  272.  
  273.     Context context;
  274.     ImageView imageViewLike;
  275. //    ImageView imageView;
  276. //    TextView textView;
  277.  
  278.     ImageView imageViewPropic;
  279.     ImageView imageViewEvent;
  280.     TextView textViewEventId,textViewEventName,textViewStartTime,textViewArtistName;
  281.  
  282.  
  283.     public ListItemAdapter(Context context, int[] evetid, String[] eventnames, String[] starttime, String[] eventImage, String[] artistName, String[] propic, boolean[] follow){
  284.         this.context = context;
  285.         this.eventId = evetid;
  286.         this.eventNames = eventnames;
  287.         this.startTime = starttime;
  288.         this.eventImage = eventImage;
  289.         this.artistName = artistName;
  290.         this.propic = propic;
  291.         this.follow = follow;
  292.  
  293.     }
  294.  
  295.     @Override
  296.     public int getCount() {
  297.         return eventId.length;
  298.     }
  299.  
  300.     @Override
  301.     public Object getItem(int i) {
  302.         return null;
  303.     }
  304.  
  305.     @Override
  306.     public long getItemId(int i) {
  307.         return i;
  308.     }
  309.  
  310.     @Override
  311.     public View getView(final int i, View view, ViewGroup viewGroup) {
  312.         LayoutInflater inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  313.         view = inflater.inflate(R.layout.list_items_announcements,null);
  314.        // imageView  = (ImageView)view.findViewById(R.id.proImage);
  315.         //imageViewEvent = (ImageView)view.findViewById(R.id.eventImage);
  316.         //textView = (TextView)view.findViewById(R.id.aname);
  317.  
  318.  
  319.         imageViewEvent = (ImageView) view.findViewById(R.id.eventImage);
  320.         imageViewPropic = (ImageView)view.findViewById(R.id.proImage);
  321.  
  322.         textViewEventId = (TextView)view.findViewById(R.id.eventId);
  323.         textViewEventName = (TextView)view.findViewById(R.id.eventName);
  324.         textViewStartTime = (TextView)view.findViewById(R.id.textViewStarts);
  325.         textViewArtistName = (TextView)view.findViewById(R.id.aname);
  326.         imageViewLike = (ImageView) view.findViewById(R.id.likeImageView);
  327.  
  328. //
  329. //        if(!eventImage[i].equals("") || eventImage[i]!=null) {
  330. //            new DownloadImageTask().execute(eventImage[i]);
  331. //        }
  332.  
  333.  
  334.         String eveImage = "http://"+Config.IP_ADDRESS+"/"+eventImage[i];
  335.         Picasso.with(context)
  336.                 .load(eveImage)
  337.                 .fit()
  338.                 .placeholder(R.drawable.loader)
  339.                 .error(R.drawable.default1)
  340.                 .into(imageViewEvent);
  341.         //if(!propic[i].equals(""))
  342.         //new DownloadImageTask2().execute(propic[i]);
  343.         //new DownloadImageTask(R.id.profileImage,view).execute(propic[i]);
  344.  
  345.  
  346.  
  347.         String profilePic = "someimage";
  348.         if(propic[i]!=null) {
  349.             if (propic[i].length() < Config.DIRECTORY_IMAGE_MAX_SIZE)
  350.                 profilePic = "http://" + Config.IP_ADDRESS + "/" + propic[i];
  351.             else
  352.                 profilePic = propic[i];
  353.         }
  354.         Picasso.with(context)
  355.                 .load(profilePic)
  356.                 .placeholder(R.drawable.default2)
  357.                 .error(R.drawable.default2)
  358.                 .into(imageViewPropic);
  359.  
  360.         textViewEventId.setText(String.valueOf(eventId[i]));
  361.         textViewArtistName.setText(artistName[i]);
  362.         textViewEventName.setText(eventNames[i]);
  363.         textViewStartTime.setText(startTime[i]);
  364.         boolean following = follow[i];
  365.  
  366.         if(following==false) {
  367.             imageViewLike.setTag(R.drawable.ic_like);
  368.             imageViewLike.setImageResource(R.drawable.ic_like);
  369.         }else{
  370.             imageViewLike.setTag(R.drawable.ic_liked);
  371.             imageViewLike.setImageResource(R.drawable.ic_liked);
  372.         }
  373. }
RAW Paste Data
Top