Java 使用intent从类到活动检索id而不是位置不会显示任何内容
我是开发移动应用程序的新手。我试图将一个变量从类传递给一个新的活动,但它什么也不显示。但是,此变量是从产品适配器检索的 我是开发移动应用程序的新手。我试图将一个变量从类传递给一个新的活动,但它什么也不显示。但是,此变量是从产品适配器检索的 班级Java 使用intent从类到活动检索id而不是位置不会显示任何内容,java,android,Java,Android,我是开发移动应用程序的新手。我试图将一个变量从类传递给一个新的活动,但它什么也不显示。但是,此变量是从产品适配器检索的 我是开发移动应用程序的新手。我试图将一个变量从类传递给一个新的活动,但它什么也不显示。但是,此变量是从产品适配器检索的 班级 package net.simplifiedlearning.volleymysqlexample; import android.content.Context; import android.content.Intent; import andro
package net.simplifiedlearning.volleymysqlexample;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
import android.util.Log;
/**
* Created by Belal on 10/18/2017.
*/
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {
private Context mCtx;
private List<Product> productList;
public ProductsAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.product_list, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = productList.get(position);
holder.textViewTitle.setText(product.getTitle());
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle;
public ProductViewHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
intent = new Intent(mCtx , Eservice.class);
int position = getAdapterPosition();
intent = intent.putExtra("KEY", productList.get(position).getId());
mCtx.startActivity(intent);
}
});
}
}
}
活动的布局,另一个只是一个类
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/access"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/access_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000" />
</LinearLayout>
第四类展示产品
package net.simplifiedlearning.volleymysqlexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import android.widget.AdapterView;
import android.widget.GridView;
import android.view.*;
import android.widget.*;
import java.util.*;
import android.content.*;
import com.android.volley.Request;
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;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.android.volley.Request;
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;
import java.util.ArrayList;
import java.util.List;
public class ProductS extends AppCompatActivity {
//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
private static final String URL_PRODUCTS = "https://syncsecser.com/testmob/api-fetch.php";
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_s);
//getting the recyclerview from xml
recyclerView = (RecyclerView) findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// selecting item in recycler view
//initializing the productlist
productList = new ArrayList<>();
loadProducts();
}
private void loadProducts() {
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
//adding the product to product list
productList.add(new Product(
product.getInt("id"),
product.getString("title")
));
}
//creating adapter object and setting it to recyclerview
ProductsAdapter adapter = new ProductsAdapter(ProductS.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
package net.simplifiedlearning.volleymmysqlexample;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入java.util.ArrayList;
导入android.widget.AdapterView;
导入android.widget.GridView;
导入android.view.*;
导入android.widget.*;
导入java.util.*;
导入android.content.*;
导入com.android.volley.Request;
导入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;
导入java.util.ArrayList;
导入java.util.List;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.GridView;
导入com.android.volley.Request;
导入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;
导入java.util.ArrayList;
导入java.util.List;
公共类产品扩展了AppCompative活动{
//这是JSON数据URL
//确保您使用的是正确的ip,否则它将无法工作
私有静态最终字符串URL_PRODUCTS=”https://syncsecser.com/testmob/api-fetch.php";
//存储所有产品的列表
列出产品清单;
//回收视图
回收视图回收视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u product\s);
//从xml获取recyclerview
recyclerView=(recyclerView)findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
//在回收器视图中选择项目
//初始化产品列表
productList=新的ArrayList();
loadProducts();
}
私有void loadProducts(){
/*
*创建字符串请求
*请求类型由第一个参数定义
*URL在第二个参数中定义
*然后我们有一个响应侦听器和一个错误侦听器
*在response listener中,我们将以字符串形式获得JSON响应
* */
StringRequest StringRequest=新的StringRequest(Request.Method.GET、URL\u产品、,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
//将字符串转换为json数组对象
JSONArray数组=新的JSONArray(响应);
//遍历所有对象
对于(int i=0;i
您的productList.get(position.getId()
正在返回Int
。尝试将其更改为String
如何知道从产品适配器检索到变量?你的代码看起来不错。请粘贴您的完整代码,否则。logcat有错误吗?@ruben我更新了代码你的代码看起来不错。你也可以粘贴布局文件吗?我粘贴了布局@ruben请检查显示json产品的最后一个类
package net.simplifiedlearning.volleymysqlexample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import android.widget.AdapterView;
import android.widget.GridView;
import android.view.*;
import android.widget.*;
import java.util.*;
import android.content.*;
import com.android.volley.Request;
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;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.android.volley.Request;
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;
import java.util.ArrayList;
import java.util.List;
public class ProductS extends AppCompatActivity {
//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
private static final String URL_PRODUCTS = "https://syncsecser.com/testmob/api-fetch.php";
//a list to store all the products
List<Product> productList;
//the recyclerview
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_s);
//getting the recyclerview from xml
recyclerView = (RecyclerView) findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// selecting item in recycler view
//initializing the productlist
productList = new ArrayList<>();
loadProducts();
}
private void loadProducts() {
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
//adding the product to product list
productList.add(new Product(
product.getInt("id"),
product.getString("title")
));
}
//creating adapter object and setting it to recyclerview
ProductsAdapter adapter = new ProductsAdapter(ProductS.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}