如何将u_id值传递给另一个活动?

时间:2018-06-25 16:12:13

标签: java android

这是我的主文件,我在其中获取u_id和其他数据,但我将u_id文本传递给其他活动 Mainactivity.java

    package com.desktop.app;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.desktop.app.Stitle.EXTRA_TITLE;

public class MainActivity extends AppCompatActivity  {
   Button signup,login,learnmore ;
   EditText ed1,ed2,u_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
signup = (Button) findViewById(R.id.signupbutton);
learnmore = (Button) findViewById(R.id.learnmore);
login = (Button) findViewById(R.id.loginbutton);
ed1 = (EditText) findViewById(R.id.emailedittext);
        u_id = (EditText) findViewById(R.id.uid);
ed2 = (EditText) findViewById(R.id.passwordedittext);




signup.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent in = new Intent(MainActivity.this,Activity2.class);
        startActivity(in);
    }
});

learnmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in = new Intent(MainActivity.this,Activity1.class);
                startActivity(in);
            }
        });


login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String email = ed1.getText().toString().trim();
        String password = ed2.getText().toString().trim();
         final String log_id = u_id.getText().toString();
        if(email.isEmpty()){
            ed1.setError("Fill this field");
            ed1.requestFocus();
            return;
        }

        if(password.isEmpty()){
            ed2.setError("Fill this field");
            ed2.requestFocus();
            return;
        }
        if(log_id.isEmpty()){
            u_id.setError("Fill this field");
            u_id.requestFocus();
            return;
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                login();
                Intent i = new Intent (MainActivity.this,fullview.class);
                Bundle b =new Bundle();
                b.putString("text", String.valueOf(u_id));
                i.putExtras(b);
                startActivity(i);

            }
        });
        }
    private void login(){


        String url= "http://192.168.0.136/fyp/andr_log.php";
        final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.trim().equals("Login Successfull")){
                    Toast.makeText(getApplicationContext(), "Login Successfull",Toast.LENGTH_LONG).show();
                    Intent in = new Intent(MainActivity.this,Search.class);
                    startActivity(in);

                }
                else {
                    Toast.makeText(getApplicationContext(), "Login Unsuccessfull",Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "error:" +error.toString() ,Toast.LENGTH_LONG).show();

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();
                params.put("Email", ed1.getText().toString().trim() );
                params.put("uid", u_id.getText().toString().trim() );
                params.put("Password", ed2.getText().toString().trim() );
                return params;
            }
        };
        requestQueue.add(stringRequest);


    }});
}
    }

fullview.java

    package com.desktop.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.support.v7.app.AlertDialog.*;

import static com.desktop.app.Stitle.EXTRA_AUTHOR;
import static com.desktop.app.Stitle.EXTRA_HR;
import static com.desktop.app.Stitle.EXTRA_PUBLISHER;
import static com.desktop.app.Stitle.EXTRA_PUBY;
import static com.desktop.app.Stitle.EXTRA_ACC;
import static com.desktop.app.Stitle.EXTRA_RAK;
import static com.desktop.app.Stitle.EXTRA_STATUS;
import static com.desktop.app.Stitle.EXTRA_TITLE;
import static com.desktop.app.Stitle.EXTRA_VR;

public class fullview extends AppCompatActivity {

