2017-03-16 9 views
0

Ich habe eine Android App gemacht, um mich in eine Datenbank einzuloggen und ich brauche sie, um auf eine Benutzerprofilseite umzuleiten, wenn die Schaltfläche angeklickt wird. Bis jetzt wird nur eine Nachricht angezeigt, um mich anzumelden war erfolgreich und macht dann nichts anderes. Die drei Codeschnipsel, die ich unten habe, sind das XML für die Anwendung, das Java für die gleiche Seite und BackgroundWorker, wo eine Menge der zugrundeliegenden Sachen gemacht wird. Könnte mir jemand zeigen, wie ich auf eine Seite namens Activity_user_profile.xml umleiten kann?Android Studio - Umleiten zu einer anderen Seite

EDITED ANTWORT:

Ich habe die Zeile: startActivity (neu Intent (this, UserProfileActivity.class)); In meiner OnLogin Methode, erlaubt dies mich auf die Seite zu verlinken, ich

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.programmingknowledge.finalyearproject2017.MainActivity"> 

<Button 
    android:text="Register" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/btnRegister" 
    android:onClick="OpenReg" 
    android:layout_below="@+id/btnLogin" 
    android:layout_alignLeft="@+id/btnLogin" 
    android:layout_alignStart="@+id/btnLogin" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:id="@+id/et_doctor_number" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:hint="Doctor number " 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:inputType="number" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:ems="10" 
    android:id="@+id/et_Password" 
    android:layout_below="@+id/et_doctor_number" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:hint="Password" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Login" 
    android:id="@+id/btnLogin" 
    android:onClick="OnLogin" 
    android:layout_below="@+id/et_Password" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

Haupttätigkeit wollte:

package com.example.programmingknowledge.finalyearproject2017; 

import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends ActionBarActivity { 

EditText EmailEt, PasswordEt; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    EmailEt = (EditText) findViewById(R.id.et_doctor_number); 
    PasswordEt = (EditText) findViewById(R.id.et_Password); 
} 

public void OnLogin(View view){ 
    String email = EmailEt.getText().toString(); 
    String password = PasswordEt.getText().toString(); 
    String type = "Login"; 

    BackgroundWorker backgroundWorker = new BackgroundWorker(this); 
    backgroundWorker.execute(type, email, password); 
    startActivity(new Intent(this,UserProfileActivity.class)); 
} 

public void OpenReg(View view){ 
    startActivity(new Intent(this,Register.class)); 
} 

}

Background Worker: 
package com.example.programmingknowledge.finalyearproject2017; 

import android.content.Context; 
import android.os.AsyncTask; 
import android.app.AlertDialog; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 

/** 
* Created by john on 25-Feb-17. 
*/ 

public class BackgroundWorker extends AsyncTask<String, Void, String> { 
    Context context; 
    AlertDialog alertDialog; 
    BackgroundWorker (Context ctx){ 
     context = ctx; 
    } 
    @Override 
    protected String doInBackground(String... params) { 
     String type = params[0]; 
     String login_url = "http://10.0.2.2/login.php"; 
     String register_url = "http://10.0.2.2/register.php"; 
     if (type.equals("login")){ 
      try { 
       String email = params[1]; 
       String password = params[2]; 
       URL url = new URL(login_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8")); 
       String post_data = URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "utf-8")+"&"+URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "utf-8"); 
       bufferedWriter.write(post_data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 
       String result =""; 
       String line =""; 
       while ((line=bufferedReader.readLine())!=null){ 
        result+=line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return result; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e){ 
       e.printStackTrace(); 
      } 
     } else if(type.equals("register")){ 
      try { 
       String patient_name = params[1]; 
       String check_in_date = params[2]; 
       String room_number = params[3]; 
       String bed_number = params[4]; 
       String notes = params[5]; 
       URL url = new URL(register_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8")); 
       String post_data = URLEncoder.encode("patient_name", "UTF-8")+"="+URLEncoder.encode(patient_name, "utf-8")+"&" 
        + URLEncoder.encode("check_in_date", "UTF-8")+"="+URLEncoder.encode(check_in_date, "utf-8")+"&" 
        + URLEncoder.encode("room_number", "UTF-8")+"="+URLEncoder.encode(room_number, "utf-8")+"&" 
        + URLEncoder.encode("bed_number", "UTF-8")+"="+URLEncoder.encode(bed_number, "utf-8")+"&" 
        + URLEncoder.encode("notes", "UTF-8")+"="+URLEncoder.encode(notes, "utf-8"); 
       bufferedWriter.write(post_data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
      outputStream.close(); 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 
      String result =""; 
      String line =""; 
      while ((line=bufferedReader.readLine())!=null){ 
       result+=line; 
      } 
      bufferedReader.close(); 
      inputStream.close(); 
      httpURLConnection.disconnect(); 
      return result; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 

    return null; 
} 

@Override 
protected void onPreExecute() { 
    alertDialog = new AlertDialog.Builder(context).create(); 
    alertDialog.setMessage("Login Status"); 
} 

@Override 
protected void onPostExecute(String result) { 
    alertDialog.setMessage(result); 
    alertDialog.show(); 
} 

@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
} 

}

+0

Was ist in BackgroundWorker? Ich sehe, dass Sie den Aktivitätskontext an ihn übergeben, also können Sie nicht einfach den Kontext 'context.startActivity (new Intent (context, UserProfileActivity.class)));'? – HendraWD

+0

Ich bin sehr mit Android Studio nicht vertraut, deshalb frage ich, ich werde das versuchen und lassen Sie wissen, ob es funktioniert. Wo sollte ich diesen Code hinzufügen? –

+0

Fügen Sie in Ihrer onPostExecute (String result) -Methode Folgendes hinzu: context.startActivity (new Intent (context, UserProfileActivity.class)); –

Antwort

0

Auf Sie OnPostExecute, Call Intent, um Ihre Userprofile Aktivität

012 zu gehen
@Override 
protected void onPostExecute(String result) { 
    alertDialog.setMessage(result); 
    alertDialog.show(); 

activity.startActivity(new Intent(activity,UserProfile.class)); 
} 
+0

Vielen Dank Ich habe es funktioniert mit Ihrer Hilfe und fügte die Antwort auf die ursprüngliche Frage –

+0

Ok, Willkommen .. :) – MathaN

Verwandte Themen