0

im trying to make a ListView refresh every button click

public class MainActivity extends AppCompatActivity {
    //private TextView txtPrayerTimes;
    /* **********GPS********** */
    Context mContext;

    /* **********ListView********** */
    ListView myPrayerList;
    double latitude = 21.6001;
    double longitude = 39.136;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //txtPrayerTimes = (TextView) findViewById(R.id.txtPrayerTimes);
        //Button getTime = (Button) findViewById(R.id.getTime);
        Button gpsBtn = (Button) findViewById(R.id.gpsBtn);
        /* **********ListView********** */
        myPrayerList = (ListView) findViewById(R.id.myPrayerList);


        /* **********GPS********** */
        mContext = this;

        ///BUTTON
        gpsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ContextCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED

                        && ActivityCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

                } else {
                    Toast.makeText(mContext, "You need have granted permission", Toast.LENGTH_SHORT).show();
                    GPSTracker gps = new GPSTracker(mContext, MainActivity.this);

                    // Check if GPS enabled

                    if (gps.canGetLocation()) {

                        latitude = gps.getLatitude();
                        longitude = gps.getLongitude();

                        // \n is for new line

                        Toast.makeText(getApplicationContext(),
                                "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    } else {

                        // Can't get location.

                        // GPS or network is not enabled.

                        // Ask user to enable GPS/network in settings.

                        gps.showSettingsAlert();
                    }
                }
                //////listView
                String[] prayerNamez;
                String[] prayerTimez;

                double timezone = (Calendar.getInstance().getTimeZone()
                        .getOffset(Calendar.getInstance().getTimeInMillis()))
                        / (1000 * 60 * 60);
                PrayTime prayers = new PrayTime();

                prayers.setTimeFormat(prayers.Time12);
                prayers.setCalcMethod(prayers.Makkah);
                prayers.setAsrJuristic(prayers.Shafii);
                prayers.setAdjustHighLats(prayers.AngleBased);
                int[] offsets = { 0, 0, 0, 0, 0, 0, 0 }; // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}
                prayers.tune(offsets);

                Date now = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(now);

                ArrayList prayerTimes = prayers.getPrayerTimes(cal, latitude,
                        longitude, timezone);
                ArrayList prayerNames = prayers.getTimeNames();

                /* **********ListView********** */
                prayerNamez = new String[5];
                prayerTimez = new String[5];

                for (int i = 0,j = 0;(i+j) < prayerNames.size();i++){
                    if ((i + j) == 1 ||  (i + j) == 4)
                        j++;
                    prayerNamez[i] = (String) prayerNames.get(i+j);
                    prayerTimez[i] = (String) prayerTimes.get(i+j);
                }
                ///ADAPTER
                ItemAdapter itemAdapter = new ItemAdapter(this,prayerNamez,prayerTimez);
                myPrayerList.setAdapter(itemAdapter);
            }
        });

    }
}

above is my main activity, the issue I'm having is where it is commented "ADAPTER" near the button of the code. the item adapter's code will be shown below

package com.example.majidalashari.myfirstapp2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



class ItemAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private String[] prayerNamez;
    private String[] prayerTimez;

//    ItemAdapter(Context c, String[] N, String[] T){
//        prayerNamez = N;
//        prayerTimez = T;
//        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    }

    ItemAdapter(View.OnClickListener c, String[] N, String[] T) {

        prayerNamez = N;
        prayerTimez = T;             /*vvvvvvvvvvvvvvvv*/
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                     /*^^^^^^^^^^^^^^^^*/
    }

    @Override
    public int getCount() {
        return prayerNamez.length;
    }

    @Override
    public Object getItem(int i) {
        return prayerNamez[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View View, ViewGroup parent) {
        View v = mInflater.inflate(R.layout.my_prayer_detail,null);
        TextView prayerNameTextView = v.findViewById(R.id.prayerNameTextView);
        TextView prayerTimeTextView = v.findViewById(R.id.prayerTimeTextView);

        String name = prayerNamez[i];
        String time = prayerTimez[i];

        prayerNameTextView.setText(name);
        prayerTimeTextView.setText(time);

        return v;
    }
}

marked, using comments, is where I'm getting the error mentioned in the title at exactly "getSystemService".

when I moved the itemAdapter inside of the onClickListener Android Studio suggested that I change a parameter in my ItemAdapter from "Context" to "View.OnClickListener" thus generating the presented error in the title.

thank you in advance for your help I am new to java and to android studio so forgive me if any dumb mistakes were found.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
  • 1
    change `View.OnClickListener` to `Context` , your commented adapter constructor is fine – Pavneet_Singh Oct 14 '17 at 04:31
  • 1
    Possible duplicate of [How to use getSystemService in a non-activity class?](https://stackoverflow.com/questions/4141555/how-to-use-getsystemservice-in-a-non-activity-class) – Vishal Yadav Oct 14 '17 at 05:10
  • 1
    [Android get activity from within anonymous class](https://stackoverflow.com/questions/31903355/android-get-activity-from-within-anonymous-class) – Pavneet_Singh Oct 14 '17 at 05:14

1 Answers1

0

When you do new View.OnClickListener() { you create a new anonymous inner class and this when used inside of it is a reference to an instance of it.

You should change

new ItemAdapter(this ,prayerNamez,prayerTimez);

to:

new ItemAdapter(mContext ,prayerNamez,prayerTimez);

And change your constructor back to accept Context.

Oleg
  • 6,124
  • 2
  • 23
  • 40
  • Thank you that worked, can you elaborate to what mContext actually represents? – Majid Alashari Oct 14 '17 at 16:20
  • @MajidAlashari It's in your code... `mContext = this;` it's an instance of `MainActivity` – Oleg Oct 14 '17 at 16:22
  • "this" being initialized outside of the OnClickListerner() will be referring to the main activity rather than an anonymous inner class, thanks again! – Majid Alashari Oct 14 '17 at 16:27
  • @MajidAlashari Yes, after your edit I see that you got it. See also the answer linked to by Pavneet for an alternative way to get the Outer `this`. – Oleg Oct 14 '17 at 16:30