    Button location, avail, request;
    TextView textviewtitle, textviewauthors, textviewpublisher, textviewpubyear,textviewacc,u_id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fullview);


        Intent intent = getIntent();

        Bundle b = getIntent().getExtras();
        String id= b.getString("text");
        u_id= findViewById(R.id.uid);

        String title = intent.getStringExtra(EXTRA_TITLE);
        String author = intent.getStringExtra(EXTRA_AUTHOR);
        String publisher = intent.getStringExtra(EXTRA_PUBLISHER);

        int  puby = intent.getIntExtra(EXTRA_PUBY,0);
        int  accc = intent.getIntExtra(EXTRA_ACC,0);
         final int  rak = intent.getIntExtra(EXTRA_RAK,0);
         final int  hr = intent.getIntExtra(EXTRA_HR,0);
         final int  vr = intent.getIntExtra(EXTRA_VR,0);
        final String status = intent.getStringExtra(EXTRA_STATUS);

        textviewtitle = findViewById(R.id.textviewtitle);
        textviewauthors = findViewById(R.id.textviewauthors);
        textviewpublisher = findViewById(R.id.textviewpublisher);
        textviewpubyear = findViewById(R.id.textviewpubyear);
        textviewacc = findViewById(R.id.textviewacc);

        textviewtitle.setText(title);
        textviewauthors.setText(author);
        textviewpublisher.setText(publisher);
        textviewpubyear.setText(String.valueOf(puby));
        textviewacc.setText(String.valueOf(accc));

        location = findViewById(R.id.loc);
        location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                {
                    AlertDialog.Builder builder=new AlertDialog.Builder(fullview.this);

                    builder.setTitle("Location");
                    builder.setMessage("Rak-No :: " + rak +  "\nColumn :: " + hr +"\nRow :: " + vr);
                    AlertDialog alertDialog=null;

                    builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(fullview.this,"Closed",Toast.LENGTH_SHORT).show();
                        }
                    });
                    alertDialog=builder.create();
                    alertDialog.show();
                }

            }
        });

        avail = findViewById(R.id.avail);
        avail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
                {
                    AlertDialog.Builder builder=new AlertDialog.Builder(fullview.this);
                    {
                        if (status.equals("yes")) {
                            builder.setTitle("Availablity");
                            builder.setMessage("Available");
                        } else {
                        builder.setTitle("Availablity");
                        builder.setMessage("Not Available");
                    }
                    }
                    AlertDialog alertDialog=null;

                    builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(fullview.this,"Closed",Toast.LENGTH_SHORT).show();
                        }
                    });
                    alertDialog=builder.create();
                    alertDialog.show();
                }
        });
        request= findViewById(R.id.request);
        request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                request();
            }
        });

    }
    private void request(){


        String url= "http://192.168.0.136/fyp/bookreq.php";
        final RequestQueue requestQueue = Volley.newRequestQueue(fullview.this);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.trim().equals("successfull")){
                    Toast.makeText(getApplicationContext(), "Go to Admin For Approval",Toast.LENGTH_LONG).show();
                    Intent in = new Intent(fullview.this,Search.class);
                    startActivity(in);

                }
                else {
                    Toast.makeText(getApplicationContext(), "Already Requested",Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "error:" +error.toString() ,Toast.LENGTH_LONG).show();

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();

                params.put("Title", textviewtitle.getText().toString().trim());
                params.put("Author", textviewauthors.getText().toString().trim());
                params.put("Publisher", textviewpublisher.getText().toString().trim());
                params.put("Acc", textviewacc.getText().toString().trim());
                params.put("Puby", textviewpubyear.getText().toString().trim());
                Log.i("Info",textviewtitle.getText().toString().trim());

                return params;
            }
        };

        requestQueue.add(stringRequest);
    }
    }

这是我上面的第二个文件,我想穿上u_id文本,但我这样做是为了将u_id文本传递给其他活动

Intent i = new Intent (MainActivity.this,fullview.class);
Bundle b =new Bundle();
b.putString("text", String.valueOf(u_id));
i.putExtras(b);
startActivity(i);

此代码我用于传递u_id文本,而我在下面的代码中使用下一个活动代码

Bundle b = getIntent().getExtras();

        String id= b.getString("text");

        u_id= findViewById(R.id.uid);

1 个答案:

答案 0 :(得分:0)

这就是你的问题。

webpack

更改此行

Intent i = new Intent (MainActivity.this,fullview.class);
Bundle b =new Bundle();
b.putString("text", String.valueOf(u_id));
i.putExtras(b);
startActivity(i);

b.putString("text", String.valueOf(u_id));

编辑:

b.putString("text", u_id.getText().toString().trim());