0

我想创建一个动态 GridView,当我单击我的选项时,我刷新我的 asyncTask,然后我从 postExecute 获取所有结果,并通知我的适配器刷新!但我不工作请帮帮我!

GridView grid;
ImageAdapter adapter;

ArrayList<String> arrayThumbs = new ArrayList<String>();
ArrayList<String> arrayBig_image = new ArrayList<String>();
ArrayList<String> arrayAuthor = new ArrayList<String>();
ArrayList<String> arrayDescription = new ArrayList<String>();
ArrayList<String> arrayDate = new ArrayList<String>();

String[] arraymThumbs;
String[] arrayBimages;
String[] arrayauthor;
String[] arraydescription;
String[] arraydate;

final static String TAG_ITEM = "img_list";
final static String TAG_THUMBNAILS = "thumbnail";
final static String TAG_BIG_IMAGE = "big_image";
final static String TAG_AUTHOR = "author";
final static String TAG_DESCRIPTION = "description";
final static String TAG_DATE = "event_date";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

        backTask();


        grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterview, View view,  int position, long id) {
                Intent intent = new Intent(MainActivity.this, ImageGallery.class);
                intent.putExtra("position", position);
                intent.putExtra("big_images", arrayBimages);
                intent.putExtra("author", arrayauthor);
                intent.putExtra("description", arraydescription);
                intent.putExtra("date", arraydate);
                startActivity(intent);
            }
        }); 

}

public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);

}

public boolean onOptionsItemSelected(MenuItem item){        
    switch (item.getItemId()) {
    case R.id.refresh:
        backTask();
        break;
    }
    return super.onOptionsItemSelected(item);
}

public void backTask(){

    BackTask task = new BackTask();
    task.execute();



    try {
        JSONObject result = task.get();
        if(result != null){
        JSONArray jarray = result.getJSONArray(TAG_ITEM);
        for(int i = 0; i < jarray.length(); i++){
            JSONObject jrss = jarray.getJSONObject(i);
            arrayThumbs.add(jrss.getString(TAG_THUMBNAILS));
            arrayBig_image.add(jrss.getString(TAG_BIG_IMAGE));
            arrayAuthor.add(jrss.getString(TAG_AUTHOR));
            arrayDescription.add(jrss.getString(TAG_DESCRIPTION));
            arrayDate.add(jrss.getString(TAG_DATE));

            Log.d("LLLLLOOOOGGGG", jrss.getString("author")+"\n");
        }}
        else{
            AlertDialog.Builder alertNetworkError = new AlertDialog.Builder(this);
            alertNetworkError.setMessage("нет подключения к интернету");
            alertNetworkError.setNegativeButton("Выйти", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();                           
                }                       
            });
            alertNetworkError.create();
            alertNetworkError.show();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    arraymThumbs = new String[arrayThumbs.size()];
    arraymThumbs = arrayThumbs.toArray(arraymThumbs);

    arrayBimages = new String[arrayBig_image.size()];
    arrayBimages = arrayBig_image.toArray(arrayBimages);

    arrayauthor = new String[arrayAuthor.size()];
    arrayauthor = arrayAuthor.toArray(arrayauthor);

    arraydescription = new String[arrayDescription.size()];
    arraydescription = arrayDescription.toArray(arraydescription);

    arraydate = new String[arrayDate.size()];
    arraydate = arrayDate.toArray(arraydate);
    adapter = new ImageAdapter(MainActivity.this, arraymThumbs);
    adapter.notifyDataSetChanged();



}

我的图像适配器在这里我从 asyncTask 获取所有结果并将拇指图像打印到我的 GridView

public class ImageAdapter extends BaseAdapter {

    public Context mContext;
    String[] mThumbs;
    public LayoutInflater inflater;
    public DisplayImageOptions options;
    public ImageLoader imageLoader;


    public ImageAdapter (Context c, String[] arrayhumbss){
        mContext = c;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mThumbs = arrayhumbss;
        options  = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.ic_contact_picture_2)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .cacheInMemory()
                .cacheOnDisc()
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(c));
    }


    @Override
    public int getCount() {
        return mThumbs.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {     
        final ImageView imageView;
        if (convertView == null) {
            imageView = (ImageView) inflater.inflate(R.layout.grid_item, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }

        imageLoader.displayImage(mThumbs[position], imageView, options);

        return imageView;
    }



}

这是我的任务

public class BackTask extends AsyncTask<String, Void, JSONObject>{  

@Override
protected JSONObject doInBackground(String... params) {
    InputStream ips = null; 
    JSONObject jsonObj = null;
    String json = "";       
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(new HttpPost("http://192.168.1.179/lenta/image.js"));                
        ips = response.getEntity().getContent();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader bufff = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = bufff.readLine()) != null){
            sb.append(line + "\n");                 
        }
        ips.close();
        json = sb.toString();       
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        jsonObj = new JSONObject(json);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObj;     

    }

    @Override
    protected void onPostExecute(JSONObject result){
        super.onPostExecute(result);



        }

}

在这里我从 url 处理我的 json,在 MainActivity 我得到这个表单 task.get()

4

2 回答 2

2

grid.setAdapter(adapter)在此行之后添加

 adapter = new ImageAdapter(MainActivity.this, arraymThumbs);

希望这能解决你的问题。

编辑: PS 看来您需要阅读一些内容,因为我认为您并不完全了解适配器是什么以及如何使用它。

于 2012-12-12T06:17:29.820 回答
0

用这个 :

adapter = new ImageAdapter(YourActivity.this, arraymThumbs); 
myGridView.setAdapter(adapter);

希望这对您有所帮助。

谢谢。

于 2012-12-12T06:49:50.393 回答