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.