2016-04-08 11 views
9

Sono nuovo ad Android. Sto usando un frammento. Ricevo un errore nel metodo onResult(). Ho bisogno di un metodo sostitutivo per setResult (RESULT_OK, dati) che posso usare nel mio frammento. Per favore aiuto.Esiste un metodo come setResult() in frammento?

CalendarFragment

package app.pal.study.samplestudy; 

import android.app.Fragment; 
import android.content .Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 
import java.util.Date; 
import java.util.List; 

public class CalendarFragment extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false); 
    return rootView; 
} 


@Override 
public void onResume() { 
    super.onResume(); 
    refresh(); 
} 

private void refresh() { 
    CalendarEventDataSource dataSource = new CalendarEventDataSource(getActivity()); 
    dataSource.openReadOnlyDB(); 
    final List<CalendarEvent> calendarEvents = dataSource.getAllEvents(); 
    dataSource.close(); 
    CalAllEventsListAdapter adapter = new CalAllEventsListAdapter(calendarEvents); 
    ListView listView = (ListView) getView().findViewById(R.id.all_event_list); 
    listView.setAdapter(adapter); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == android.R.id.home) { 
     end(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 



public void onBackPressed() { 
    end(); 
} 

private void end() { 
    Intent data = new Intent(); 
    data.putExtra(Constants.DATE_KEY, (Date)(getArguments().get(Constants.DATE_KEY))); 
    setResult(RESULT_OK, data); 
    } 
} 
+0

quale fosse l'errore ..? –

+1

setResult viene utilizzato per le attività richiamate da startActivityForResult. Dovresti usare una callback dell'interfaccia. – Nanoc

+0

Errore @AbhishekPatel: (58, 19) errore: impossibile trovare la variabile di simbolo RESULT_OK – PersianBlue

risposta

15

Si dovrebbe chiamare sul vostro frammento attività possedere:

getActivity().setResult(Activity.RESULT_OK, data) 

anche si potrebbe desiderare di finire la vostra attività:

getActivity().finish(); 
+0

RESULT_OK genera ancora un errore – PersianBlue

+0

'genera un errore' - cosa intendi? Il compilatore può mostrare un errore, questo è durante la compilazione. L'eccezione può essere lanciata durante l'esecuzione - quindi si dovrebbe guardare lo stack delle chiamate in logcat. – marcinj

+0

potrebbe anche utilizzare startActivityForResult quando si avvia il frammento di attività – MojioMS

1

Usa

getActivity().setResult(Activity.RESULT_OK, data); 
1

Utilizzare questo può essere di aiuto per voi ..

getActivity().setResult(Activity.RESULT_OK, data); 
5

Se iniziare la vostra frammento da un altro frammento.

è necessario utilizzare:

/** 
* Optional target for this fragment. This may be used, for example, 
* if this fragment is being started by another, and when done wants to 
* give a result back to the first. The target set here is retained 
* across instances via {@link FragmentManager#putFragment 
* FragmentManager.putFragment()}. 
* 
* @param fragment The fragment that is the target of this one. 
* @param requestCode Optional request code, for convenience if you 
* are going to call back with {@link #onActivityResult(int, int, Intent)}. 
*/ 

public void setTargetFragment(Fragment fragment, int requestCode) { 
} 

Quando si avvia il tuo Fragment.

Ti piace questa:

Fragment newFragment = new YourFragment(); 
newFragment .setTargetFragment(this, SOME_REQUEST_INT); 

E poi, in YourFragment

Intent data = new Intent(); 
data.putExtra(Constants.DATE_KEY, (Date)(getArguments().get(Constants.DATE_KEY))); 
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent); 

O

getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null); 
Problemi correlati