如何获取ClickView的ClickView的值

时间:2014-11-07 01:05:57

标签: android android-intent

我是android新手。我正在开发一个应用程序。我有一个列出页面的列表页面(从mysql数据库加载)。每个列表项包含一个ImageView(酒店的图像),TextViews(用于保存特定酒店的名称,地址,价格等)。我设置了一个SimpleAdapter来设置值。并使用setViewBinder将图像(从Internet)设置为ImageView。

我的问题是,我想让ClickView(这个imageview就像整个列表项的背景)可点击。当用户点击特定的listitem(imageview)时,需要调用一个intent,我可以通过该intent展示所点击的酒店的更多细节。我想通过意图传递一些细节。 我试了很多次。我可以在列表视图中设置酒店的图像和其他详细信息。但是当我单击一个imageview时,意图只携带第一行值。

我将提供以下代码。请帮帮我。

private class LoadHotels extends AsyncTask<Void, Void, Void>
{
    int jarSize;
    protected void onPreExecute() 
    {
        // TODO Auto-generated method stub
        super.onPreExecute();
        //pd.show();
    }
    protected Void doInBackground(Void... args) 
    {
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost
                                  ("http://dev.grapelime.in/projects/staytonight/api/gethotels");
            postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new   BasicNameValuePair
                                 ("location_id",Integer.toString(location_id)));
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            if (entity != null)
            {
                is = entity.getContent();
                Log.e("Entity",entity.toString());
                Log.e("Response","Not Null");
            }
            else
            {
                //Toast.makeText(getApplicationContext(), "Response is null", Toast.LENGTH_LONG);
                Log.e("Response","Null");
            }
            Log.e("Pass 1", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 1", e.toString());

        }
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("Pass 2", "connection success ");
            Log.e("Result", "Result "+result);
        }
        catch(Exception e)
        {
            Log.e("Fail 2", e.toString());
        } 

        JSONArray jar;
        try 
        {
            jar = new JSONArray(result);



            jarSize = jar.length();
            System.out.println("jarsize  "+jarSize);
            hotel_name=new String[jarSize];
            hotel_location=new String[jarSize];
            hotel_type=new String[jarSize];
            hotel_actual_price=new String[jarSize];
            slide_image_paths=new String[jarSize];
            //hotel_discount=new String[jarSize];
            Bitmap bimage = null;
            for(int i=0;i<jarSize;i++)
            {
                JSONObject menuObject = jar.getJSONObject(i);
                hotel_name[i]=menuObject.getString("hotel_name");
                hotel_location[i]=menuObject.getString("locality");
                hotel_type[i]=menuObject.getString("hotel_type");
                hotel_actual_price[i]=menuObject.getString("actual_price");
                slide_image_paths[i]=menuObject.getString("slide_image_paths");

            }
            hotelList = new ArrayList<HashMap<String, Object>>();
            hotelMap = new HashMap<String, Object>();
            System.out.println("length  "+hotel_name.length);
            Bitmap mIcon11 = null;
            for(int j=0;j<hotel_name.length;j++)
            {
                try 
                {
                      InputStream in = new java.net.URL(slide_image_paths[j]).openStream();
                      mIcon11 = BitmapFactory.decodeStream(in);
                      //mIcon11.setHeight(200);
                      //mIcon11.setWidth(240);
                      hotelMap.put("hotel_image",mIcon11);
                } 
                catch (Exception e) 
                {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                }


                hotelMap.put("hotel_name", hotel_name[j]);
                System.out.println("hotel_name  "+hotel_name[j]);
                hotelMap.put("hotel_location", hotel_location[j]);
                System.out.println("hotel_location  "+hotel_location[j]);
                hotelMap.put("hotel_type", hotel_type[j]);
                hotelMap.put("hotel_actual_price","INR"+hotel_actual_price[j]);
                //hotelMap.put("slide_image_paths", slide_image_paths[j]);
                hotelList.add(hotelMap);
                hotelMap = new HashMap<String, Object>();   
            }

        }
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return null;
 }

 protected void onPostExecute(Void result) 
 {

            newsAdapter= new SimpleAdapter(getApplicationContext(), 
                                 hotelList,R.layout.hotel_list_design,
    new String[] {"hotel_image","hotel_name","hotel_location", "hotel_type", 
                                          "hotel_actual_price"}, 
        new int[] {R.id.bgImage, R.id.hotel_name, R.id.hotel_location, 
         R.id.hotel_category,  R.id.hotel_actual_amount});
            newsAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(View view, Object data,String textRepresentation) 
                {
                    if((view instanceof ImageView) & (data instanceof Bitmap)) 
                    {
                        ImageView iv = (ImageView) view;
                        final Bitmap bm = (Bitmap) data;
                        iv.setImageBitmap(bm);

                        iv.setOnClickListener(new OnClickListener() 
                        {

                            @Override
                            public void onClick(View arg0) 
                            {
                                // TODO Auto-generated method stub
                                try
                                {
                                    Intent intent = 
           new Intent(getApplicationContext(), HotelDetailsActivity.class);


                        TextView hotel_name = (TextView)  findViewById(R.id.hotel_name);
                        String hotelName = hotel_name.getText().toString();
                        TextView hotel_location = (TextView) findViewById(R.id.hotel_location);
                        String hotelLocation = hotel_location.getText().toString();
                        TextView hotel_category = (TextView) findViewById(R.id.hotel_category);
                        String hotelCategory = hotel_category.getText().toString();
                TextView hotel_actual_amount = (TextView) findViewById(R.id.hotel_actual_amount);
                String hotelActualAmount = hotel_actual_amount.getText().toString();


             ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                    byte[] byteArray = stream.toByteArray();
                                    intent.putExtra("image", byteArray);
                                    intent.putExtra("name", hotelName);
                                    intent.putExtra("location", hotelLocation);
                                    intent.putExtra("category", hotelCategory);
                                    intent.putExtra("actual_amount", hotelActualAmount);
                                    //intent.putExtra("discount", hotelDiscount);
                                    startActivity(intent);
                                }
                                catch(Exception e)
                                {
                                    Log.e("ERROR ON ONCLICK","Error"+e);
                                }
                            }
                        }); 



                        return true;
                    }
                    return false;
                }
            });

            lv_hotels.setAdapter(newsAdapter);

 }

}

我的XML文件在下面给出

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hotel_list_design"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/bg"
    >
    <ImageView
        android:id="@+id/bgImage"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"


        android:scaleType="centerCrop"
        />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" 
        android:layout_marginBottom="10sp"
        android:background="#8C000000"
        android:paddingLeft="10sp">

        <TextView
            android:id="@+id/hotel_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/hotel_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="0dp"
        android:background="#8C000000"
        android:gravity="center_horizontal"
        android:paddingTop="25dp"
        android:orientation="vertical" >
        <LinearLayout 
            android:layout_width="60dp"
            android:layout_height="100dp"
            android:gravity="center_horizontal"
            android:background="#000000"
            android:orientation="vertical">

        <TextView
            android:id="@+id/hotel_category"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#FF0000"
            android:gravity="center"
            android:textSize="12dp"
            android:text="Cate"/>

        <TextView
            android:id="@+id/hotel_actual_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:textSize="17dp"
            android:text="Price"
            />
        <TextView
            android:id="@+id/hotel_discount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:textSize="17dp"

            />

        </LinearLayout>


    </LinearLayout>

    </RelativeLayout>

0 个答案:

没有答案