将值从ListView传递到另一个Activity / Class

时间:2017-01-05 13:04:50

标签: java android listview

所以这里我有类ListProductActivity.java,它根据用户输入的搜索条件显示产品列表,并使用Json从MySQL服务器检索数据。我的问题是,当用户从列表视图中选择另一个活动时,如何传递单个值(在这种情况下为productName),以便我可以在新的Activity(viewProductActivity.java)中使用该值。

ListProductActivity.java

public class ListProductActivity extends AppCompatActivity {

    private ListView list;
    private ProgressDialog loading;
    ArrayList<HashMap<String, String>> productList;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_product);
        list = (ListView) findViewById(R.id.listView);
        productList = new ArrayList<HashMap<String,String>>();
        getData();
    }
    public void getData(){

        String s = getIntent().getStringExtra("productName");

        loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

        String url = Config.DATA_URL2 + s;

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                loading.dismiss();
                showList(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(ListProductActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }

    private void showList(String response){

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray products = jsonObject.getJSONArray(Config.JSON_ARRAY);

            for(int i=0;i<products.length();i++){
                JSONObject productData = products.getJSONObject(i);
                String name = productData.getString(KEY_NAME);
                String price = productData.getString(KEY_PRICE);
                String brand = productData.getString(KEY_BRAND);

                HashMap<String,String> product = new HashMap<String,String>();

                product.put(KEY_NAME,name);
                product.put(KEY_PRICE,price);
                product.put(KEY_BRAND,brand);

                productList.add(product);
            }

            ListAdapter adapter = new SimpleAdapter(
                    ListProductActivity.this, productList, R.layout.list_product,

                    new String[]{KEY_NAME,KEY_PRICE,KEY_BRAND},
                    new int[]{R.id.name, R.id.price, R.id.brand}
            );

            list.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

} 

那么,我可以在ListProductActivity类中实现什么功能,以便将基于listView中选择的用户的productName传递给另一个活动,以及如何在viewProductActivity类中接收传递值?

我真的希望你们能发布这个功能并简要说明它是如何工作的,这样我才能真正理解这些代码。对代码的评论将更加清晰和有用。谢谢。

被修改

以下是搜索后listview的结果图片 Click To View Image

5 个答案:

答案 0 :(得分:3)

像这样实施,

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      String name = productList.get(position).get(KEY_NAME);
      Intent i;
        i = new Intent(mContext, viewProductActivity.class);
        i.putExtra("productName",name);
        startActivity(i);
  } 
});

答案 1 :(得分:3)

首先在当前活动

上实现listview的onItemClickListener
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String product_name = productList.get(position).get(KEY_NAME); 
                Intent intent = new Intent(YourCurrentActivity.this,YourNextActivity.class);
                intent.putExtra("product_name",product_name);
                startActivity(intent);
            }
        });

并在YourNextActivity.java的onCreate方法中接收该值,如下所示:

Bundle bundle = getIntent().getExtras();
String productname = bundle.getString("product_name");

答案 2 :(得分:2)

将数组列表传递给新活动以后,您可以使用该额外的任何内容 例如: -

Intent i = new Intent(ctx, SecondActivity.class);

            i.putExtra("List", List); //List is an arraylist like yours

            startActivity(i);

第二项活动:

SList = (ArrayList<HashMap<String,String>>) getIntent().getSerializableExtra("List");

现在使用任何你想要的SList.get(0).get(&#34; keyname&#34;)

答案 3 :(得分:2)

将您的代码放在这里

private void showList(String response){

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray products = jsonObject.getJSONArray(Config.JSON_ARRAY);

            for(int i=0;i<products.length();i++){
                JSONObject productData = products.getJSONObject(i);
                String name = productData.getString(KEY_NAME);
                String price = productData.getString(KEY_PRICE);
                String brand = productData.getString(KEY_BRAND);

                HashMap<String,String> product = new HashMap<String,String>();

                product.put(KEY_NAME,name);
                product.put(KEY_PRICE,price);
                product.put(KEY_BRAND,brand);

                productList.add(product);
            }

            ListAdapter adapter = new SimpleAdapter(
                    ListProductActivity.this, productList, R.layout.list_product,

                    new String[]{KEY_NAME,KEY_PRICE,KEY_BRAND},
                    new int[]{R.id.name, R.id.price, R.id.brand}
            );

            list.setAdapter(adapter);

           // put your new code here
           list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String product_name = productList.get(position).get(KEY_NAME); 
                Intent intent = new Intent(YourCurrentActivity.this,YourNextActivity.class);
                intent.putExtra("product_name",product_name);
                startActivity(intent);
            }
        });

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

答案 4 :(得分:0)

您可以在listview适配器中通过Intent传递它

        Intent i;
        i = new Intent(mContext, viewProductActivity.class);
        i.putExtra("productName",whateverstring);
        mContext.startActivity(i);