2 clcurie 11 clcurie_11 于 2013.06.14 09:55 提问

如何获得onListItemClick 值再传到另一个 activity中

程序里 listView 中的信息,当我点击一行,它就会给出所选项的所有细节。在所选项中有image, imagename, price等。当我点击listview中的图像时,它必须填充所有的信息包括下一个activity 的图像。
在listView中显示信息

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/p.php");
            try{
                JSONArray  earthquakes = json.getJSONArray("prop");
                for(int i=0;i<earthquakes.length();i++){                        
                    JSONObject e = earthquakes.getJSONObject(i);
                    String BookName = e.getString("P_City");
                    PNames.add(BookName);
                    String BookImg = e.getString("Pname");
                    PImages.add(BookImg);
                }       
            }catch(JSONException e){
                 Log.e("log_tag", "Error parsing data "+e.toString());
            }   
        setListAdapter(new MobileArrayAdapter(this, PNames,PImages));
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
         Intent i = new Intent(getApplicationContext(), Searching.class);
         // sending data to new activity
         i.putExtra("product", selectedValue);
         startActivity(i);
    }
  }

我可以把position传递到下一个类中。但是如何从一个已选项中获取所有的信息呢?

3个回答

cj6585256
cj6585256   2013.06.14 10:40
已采纳

可以封装成一个bean 实现序列化接口 使用 public Intent putExtra(String name, Serializable value) {}

在另外一个Activity获取 直接使用Intent.getSerializableExtra(String name) 获取一个bean

也可以通过上面哥们说的方法传递各种类型的key value

Jave_
Jave_   2013.06.14 10:31
   Bundle bundle = new Bundle();
  //保存信息
   bundle.putString("BookName ", BookName );
   ....
   intent.putExtras(bundle);
csh_34
csh_34   2013.06.18 17:16

OnListItemClick 会传递一个 position:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
Intent moreDetailsIntent = new Intent(ListMobileActivity.this,PropertiesDetails.class);
        Bundle dataBundle = new Bundle();
        dataBundle.putString("SelectedProperty", selectedValue);
        moreDetailsIntent.putExtras(dataBundle);
        startActivity(moreDetailsIntent);
    }

第二个类会接收值包括一个图像:

String Price,Desc,City,rooms,address,size,garage,sold,Pname,image1,image2,image3;
    Bitmap bitmap,bitmap1,bitmap2,bitmap3;
    TextView txtPrice,txtDesc,txtCity,txtrooms,txtaddress,txtsize,txtgarage,txtsold;
    ImageView imgpropertyImage,imgpropertyImage2;
    String result = null;
    InputStream is = null; 
    StringBuilder sb=null;
    Button btnNext,btnPrevs;
    int imgcount = 1;
    JSONPARSER jsonParser = new JSONPARSER();
    private static String url_create_product = "http://10.0.2.2/php/ViewPropertiesByName.php";
    // JSON Node names
   // private static final String TAG_SUCCESS = "success";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thenew);

        txtPrice = (TextView) findViewById(R.id.price);
        txtDesc = (TextView) findViewById(R.id.desc);
        txtCity  = (TextView) findViewById(R.id.City);
        txtrooms = (TextView) findViewById(R.id.Rooms);
        txtaddress = (TextView) findViewById(R.id.Address);
        txtsize  = (TextView) findViewById(R.id.Size);
        txtgarage  = (TextView) findViewById(R.id.garage);
    //  txtsold  = (TextView) findViewById(R.id.sold);      
        imgpropertyImage = (ImageView) findViewById(R.id.imageViewProp);
        imgpropertyImage2 = (ImageView) findViewById(R.id.imageViewProp2);
        btnNext =(Button) findViewById(R.id.btnNext);
        btnPrevs =(Button) findViewById(R.id.btnPrevis);
Bundle b = getIntent().getExtras(); // Getting the Bundle object that pass from another activity
String SelectedProperty = b.getString("SelectedProperty");

////////////////////////Executing A Search Query//////////////////////////////////
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("P_City",SelectedProperty));

//getting JSON Object
// Note that create product url accepts POST method
 JSONObject json = jsonParser.makeHttpRequest(url_create_product,
         "POST", postParameters);
// check log cat fro response
Log.d("Create Response", json.toString());

/////////////Getting Jason Object from the search Query and displaying results
try{

    JSONArray  earthquakes = json.getJSONArray("PropertyDetails");

    for(int i=0;i<earthquakes.length();i++){                        

        JSONObject e = earthquakes.getJSONObject(i);

        Price = e.getString("P_Price");
        Desc = e.getString("P_Desc");
        City = e.getString("P_City"); 
        rooms = e.getString("P_Rooms");
        address = e.getString("P_Address");
        size = e.getString("P_Size");
        garage = e.getString("P_garage");
        //sold = e.getString("P_Sold");
        Pname = e.getString("Pname");
        image1 = e.getString("image1");
        image2 = e.getString("image2");
        image3 = e.getString("image3");

        //BookImages.add(BookImg);
    }
Csdn user default icon
上传中...
上传图片
插入图片