如何粘贴previous活动数据? [英] How to paste data from previous activity?
问题描述
我想在这里做一个项目,可以扫描棒code(做到这一点),之后,它应该表明基于酒吧code从数据库中的一些值(产品姓名,地址)。
我设法值从酒吧code复制到剪贴板,但我不能管理它复制到 activity_second.xml
的EditText上。
的AndroidManifest.xml
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=br.exemplozxingintegration
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =19/> <使用许可权的android:NAME =android.permission.CAMERA/>
<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.VIBRATE/>
<使用许可权的android:NAME =android.permission.FLASHLIGHT/>
<使用许可权的android:NAME =android.permission.READ_CONTACTS/>
<使用许可权的android:NAME =com.android.browser.permission.READ_HISTORY_BOOKMARKS/>
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用许可权的android:NAME =android.permission.CHANGE_WIFI_STATE/>
<使用许可权的android:NAME =android.permission.ACCESS_WIFI_STATE/> <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
<使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAGE/> <应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=ScannerBucatarie
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。MainActivity
机器人:标签=ScannerBucatarie
机器人:screenOrientation =肖像>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名字=com.google.zxing.client.android.CaptureActivity
机器人:configChanges =方向| keyboardHidden
机器人:标签=ZXing ScanBar
机器人:主题=@安卓风格/ Theme.NoTitleBar.Fullscreen
机器人:windowSoftInputMode =stateAlwaysHidden>
&所述;意图滤光器>
<作用机器人:名字=com.google.zxing.client.android.SCAN/> <类机器人:名字=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名字=。SecondActivity
机器人:标签=@字符串/ title_activity_second>< /活性GT;
< /用途>< /清单>
Config.java
包br.exemplozxingintegration;/ **
*创建者Boghy于2016年2月9日。
* /
公共类配置{
公共静态最后弦乐DATA_URL =http://192.168.94.1/test/getData.php?id=;
公共静态最后弦乐KEY_NAME =名;
公共静态最后弦乐KEY_ADDRESS =地址;
公共静态最后弦乐KEY_VC =VC;
公共静态最后弦乐JSON_ARRAY =结果;
}
SecondActivity.java
包br.exemplozxingintegration;进口android.app.ProgressDialog;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;进口com.android.volley.RequestQueue;
进口com.android.volley.Response;
进口com.android.volley.VolleyError;
进口com.android.volley.toolbox.StringRequest;
进口com.android.volley.toolbox.Volley;进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;公共类SecondActivity扩展AppCompatActivity实现View.OnClickListener { 私人的EditText editTextId;
私人按钮buttonGet;
私人TextView的textViewResult; 私人ProgressDialog负荷; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); editTextId =(EditText上)findViewById(R.id.editTextId);
buttonGet =(按钮)findViewById(R.id.buttonGet);
textViewResult =(的TextView)findViewById(R.id.textViewResult); buttonGet.setOnClickListener(本);
} 私人无效的getData(){
。字符串ID = editTextId.getText()的toString()修剪();
如果(id.equals()){
Toast.makeText(这一点,请输入ID,Toast.LENGTH_LONG).show();
返回;
}
装载= ProgressDialog.show(这一点,请稍候...,读取...,假的,假的); 。字符串URL = Config.DATA_URL + editTextId.getText()的toString()修剪(); StringRequest stringRequest =新StringRequest(网址,新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
loading.dismiss();
showJSON(响应);
}
},
新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
Toast.makeText(SecondActivity.this,error.getMessage()的toString(),Toast.LENGTH_LONG。).show();
}
}); 请求队列请求队列= Volley.newRequestQueue(本);
requestQueue.add(stringRequest);
} 私人无效showJSON(字符串响应){
字符串名称=;
字符串的地址=;
字符串VC =;
尝试{
的JSONObject的JSONObject =新的JSONObject(响应);
JSONArray结果= jsonObject.getJSONArray(Config.JSON_ARRAY);
的JSONObject collegeData = result.getJSONObject(0);
名称= collegeData.getString(Config.KEY_NAME);
地址= collegeData.getString(Config.KEY_ADDRESS);
VC = collegeData.getString(Config.KEY_VC);
}赶上(JSONException E){
e.printStackTrace();
}
textViewResult.setText(姓名:\\ t+名字+\\ n地址:\\ t+地址+\\ nVice校长:\\ t+ VC);
} @覆盖
公共无效的onClick(视图v){
的getData();
}
}`AndroidManifet.xml
< XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=br.exemplozxingintegration
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =19/> <使用许可权的android:NAME =android.permission.CAMERA/>
<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.VIBRATE/>
<使用许可权的android:NAME =android.permission.FLASHLIGHT/>
<使用许可权的android:NAME =android.permission.READ_CONTACTS/>
<使用许可权的android:NAME =com.android.browser.permission.READ_HISTORY_BOOKMARKS/>
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用许可权的android:NAME =android.permission.CHANGE_WIFI_STATE/>
<使用许可权的android:NAME =android.permission.ACCESS_WIFI_STATE/> <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
<使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAGE/>
Config.java 包br.exemplozxingintegration; / **
*创建者Boghy于2016年2月9日。
* /
公共类配置{
公共静态最后弦乐DATA_URL =http://192.168.94.1/test/getData.php?id=;
公共静态最后弦乐KEY_NAME =名;
公共静态最后弦乐KEY_ADDRESS =地址;
公共静态最后弦乐KEY_VC =VC;
公共静态最后弦乐JSON_ARRAY =结果;
}
activity_second.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools的android:layout_width =match_parent
机器人:方向=垂直
机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=SecondActivity。> <的LinearLayout
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <的EditText
机器人:ID =@ + ID / txResult
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1/>
<按钮
机器人:ID =@ + ID / buttonGet
机器人:文字=GET
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/> < / LinearLayout中>
<的TextView
机器人:ID =@ + ID / textViewResult
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>< / LinearLayout中>
activity_main.xml中
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / LinearLayout1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:weightSum =1> <查看
机器人:layout_width =match_parent
机器人:layout_height =1DP
机器人:layout_marginTop =10dp
机器人:layout_marginBottom =10dp
机器人:背景=#DDD/> <按钮
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字=扫描
安卓的onClick =callZXing
机器人:ID =@ + ID /按钮/> <查看
机器人:layout_width =match_parent
机器人:layout_height =1DP
机器人:layout_marginTop =10dp
机器人:layout_marginBottom =10dp
机器人:背景=#DDD/> <的TextView
机器人:ID =@ + ID / txResult
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字=Rezultat:
机器人:TEXTSIZE =20SP/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:文字=这是正确的code?
机器人:ID =@ + ID / TextView的
机器人:layout_marginTop =70dp
机器人:layout_gravity =CENTER_HORIZONTAL/> <按钮
风格=@风格/ CaptureTheme
机器人:layout_width =112dp
机器人:layout_height =68dp
机器人:文字=是
机器人:layout_marginTop =30dp
机器人:ID =@ + ID /按钮2
机器人:layout_weight =0.10
机器人:layout_gravity =CENTER_HORIZONTAL
安卓的onClick =sendSecond/> <按钮
风格=@风格/ CaptureTheme
机器人:layout_width =112dp
机器人:layout_height =68dp
机器人:文字=否
机器人:layout_marginTop =10dp
机器人:ID =@ + ID /按钮1
机器人:layout_weight =0.10
机器人:layout_gravity =CENTER_HORIZONTAL
安卓的onClick =callZXing/>< / LinearLayout中>
SecondActivity.java包br.exemplozxingintegration;进口android.app.ProgressDialog;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;进口com.android.volley.RequestQueue;
进口com.android.volley.Response;
进口com.android.volley.VolleyError;
进口com.android.volley.toolbox.StringRequest;
进口com.android.volley.toolbox.Volley;进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;公共类SecondActivity扩展AppCompatActivity实现View.OnClickListener { 私人的EditText editTextId;
私人按钮buttonGet;
私人TextView的textViewResult; 私人ProgressDialog负荷; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); editTextId =(EditText上)findViewById(R.id.editTextId);
buttonGet =(按钮)findViewById(R.id.buttonGet);
textViewResult =(的TextView)findViewById(R.id.textViewResult); buttonGet.setOnClickListener(本);
} 私人无效的getData(){
。字符串ID = editTextId.getText()的toString()修剪();
如果(id.equals()){
Toast.makeText(这一点,请输入ID,Toast.LENGTH_LONG).show();
返回;
}
装载= ProgressDialog.show(这一点,请稍候...,读取...,假的,假的); 。字符串URL = Config.DATA_URL + editTextId.getText()的toString()修剪(); StringRequest stringRequest =新StringRequest(网址,新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
loading.dismiss();
showJSON(响应);
}
},
新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
Toast.makeText(SecondActivity.this,error.getMessage()的toString(),Toast.LENGTH_LONG。).show();
}
}); 请求队列请求队列= Volley.newRequestQueue(本);
requestQueue.add(stringRequest);
} 私人无效showJSON(字符串响应){
字符串名称=;
字符串的地址=;
字符串VC =;
尝试{
的JSONObject的JSONObject =新的JSONObject(响应);
JSONArray结果= jsonObject.getJSONArray(Config.JSON_ARRAY);
的JSONObject collegeData = result.getJSONObject(0);
名称= collegeData.getString(Config.KEY_NAME);
地址= collegeData.getString(Config.KEY_ADDRESS);
VC = collegeData.getString(Config.KEY_VC);
}赶上(JSONException E){
e.printStackTrace();
}
textViewResult.setText(姓名:\\ t+名字+\\ n地址:\\ t+地址+\\ nVice校长:\\ t+ VC);
} @覆盖
公共无效的onClick(视图v){
的getData();
}
}
由于@Mohammed卡尔玛解释,你必须发送ID作为额外的意向(无需将其复制到剪贴板)。我相信你已经创造了这个意图,但我重新创建它。有想法,我不知道你选择了你的意图的名字,所以我叫它'我'。
在您的MainActivity.java:
意向书I =新意图(MainActivity.class,SecondActivity.java);
i.putExtra(栏code,txtResult.getText()的toString());
startActivity(ⅰ);
在您的SecondActivity.java:
textViewResult =(的TextView)findViewById(R.id.textViewResult);
串吧code = getIntent()getStringExtra(栏code)。
textViewResult.setText(巴code);
I'm trying here to make a project, that can scan a barcode (that is done), and after that it's supposed to show some values from a database based on that barcode (product name, address).
I manage to copy the value from the barcode to the clipboard, but then I cannot manage to copy it to the activity_second.xml
's EditText.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.exemplozxingintegration"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="ScannerBucatarie"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="ScannerBucatarie"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:label="ZXing ScanBar"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"></activity>
</application>
</manifest>
Config.java
package br.exemplozxingintegration;
/**
* Created by Boghy on 09.02.2016.
*/
public class Config {
public static final String DATA_URL = "http://192.168.94.1/test/getData.php?id=";
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_VC = "vc";
public static final String JSON_ARRAY = "result";
}
SecondActivity.java
package br.exemplozxingintegration;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextId = (EditText) findViewById(R.id.editTextId);
buttonGet = (Button) findViewById(R.id.buttonGet);
textViewResult = (TextView) findViewById(R.id.textViewResult);
buttonGet.setOnClickListener(this);
}
private void getData() {
String id = editTextId.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+editTextId.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String address="";
String vc = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
vc = collegeData.getString(Config.KEY_VC);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}
@Override
public void onClick(View v) {
getData();
}
}` AndroidManifet.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.exemplozxingintegration"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Config.java
package br.exemplozxingintegration;
/**
* Created by Boghy on 09.02.2016.
*/
public class Config {
public static final String DATA_URL = "http://192.168.94.1/test/getData.php?id=";
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_VC = "vc";
public static final String JSON_ARRAY = "result";
}
activity_second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".SecondActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/buttonGet"
android:text="Get"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/textViewResult"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ddd" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan"
android:onClick="callZXing"
android:id="@+id/Button" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ddd" />
<TextView
android:id="@+id/txResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rezultat:"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Is this the correct code?"
android:id="@+id/textView"
android:layout_marginTop="70dp"
android:layout_gravity="center_horizontal" />
<Button
style="@style/CaptureTheme"
android:layout_width="112dp"
android:layout_height="68dp"
android:text="Yes"
android:layout_marginTop="30dp"
android:id="@+id/button2"
android:layout_weight="0.10"
android:layout_gravity="center_horizontal"
android:onClick="sendSecond" />
<Button
style="@style/CaptureTheme"
android:layout_width="112dp"
android:layout_height="68dp"
android:text="No"
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:layout_weight="0.10"
android:layout_gravity="center_horizontal"
android:onClick="callZXing" />
</LinearLayout>
SecondActivity.java
package br.exemplozxingintegration;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextId = (EditText) findViewById(R.id.editTextId);
buttonGet = (Button) findViewById(R.id.buttonGet);
textViewResult = (TextView) findViewById(R.id.textViewResult);
buttonGet.setOnClickListener(this);
}
private void getData() {
String id = editTextId.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+editTextId.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String address="";
String vc = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
vc = collegeData.getString(Config.KEY_VC);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}
@Override
public void onClick(View v) {
getData();
}
}
As @Mohammed Kharma explained, you must send the id as an Intent extra (no need to copy it to the clipboard). I believe you already created this Intent, but I'm recreating it. Have in mind that I don't know the name you chose for your intent, so I'll just call it 'i'.
On your MainActivity.java:
Intent i= new Intent(MainActivity.class,SecondActivity.java);
i.putExtra("barcode",txtResult.getText().toString());
startActivity(i);
On your SecondActivity.java:
textViewResult = (TextView) findViewById(R.id.textViewResult);
String barcode=getIntent().getStringExtra("barcode");
textViewResult.setText(barcode);
这篇关于如何粘贴previous活动数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!