简体   繁体   中英

Pass the name from listview item to another activity

Hey guys i have builted a listview in which i parse data from mysql through json. I want to click an item on list view and get the name and the price of the selected item to another activity. I have done those so far:

public void registerCallClickBack() {
        ListView list = (ListView) findViewById(R.id.listView1);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,
                    int position, long id) {
                Object o = parent.getItemAtPosition(position);
                Intent intent = new Intent(MainActivity.this, StockItem.class);
                intent.putExtra("name", o.toString());
                //intent.putExtra("price", R.id.stock_price);
                startActivity(intent);
            }
        });
    }

and in StockItem.java

TextView tv;
    String name, ball;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_item);
        tv = (TextView)findViewById(R.id.stockView1);
        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            name = extras.getString("name");
        }
        tv.setText(name);
    }

It works fine by now but it gives me a result of {price=the selected item price, name=the selected item name}...i would like to have 2 textview with the 1st to have the name and the 2nd the price. how is that possible??

This is how my ListView is being created.

public void ListDrawer() {
        List<Map<String, String>> stocksList = new ArrayList<Map<String, String>>();
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("name");
                String price = jsonChildNode.optString("price");
                stocksList.add(createStockList(name, price));
            }


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        String[] from = { "name", "price"};
        int[] to = { R.id.stock_name, R.id.stock_price };
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
                R.layout.list_item,
                from, to);
        listView.setAdapter(simpleAdapter);
    }
    private HashMap<String, String> createStockList(String name, String price) {
        HashMap<String, String> stockNameNo = new HashMap<String, String>();
        stockNameNo.put("name", name);
        stockNameNo.put("price", price);
        return stockNameNo;
    }

My list_item.xml responsible for each item in the listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#000"
    android:fitsSystemWindows="true"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/stock_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="28dp"
        android:textColor="#fff"
        android:layout_toRightOf="@+id/textView1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/stock_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/textView2"
        android:padding="10dp"
        android:shadowColor="#fff"
        android:shadowDy="4.0"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/stock_name"
        android:text="@string/current_price"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/stock_name"
        android:layout_alignBottom="@+id/stock_name"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="@string/stock_name"
        android:textColor="#fff" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="14dp"
        android:layout_marginBottom="3dp"
        android:src="@drawable/ic_launcher_stock_custom_icon" />

</RelativeLayout>

I only use the stock_name and stock_price from the textview to place the name and the price of each stock that comes from the database.

I hope this helps more...

The layout i inflate is this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StockItem" >

    <TextView
        android:id="@+id/stockView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

</RelativeLayout>

You are using intent which is the right way

   @Override
   public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) {
            TextView tv1 =(TextView)viewClicked.findViewById(R.id.stock_name);
            TextView tv2 =(TextView)viewClicked.findViewById(R.id.stock_price);
            Intent intent = new Intent(MainActivity.this, StockItem.class);
            intent.putExtra("name", tv1.getText().toString());
            intent.putExtra("price",tv2.getText().toString());
            startActivity(intent);
        }
    });

Edit:

You can also use the below to avoid initialization of textviews

HashMap<String,String> map = (HashMap<String,String>)stoacklist.get(position);

Then

String name = map.get("name");
String price = map.get("price");

Get name and price from your array, using which you had inflate ListView. Like below:

public void registerCallClickBack() {
        ListView list = (ListView) findViewById(R.id.listView1);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,
                    int position, long id) {

                String name=YOUR ARRAY.get(position);
                Intent intent = new Intent(MainActivity.this, StockItem.class);
                intent.putExtra("name", name);
                //intent.putExtra("price", R.id.stock_price);
                startActivity(intent);
            }
        });
}

You can override toString() method of your model. Eg:

@Override
public String toString() {

    return price + "separator" + name;
}

Then you can parse it:

String[] item = string.split("separator");
String part1 = parts[0]; // price
String part2 = parts[1]; //name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM