リストビューでアイテムをクリックしたときに、アクティビティからメインフラグメントに戻ろうとしています。また、ビューにテキストを戻す必要があります。残念ながら私はまだそれを成功させることができませんでした。私が下に投稿しているコードは、 "return fragobj;"というエラーを出しています。Onclicklistenerアクティビティが動作していないときにフラグメントに戻る
ボイド結果タイプで
ActivityViewメソッドから値を返すことはできません:
package com.example.curtisboylan.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class DevicesList extends AppCompatActivity {
private DeviceListAdapter dlAdapter;
private ArrayList<String> listType;
private ArrayList<Integer> listIcon;
private ListView gridView;
private String type;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devices_list);
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("devicetype");
type = message;
// Toast.makeText(this, "DEBUGTYPE: " + message, Toast.LENGTH_SHORT).show();
setTitle("Choose Your Device");
prepareList();
// prepared arraylist and passed it to the Adapter class
dlAdapter = new DeviceListAdapter(DevicesList.this, listType, listIcon);
// Set custom adapter to gridview
gridView = (ListView) this.findViewById(R.id.GridViewDeviceList);
gridView.setAdapter(dlAdapter);
dlAdapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Bundle bundle = new Bundle();
bundle.putString("device", dlAdapter.getItem(position));
SearchScreen fragobj = new SearchScreen();
fragobj.setArguments(bundle);
return fragobj;
}
});
}
public void prepareList() {
listType = new ArrayList<String>();
if(type.equals("iphone")) {
listType.add("iPhone 7");
listType.add("iPhone 6S");
listType.add("iPhone 6");
listType.add("iPhone 5S");
listType.add("iPhone 5");
listType.add("iPad Pro");
listType.add("iPad");
listType.add("iPad Mini");
listType.add("Other");
setTitle("iPhone Devices");
}
else if(type.equals("android")){
listType.add("Samsung S7");
listType.add("Samsung S6");
listType.add("Samsung S5");
listType.add("Samsung Galaxy Mini");
listType.add("Nexus Pro");
listType.add("HTC One");
listType.add("Google Pixel");
listType.add("Other");
setTitle("Android Devices");
}
else if(type.equals("windows")) {
listType.add("windows 10");
listType.add("windows 8");
listType.add("windows 7");
listType.add("Other");
setTitle("Windows PC");
}
else if(type.equals("mac")) {
listType.add("iMac Pro");
listType.add("iMac");
listType.add("Other");
setTitle("Apple Mac PC");
}
else if(type.equals ("console")) {
listType.add("Xbox One");
listType.add("Xbox 360");
listType.add("PS4");
listType.add("PS4 Pro");
listType.add("Nintendo Switch");
listType.add("Nintendo Wii U");
listType.add("Other");
setTitle("Consoles");
}
else if(type.equals ("laptop")) {
listType.add("windows 10");
listType.add("windows 8");
listType.add("windows 7");
listType.add("Other");
setTitle("Laptops");
}
else if(type.equals ("camera")) {
listType.add("Nikon");
listType.add("FujiFilm");
listType.add("Sony");
listType.add("Olympus");
listType.add("Kodak");
listType.add("Leica");
listType.add("Pentax");
listType.add("Ricah");
listType.add("Other");
setTitle("Camera");
}
else if(type.equals ("printer")) {
listType.add("Cannon");
listType.add("Hewlett Packard");
listType.add("Xerox");
listType.add("Epson");
listType.add("Toshiba");
listType.add("Dell");
listType.add("Other");
setTitle("Printers");
}
listIcon = new ArrayList<Integer>();
}
}
をMAINVIEW:
package com.example.curtisboylan.myapplication;
import android.Manifest;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
import com.google.android.gms.maps.model.LatLng;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static com.google.android.gms.wearable.DataMap.TAG;
public class SearchScreen extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
public final static String EXTRA_MESSAGE = "com.example.curtisboylan.myapplication";
private GoogleApiClient client;
private GridViewAdapter mAdapter;
private ArrayList<String> listType;
private ArrayList<Integer> listIcon;
private String repairtype;
private GridView gridView;
private TextView tv;
private Button contBtn;
private Button googleLocBtn;
private Button devicepicker;
private Button gpsBtn;
private boolean setGPSloc = false;
private Location mLocation;
private LocationManager locationManager;
private long UPDATE_INTERVAL = 2 * 1000; /* 10 secs */
private long FASTEST_INTERVAL = 2000;
private LocationRequest mLocationRequest;
private com.google.android.gms.location.LocationListener listener;
private TextView mLatitudeTextView;
private TextView mLongitudeTextView;
private TextView tv2;
Double longitude;
Double latitude;
public static SearchScreen newInstance(){
SearchScreen fragment = new SearchScreen();
return fragment;
}
public SearchScreen(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
client = new GoogleApiClient.Builder(this.getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
checkLocation();
Intent intent = getActivity().getIntent();
if (intent != null) {
//String device = intent.getStringExtra("device");
//String device = bundle.getString("device");
Toast.makeText(getActivity(), "NODEVICE", Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(getActivity(), "DEVICESELECTED: " + intent.getStringExtra("device"), Toast.LENGTH_SHORT).show();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_main, container, false);
tv = (TextView) view.findViewById(R.id.textView);
prepareList();
repairtype ="";
text = (TextView) view.findViewById(R.id.txt_TextDateTime);
btn_date = (Button) view.findViewById(R.id.btn_datePicker);
btn_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDate();
}
});
contBtn = (Button) view.findViewById(R.id.button);
contBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getActivity(), "Latitude: " + latitude + "Longitude: " + longitude, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), TechnicianListView.class);
intent.putExtra("type", repairtype);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);
}
});
gpsBtn = (Button) view.findViewById(R.id.button3);
gpsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv2.setText("" + getMyPosAddress(mLocation.getLatitude(), mLocation.getLongitude()));
latitude = mLocation.getLatitude();
longitude = mLocation.getLongitude();
}
});
tv2 = (TextView) view.findViewById((R.id.textView));
updateTextLabel();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridViewAdapter(getActivity(), listType, listIcon);
// Set custom adapter to gridview
gridView = (GridView) view.findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(getActivity(), "DEBUG " + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
repairtype = mAdapter.getItem(position);
}
});
googleLocBtn = (Button) view.findViewById(R.id.googleLocBtn);
googleLocBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findPlace(v.findViewById(android.R.id.content));
}
});
devicepicker = (Button) view.findViewById(R.id.devicepick);
devicepicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getActivity(), DevicePicker.class);
// intent.putExtra("message", "test");
startActivity(intent);
// devicepicker.setText(getId("devicetype"));
}
});
return view;
}
//Calling method DateFormat and Calendar
DateFormat formatDateTime = DateFormat.getDateInstance();
Calendar dateTime = Calendar.getInstance();
//Creating Vars
private TextView text;
private Button btn_date;
private void updateDate() {
new DatePickerDialog(getActivity(), d, dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH)).show();
}
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime.set(Calendar.YEAR, year);
dateTime.set(Calendar.MONTH, monthOfYear);
dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateTextLabel();
}
};
private void updateTextLabel() {
text.setText(formatDateTime.format(dateTime.getTime()));
}
public void prepareList() {
listType = new ArrayList<String>();
listType.add("di1");
listType.add("iphonescreen");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listType.add("placeholder");
listIcon = new ArrayList<Integer>();
listIcon.add(R.drawable.camerabrokenicon);
listIcon.add(R.drawable.screenrepairicon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
listIcon.add(R.drawable.placeholdericon);
}
public void testBtn(View v) {
// tv.setText("Welcome");
}
public void findPlace(View v) {
try {
Intent intent =
new PlaceAutocomplete
.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.build(getActivity());
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this.getContext(), data);
Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber());
((TextView) getView().findViewById(R.id.textView))
.setText("" + place.getName());
latitude = place.getLatLng().latitude;
longitude = place.getLatLng().longitude;
Log.e("Tag", "Latitude: " + latitude + "longitude: " + longitude);
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
} else if (resultCode == RESULT_CANCELED) {
}
}
}
@Override
public void onStart() {
super.onStart();
super.onStart();
if (client != null) {
client.connect();
}
}
@Override
public void onStop() {
super.onStop();
client.disconnect();
}
private String getMyPosAddress(double dbLat, double dbLon) {
String addressString = "No address found";
Geocoder gc = new Geocoder(getContext(), Locale.getDefault());
try {
List<android.location.Address> gclist ;
gclist = gc.getFromLocation(dbLat, dbLon, 1);
if (gclist.size() > 0) {
android.location.Address address = gclist.get(0);
addressString = "" + address.getAddressLine(0) + ", " + address.getAdminArea();
}
} catch (IOException e) {
e.printStackTrace();
}
return addressString;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
showAlert();
return;
}
startLocationUpdates();
mLocation = LocationServices.FusedLocationApi.getLastLocation(client);
if(mLocation == null){
startLocationUpdates();
}
if (mLocation != null) {
latitude = mLocation.getLatitude();
longitude = mLocation.getLongitude();
}
else {
}
}
protected void startLocationUpdates() {
// Create the location request
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);
// Request location updates
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.i("LOCCCC", "loc failed");
startLocationUpdates();
}
else{
Log.i("LOCCCC", "loc ok");
}
LocationServices.FusedLocationApi.requestLocationUpdates(client,
mLocationRequest, this);
Log.d("reque", "--->>>>");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
client.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
}
@Override
public void onLocationChanged(Location location) {
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
else {
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
latitude = mLocation.getLatitude();
longitude = mLocation.getLongitude();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
}
private boolean checkLocation() {
if(!isLocationEnabled())
showAlert();
return isLocationEnabled();
}
private void showAlert() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use MyGeek")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
private boolean isLocationEnabled() {
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
}