【问题标题】:How to pass the URL of Image when Thumb is clicked in Listview of Android在Android的Listview中点击Thumb时如何传递Image的URL
【发布时间】:2014-06-03 16:51:17
【问题描述】:

嗨,朋友们。

我有:

  • 通过 CustomAdapter 存储图像缩略图的列表视图。

  • 一个不属于列表视图的预览按钮。

我的要求是:

当用户选择 listView 的 Image-Thumb 时,新 Activity 应该以所选 Thumb 的 URL 打开。

我从 JSON 中解析图像和拇指。

谁能帮帮我,我该怎么做?

我的代码是:

public class ECard_main extends Activity 
{
    String catagories;
    String anim_id,album_id,anim_name,anim_thumb,anim_url;
    int global_position; 
    String anim_url_pass;

    ImageView btnpreview;
    TextView title;
    public static boolean isQuit = false;

    HorizontalListView hlv;
    ArrayList<HashMap<String, String>> albumList;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ecard_main);

        btnpreview=(ImageView)findViewById(R.id.iv_preview);
        title.setText(catagories);

        hlv = (HorizontalListView)findViewById(R.id.listData);
        albumList = new ArrayList<HashMap<String, String>>();

        hlv.setOnItemClickListener(new OnItemClickListener()
        {
                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                         int position, long arg3)
                {
                    // TODO Auto-generated method stub
                     Toast.makeText(getApplicationContext(), "POSITION: "+position,Toast.LENGTH_SHORT).show();
                     btnpreview.setTag(position);
                     global_position=position;
                }
            }
        );

        AsycLove().execute();
    }
    public void onClick_preview(View v)
    {
        Intent i=new Intent(ECard_main.this,Preview.class);

        Toast.makeText(getApplicationContext(),"Position in Preview::"+global_position,Toast.LENGTH_SHORT).show();
        i.putExtra("Catagories", catagories);
        i.putExtra("Web_Url", anim_url);

        startActivity(i);
    }
    class AsycLove extends AsyncTask<String, String, String>     
    {
        ProgressDialog progressDialog;      

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();            
            progressDialog = new ProgressDialog(ECard_main.this);
            progressDialog.setTitle("Loading");
            progressDialog.setMessage("Please wait");

        }

        @Override
        protected String doInBackground(String... aurl)        
        {           
            try 
            {
                HttpPost postMethod = new HttpPost("http://demo1.idevtechnolabs.com/smartecard/love.php");              
                BufferedReader bufferedReader = null;

                HttpClient client = new DefaultHttpClient();
                HttpResponse response = null;

                response = client.execute(postMethod);
                final int statusCode = response.getStatusLine().getStatusCode();

                bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer stringBuffer = new StringBuffer("");
                String line = "";
                String LineSeparator = System.getProperty("line.separator");
                while ((line = bufferedReader.readLine()) != null) 
                {
                    stringBuffer.append(line + LineSeparator); 
                }
                bufferedReader.close(); 

                //-------------CONVERT DATA TO JSON---------------------------------

                try 
                {
                    String myjsonstring = stringBuffer.toString();
                    JSONArray jsonArray = new JSONArray(myjsonstring);
                    JSONObject jsonObj = null;
                    albumList.clear();
                    jsonObj = jsonArray.getJSONObject(0);   

                    for(int i=0; i<jsonArray.length();i++)
                    {
                            jsonObj = jsonArray.getJSONObject(i);                           

                            anim_id = jsonObj.getString("animation_id");      
                            album_id = jsonObj.getString("album_id");
                            anim_name = jsonObj.getString("animation_name");      
                            anim_thumb= jsonObj.getString("animation_thumb");

                            anim_url = jsonObj.getString("animation_url");

                            anim_url=anim_url.replaceAll("\"","");


                            HashMap<String, String> tmp_album = new HashMap<String, String>();
                            tmp_album.put("anim_id", anim_id);
                            tmp_album.put("anim_thumb", anim_thumb);
                            tmp_album.put("anim_url",anim_url);

                            albumList.add(tmp_album);
                            Log.v("Animation URL Position","Animation::"+global_position+"::"+anim_url);

                    }

                } 
                catch (Exception e) 
                {
                    Log.v("Home ::","Call JSON Exception in get Album in List--->"+e.toString());
                    e.printStackTrace();
                }
            } 
            catch (IOException e) 
            {
                Log.v("Exception: Get get Album in List","Name-"+e.toString());
                e.printStackTrace();
            }

            return "0";
        }

        @Override
        protected void onPostExecute(String code) 
        {   
            Toast.makeText(getApplicationContext(), "Image URL Call Successfully", Toast.LENGTH_LONG).show();
            Log.v("Album","Album List::"+albumList);

            ECard_main_Custom_Adapter adapter =new ECard_main_Custom_Adapter(getApplicationContext(),albumList,ECard_main.this);
            hlv.setAdapter(adapter);    

            progressDialog.dismiss();
            progressDialog = null;

        }
    }

}

请告诉我我在哪里做错了,我该如何解决?

谢谢

【问题讨论】:

    标签: android json image listview thumbnails


    【解决方案1】:

    我终于找到了解决方案:

    public void onClick_preview(View v)
        { 
            for(int p=0;p<=albumList.size();p++)
            {
    
                String animation_id=albumList.get(p).get("anim_id");
    
                if(Integer.parseInt(animation_id)==global_position)
                {
                    animation_url=albumList.get(p).get("anim_url");
                    break;
                }
            }
            Intent i=new Intent(ECard_main.this,Preview.class);
            i.putExtra("Catagories", catagories);
            i.putExtra("Web_Url", animation_url);
            startActivity(i);
        }
    

    【讨论】:

      【解决方案2】:
          hlv = (HorizontalListView)findViewById(R.id.listData);
          btnpreview=(ImageView)hlv.findViewById(R.id.iv_preview);
          title.setText(catagories);
      

      如果你的按钮在一个 lilst 项目中,你想像这样初始化你的按钮

      【讨论】:

      • 它在列表视图之外而不是在里面
      【解决方案3】:
       hlv.setOnItemClickListener(new OnItemClickListener()
          {
                  @Override
                  public void onItemClick(AdapterView<?> arg0, View view,
                           int position, long arg3)
                  {
                      // TODO Auto-generated method stub
                       Toast.makeText(getApplicationContext(), "POSITION: "YourArrayList.get(position).get("your_image_url_tag");+,Toast.LENGTH_SHORT).show();
                       btnpreview.setTag(position);
                       global_position=position;
                  }
              }
          );
      

      【讨论】:

      • 这是什么?你能用描述说明它有什么用处吗??
      • 您的回答有什么不同?我们可以在问题中看到相同的代码。
      • 我已经找到了位置,但我需要传递拇指拥有的 exzact URL
      • 描述答案@dev
      • 她有一个包含一些数据的数组列表..她想要她点击的位置的一项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 2014-10-20
      • 2012-02-07
      相关资源
      最近更新 更多