2012-06-23 25 views
5

Tôi có một listView có thông tin bên trong và khi tôi nhấp vào một hàng, nó phải cho tôi tất cả chi tiết bên dưới hàng đã chọn, trong hàng chọn đó tôi có hình ảnh, tên tưởng tượng, giá vv nên khi tôi bấm hình ảnh trong ListView nó phải cư tất cả các thông tin bao gồm các hình ảnh trên một hoạt động tiếp theoCách lấy giá trị trênListItemClick và chuyển nó sang hoạt động khác

tôi đã cố gắng để hiển thị trên ListView của tôi và nó ok, đây mã của tôi:

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


    } 
    } 

EDIT:

private final Context context; 
    private final String[] myBookNamelist = null; 
    private ArrayList<String> MyP = new ArrayList<String>(); 
    private ArrayList<String> myPurl = new ArrayList<String>(); 
    //ArrayList<HashMap<String, String>> myBookNamelist = new ArrayList<HashMap<String, String>>(); 
    //ArrayList<HashMap<String, String>> myBookImageurl = new ArrayList<HashMap<String, String>>(); 

    public MobileArrayAdapter(Context context,ArrayList<String> Bname,ArrayList<String> BUrl) { 
     super(context, R.layout.list_mobile, Bname); 
     this.context = context; 
     this.MyP = Bname; 
     this.myPurl = BUrl; 
    } 


    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.list_mobile, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.label); 
     ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
     textView.setText(MyP.get(position)); 
     Bitmap bitmap = null; 
     // Change icon based on name 
     JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/p.php"); 
      // http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo 
      try{ 

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

       //for(position=0;position<earthquakes.length();position++){ 
        JSONObject e = earthquakes.getJSONObject(position); 
        String BB = e.getString("P_City"); 
        MyP.add(BB); 

        String UU = e.getString("Pname"); 
        myPurl.add(UU); 

       //} 

      }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 


      String s = MyP.get(position); 
      String i = myPurl.get(position); 

      System.out.println(s); 
      System.out.println(i); 


      try { 
       bitmap = BitmapFactory.decodeStream((InputStream)new URL(i).getContent()); 
      } catch (MalformedURLException err) { 
       // TODO Auto-generated catch block 
       err.printStackTrace(); 
      } catch (IOException err) { 
       // TODO Auto-generated catch block 
       err.printStackTrace(); 
      } 

      //if (!s.equals("")) { 
       imageView.setImageBitmap(bitmap); 
      //} else { 
      System.out.println("Bitmap image: "+position+"="+bitmap); 
       //imageView.setImageResource(R.drawable.android_logo); 
      //} 


     return rowView; 
    } 
} 

Và tôi có thể chuyển vị trí cho lớp tiếp theo, nhưng cách nhận tất cả thông tin từ một hàng đã chọn?

Trả lời

5

Tôi đã cố gắng mã này trong hai ngày nay, sau đó tôi đã nhận nó, vì vậy ở đây nó đi .. OnListItemClick sẽ vượt qua một số các mục:

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


    } 

lớp Rồi thứ hai sẽ nhận được rằng các giá trị trong đó có một image:

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


    } 

tôi giúp đỡ những người sẽ có cùng một vấn đề như tôi trong tương lai :)

+0

'String selectedValue = (String) getListAdapter(). GetItem (vị trí);' cho tôi 'NullPointerException'. Điều đó có bình thường không? – CagCak

2

Tôi tin rằng bạn đang mở rộng ArratAdapter và phải có row.xml thổi phồng dưới getView .Và cho phép giả định row.xml của bạn có các thuộc tính imagename và price.Then bạn sẽ sử dụng một cái gì đó như dưới đây đoạn để trích xuất những giá trị đó.

mListView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView <? > myAdapter, View myView, int myItemInt, long mylng) { 


      String imageName = ((TextView) myView.findViewById(R.id.imageName)).getText().toString(); 
      String price = ((TextView) myView.findViewById(R.id.price)).getText().toString(); 

     } 

    }); 
+1

vì vậy, tôi cần phải tạo ra một ListView mới từ một lớp học mới? bởi vì bạn đặt mListView? ... hãy rõ ràng hơn vì tôi không hiểu đầy đủ về android. – Zamani

+0

hiển thị tập tin MobileArrayAdapter.java của bạn –

+0

owk, tôi sẽ sửa mã của tôi – Zamani

Các vấn đề liên quan