我试图让ListView刷新每个按钮点击
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);
}
});
}
}
以上是我的主要活动,我所遇到的问题是评论的地方&#34; ADAPTER&#34;靠近代码的按钮。 项目适配器的代码将显示在下面
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;
}
}
使用评论标记是我在标题中提到错误的地方&#34; getSystemService&#34;。
当我在onClickListener Android Studio中移动itemAdapter时,建议我更改我的ItemAdapter中的参数&#34; Context&#34;到&#34; View.OnClickListener&#34;从而在标题中生成所呈现的错误。
提前感谢您的帮助我是java和android工作室的新手,如果发现任何愚蠢的错误,请原谅我。
答案 0 :(得分:0)
当你执行new View.OnClickListener() {
时,你创建了一个新的匿名内部类,this
在其中使用时,它是对它的实例的引用。
你应该改变
new ItemAdapter(this ,prayerNamez,prayerTimez);
为:
new ItemAdapter(mContext ,prayerNamez,prayerTimez);
然后将构造函数更改为接受Context
。