将名称从listview项目传递给另一个活动

时间:2014-03-14 10:17:48

标签: android mysql listview

嘿伙计们我已经构建了一个listview,其中我通过json从mysql解析数据。我想单击列表视图上的项目,并将所选项目的名称和价格提供给另一个活动。到目前为止我做过那些:

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

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

它现在工作正常,但它给了我{price =所选项目价格,名称=所选项目名称}的结果...我希望有2个textview,第1个有名字和第2个价格。怎么可能?

这就是我的ListView的创建方式。

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

我的list_item.xml负责列表视图中的每个项目

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

我只使用textview中的stock_name和stock_price来放置来自数据库的每个股票的名称和价格。

我希望这有助于更多......

我膨胀的布局是这个

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

3 个答案:

答案 0 :(得分:5)

你正在使用正确的意图

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

编辑:

您还可以使用以下内容来避免初始化textviews

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

然后

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

答案 1 :(得分:1)

从您的阵列获取名称和价格,使用它扩充ListView。如下所示:

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

答案 2 :(得分:0)

您可以覆盖模型的toString()方法。例如:

@Override
public String toString() {

    return price + "separator" + name;
}

然后你可以解析它:

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