![](/img/trans.png)
[英]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.