2016-03-20 21 views
0

Ich möchte Daten von einem Fragment an eine Aktivität senden, aber mein derzeitiger Code funktioniert nicht.So senden Sie Daten von einem Fragment an eine Aktivität?

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    Intent intent = new Intent(); 
    Context ctx = getActivity(); 
    DBoperations db = new DBoperations(ctx); 
    Cursor cr = db.getInfo(db); 
    cr.moveToFirst(); 
    long count = id; 
    while(count > 0){ 
     cr.moveToNext(); 
     count --; 
    } 
    String ID = Integer.toString(cr.getInt(0)); 
    String Name = cr.getString(1); 
    intent.putExtra("extra",ID + " " + Name); 
    startActivity(getActivity(),LocationInfo.class); 
} 

Diese Funktion befindet sich in einer Klasse, die Fragment erweitert. Hier wird die gesamte letzte Zeile startActivity (...) ist rot unterstrichen und sagt:

startActivity (android.content.intent, android.os.Bundle) in Fragment kann nicht auf (android angewendet werden. support.v4.app.FragmentActivity, java.lang.Class)

Also, wie ich Daten von Fragment zu einer Aktivität übergeben?

+3

Ich denke du meinst 'startActivity (neu Intent (getActivity() , LocationInfo.class)) ' – fRoStBiT

+0

Ja, nur diese Zeile – PKBEST

+0

Verwenden Sie den Kommentar von fRoStBiT. Sie haben keine neue Absicht geschrieben –

Antwort

1

ich nicht schreiben

startActivity(new Intent(getActivity(),LocationInfo.class)); 

Das ist das einzige Problem war ...

+0

Ja, Sie begannen Aktivität (Aufruf Absicht), aber Sie haben nicht die Absicht zuerst erstellt, bevor Sie es verwenden, und das war das Problem! Freut mich, dass du es selbst gelöst hast – Deepanshu

0

Try this:

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    Intent intent = new Intent(getActivity(), LocationInfo.class); 
    Context ctx = getActivity(); 
    DBoperations db = new DBoperations(ctx); 
    Cursor cr = db.getInfo(db); 
    cr.moveToFirst(); 
    long count = id; 
    while(count > 0){ 
     cr.moveToNext(); 
     count --; 
    } 
    String ID = Integer.toString(cr.getInt(0)); 
    String Name = cr.getString(1); 
    intent.putExtra("extra",ID + " " + Name); 
    startActivity(intent); 
} 
Verwandte Themen