如何将ListView转换为GridView?

时间:2014-05-27 06:48:59

标签: android list gridview android-listview

我创建了一个应用程序,在ListView中显示来自互联网的一些listview图片。我想在GridView中显示它。我尝试使用基本适配器代替列表适配器!但这不起作用,因为它不能采取arg(arg在代码中)

有人可以帮助我在gridview中显示这些图片吗?

这是我的代码。

MainPage.java

public class MainPage extends ListActivity {

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

        setContentView(R.   layout.    contacts_list);

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

        /** This block is for getting the <span id="IL_AD9" class="IL_AD">image url</span> to <span id="IL_AD2" class="IL_AD">download from</span> the server **/
        final GetDataFromDB getvalues = new GetDataFromDB();

        final ProgressDialog dialog = ProgressDialog.show(MainPage.this,
                "", "Gettting values from DB", true);
        final CountDownLatch latch = new CountDownLatch(1);
        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);
                }
                latch.countDown();
            }
        }).start();
        /*************************** GOT data from Server ********************************************/
 try {
    latch.await();
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
        ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
        setListAdapter(adapter);

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


    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);
    }
    @Override
    public void onBackPressed() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);

            // set title
            alertDialogBuilder.setTitle("Exit!");

            // set dialog message
            alertDialogBuilder
                .setMessage("Are you sure you want to leave?")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MainPage.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
    }
        // your code.

}

MyCustomArrayAdapter.java

public class MyCustomArrayAdapter extends ArrayAdapter<Model> {

    private final Activity context;
    private final List<Model> list;

    public MyCustomArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.list_layout_relative, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected ImageView image;
        protected ProgressBar pb;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.list_layout_relative, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.text.setTextColor(Color.BLACK);
            viewHolder.image = (ImageView) view.findViewById(R.id.image);
            viewHolder.image.setVisibility(View.GONE);
            viewHolder.pb = (ProgressBar) view.findViewById(R.id.progressBar1);
            view.setTag(viewHolder);
        } else {
            view = convertView;
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.image.setTag(list.get(position).getURL());
        holder.image.setId(position);
        PbAndImage pb_and_image = new PbAndImage();
        pb_and_image.setImg(holder.image);
        pb_and_image.setPb(holder.pb);
        new DownloadImageTask().execute(pb_and_image);
        return view;
    }
}

Model.java

public class Model {

    private String name;
    private String url;

    public Model(String name, String url) {
        this.name = name;
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getURL() {
        return url;
    }

    public void setURL(String url) {
        this.url = url;
    }
}

我想问别的东西。克林达离开话题..我正在从网址下载图片并在gridview中显示。但它会下载需要大量数据的巨大图像。无论如何,我可以下载缩略图到该图像,以减少数据使用?

1 个答案:

答案 0 :(得分:2)

相同的代码,但不是扩展listActivity扩展普通活动,并使用带有id foo或任何你想要的GridView定义xml。

GridView foo = (GridView) findViewById(R.id.foo);
ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
foo.setAdapter(adapter);