[英]How to pass the value to another activity of an item from listview using contextMenu
[英]Pass the name from listview item to another activity
大家好,我建立了一个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 =所选商品的价格,name =所选商品的名称} ...我想拥有2个textview,第一个具有名称,第二个具有价格。 那怎么可能?
这就是创建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负责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>
我只使用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>
您正在使用意图,这是正确的方法
@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);
}
});
编辑:
您还可以使用以下内容来避免文本视图的初始化
HashMap<String,String> map = (HashMap<String,String>)stoacklist.get(position);
然后
String name = map.get("name");
String price = map.get("price");
从数组中获取名称和价格,并使用该数组对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);
}
});
}
您可以覆盖模型的toString()方法。 例如:
@Override
public String toString() {
return price + "separator" + name;
}
然后,您可以解析它:
String[] item = string.split("separator");
String part1 = parts[0]; // price
String part2 = parts[1]; //name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.