Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获取ListView的clicked TextView的值_Android_Android Intent - Fatal编程技术网

Android 如何获取ListView的clicked TextView的值

Android 如何获取ListView的clicked TextView的值,android,android-intent,Android,Android Intent,我是android新手。我正在开发一个应用程序。我有一个列表页面,其中列出了从mysql数据库加载的酒店。每个列表项都包含一个酒店的ImageViewimage,TextViews用于保存特定酒店的名称、地址、价格等。我设置了一个SimpleAdapter来设置值。并使用setViewBinder将imagefrom Internet设置为ImageView 我的问题是,我想让imageview这个imageview像整个列表项的背景一样可点击。当用户单击特定的listitemimageview

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

我的问题是,我想让imageview这个imageview像整个列表项的背景一样可点击。当用户单击特定的listitemimageview时,需要调用一个意图,通过它我可以显示所单击酒店的进一步详细信息。我想传递一些细节。 我试了很多次。我可以在listview上设置酒店的图像和其他详细信息。但是,当我单击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>

谷歌为何不可能复制first@Turdaliev努尔苏丹对不起。我在谷歌上搜索了很多次。我有很多。但我不明白。我检查了你建议的线程。现在它正在工作。感谢您的快速支持。非常感谢你