从类别到活动使用意图检索ID而不是位置,未显示任何内容。

huangapple go评论79阅读模式
英文:

Retrieving id and not position from class to activity using intent displays nothing

问题

我是移动应用开发的新手。我尝试将一个变量从一个类传递到一个新的活动(Activity),但是没有显示任何内容。然而,这个变量是从产品适配器中检索出来的。

类:

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {
    // ... (略去部分代码)

    class ProductViewHolder extends RecyclerView.ViewHolder {
        // ... (略去部分代码)

        public ProductViewHolder(View itemView) {
            super(itemView);

            // ... (略去部分代码)

            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);
                }
            });
        }
    }
}

活动:

public class Eservice extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eservice);

        String id = getIntent().getExtras().getString("KEY");
        TextView access_text = (TextView) findViewById(R.id.access_text);
        access_text.setText(id);
    }
}

活动的布局:

<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>

显示产品的类:

public class ProductS extends AppCompatActivity {
    // ... (略去部分代码)

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

        recyclerView = (RecyclerView) findViewById(R.id.recylcerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        productList = new ArrayList<>();
        loadProducts();
    }

    private void loadProducts() {
        // ... (略去部分代码)
    }
}
英文:

I am new in developing mobile applications. I am trying to pass a variable from class to a new activity, but it displays nothing. However, this variable is retrieved from the products adapter.

I am new in developing mobile applications. I am trying to pass a variable from class to a new activity, but it displays nothing. However, this variable is retrieved from the products adapter.

The Class

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&lt;ProductsAdapter.ProductViewHolder&gt; {

    private Context mCtx;
    private List&lt;Product&gt; productList;    

    public ProductsAdapter(Context mCtx, List&lt;Product&gt; 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(&quot;KEY&quot;,  productList.get(position).getId());
                    mCtx.startActivity(intent);
                }
            });
        }
    }
}

The activity

package net.simplifiedlearning.volleymysqlexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Eservice extends AppCompatActivity {

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

        String id = getIntent().getExtras().getString(&quot;KEY&quot;);
        TextView access_text = (TextView) findViewById(R.id.access_text);
        access_text.setText(id);
    }
}

Layout for the activity and the other is just a class

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/access&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:background=&quot;@drawable/border&quot;
    android:gravity=&quot;center_horizontal&quot;
    android:orientation=&quot;vertical&quot;
    android:padding=&quot;8dp&quot;&gt;    

    &lt;TextView    
        android:id=&quot;@+id/access_text&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Medium Text&quot;
        android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot;
        android:textColor=&quot;#000&quot; /&gt;

&lt;/LinearLayout&gt;

Th class displaying products

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 = &quot;https://syncsecser.com/testmob/api-fetch.php&quot;;

    //a list to store all the products
    List&lt;Product&gt; 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&lt;&gt;();
        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&lt;String&gt;() {
                    @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 &lt; 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(&quot;id&quot;),
                                    product.getString(&quot;title&quot;)
                                ));
                            }

                            //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);
    }
}

答案1

得分: 0

你的 productList.get(position).getId() 返回的是 Int。尝试将它改为 String

英文:

Your productList.get(position).getId() is returning Int. Try changing it to String

huangapple
  • 本文由 发表于 2020年10月23日 03:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64489574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定