单击列表视图项目时在新活动中打开图像?

时间:2014-05-24 04:15:02

标签: android listview onitemclicklistener

这是我的代码,当我点击列表视图项目时,我想转到一个新活动。它应该在AsyncTask下载图片时打开新活动。 到目前为止我已经这样做了。 但它给了FC。 anuy身体可以帮忙吗?

    public class     MainPage    extends ListActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.   layout.    contacts_list);

            final List<Model> list = new ArrayList<Model>();

            /** This block is for getting the image url to download from the server **/
            final GetDataFromDB getvalues = new GetDataFromDB();

            final ProgressDialog dialog = ProgressDialog.show(MainPage.this,
                    "", "Gettting values from DB", true);
            new    Thread   (new Runnable() {
                public void run() {
                    String response = getvalues.getImageURLAndDesciptionFromDB();
                    System.out.println("Response : " + response);

                    dismissDialog(dialog);
                    if (!response.equalsIgnoreCase("")) {
                        if (!response.equalsIgnoreCase("error")) {
                            dismissDialog(dialog);

                            // Got the response, now split it to get the image Urls and description
                            String all[] = response.split("##"); 
                            for(int k = 0; k < all.length; k++){
                                String urls_and_desc[] = all[k].split(","); //  urls_and_desc[0] contains image url and [1] -> description.
                                list.add(get(urls_and_desc[1],urls_and_desc[0]));
                            }
                        }

                    } else {
                        dismissDialog(dialog);
                    }
                }
            }).start();
            /*************************** GOT data from Server ********************************************/

            ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
            setListAdapter(adapter);
ListView lv = getListView();
//setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
         Intent intent = new Intent(MainPage.this, ViewImage.class);
             //Get the value of the item you clicked
            Model model = list.get(position);
                 intent.putExtra("image", model.getURL());
            startActivity(intent);}
});
        }

        public void dismissDialog(final ProgressDialog dialog){
            runOnUiThread(new Runnable() {
                public void run() {
                    dialog.dismiss();
                }
            });
        }
        private Model get(String s, String url) {
            return new Model(s, url);
        }

    }

这是我的布局文件“contact_list”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_margin="0dp" >

  <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="00dp"
            android:dividerHeight="4dp"
            android:cacheColorHint="#0000"
            android:clipToPadding="true"
            android:layout_margin="00dp"
            android:soundEffectsEnabled="true"
            android:scrollbars="none"
            android:divider="#81F79F"

            android:layout_weight="1">
    </ListView>

</LinearLayout>

我的新活动的代码,它会通过从网址重新下载来显示图片而不需要调整大小!

public class ViewImage extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_image);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.view_image, menu);
        String country = getIntent().getStringExtra("image");

        return true;

    }

编辑:我正在使用setContentView()两次!我现在已经解决了这个问题!现在唯一的问题是如何将我的PHP文件中的URL传递给新活动,以便在不调整大小的情况下下载并以完整分辨率显示图像!?

编辑:我已在MainPage.java中为setonitemlistener();

添加了此内容
 ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override 
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                 Intent intent = new Intent(MainPage.this, ViewImage.class);
                     Model model = list.get(position);
                     String myURL = model.getURL();

                     intent.putExtra("image", myURL);
                    startActivity(intent);}
        });

并在新活动ViewImage.java

final String image = getIntent().getStringExtra("image");
        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(image);
        final AQuery aq=new AQuery(this);
        aq.id(R.id.imageView1).image(image, true, true, 200, 0);

现在它可以工作,但只在listview的第一项中下载新活动(ViewImage.java)中的图像。尽管它在listview上下载图像 有人帮帮我吗?

3 个答案:

答案 0 :(得分:0)

我建议您将图片的网址传递给DownloadedImage活动,然后重新获取活动中的图片。最好使用像Nostra这样的ImageLoader来使用缓存而不是从URL加载缓存。这只是我的建议。这是你的选择。干杯!

答案 1 :(得分:0)

试试这个,

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
         Intent intent = new Intent(MainPage.this, ViewImage.class);
             Model model = list.get(position);
             intent.putExtra("image", model.geturl());
            startActivity(intent);}
});

答案 2 :(得分:0)

    Clear How you get Image Url Fisrt If it Come From medel.getUrl() with respective postion of 
    list view accrding to erlier solution of regrading your problem Then on list view click 
    First get Url From model and put it into bundel like this as below menthion

    MainPage.java

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        { 
             Intent intent = new Intent(MainPage.this, ViewImage.class);
             Model model = list.get(position);
             intent.putExtra("imageUrl", model.geturl());
             startActivity(intent);
       }
    });
ViewImage.java
protected void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.xmlview);
     new MyTask().execute();


}

Do background Method of MyTask
@Override
protected Void doInBackground(Void... arg0)
{
    try
    {
     String urlstr = getIntent().getExtras().getString("imageUrl");
     URL url = new URL(urlstr);
     bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
     imageview.setImageBitmap(bmp);

    } 
     catch(Exception e)
     {
       e.printStackTrace();
     }
       return null;
}