使用位图工厂内部列表视图适配器类加载默认图像

时间:2013-12-11 11:28:50

标签: android listview imageview baseadapter

我正在调用一个Web服务来获取一些细节并在列表视图中显示它们。现在我必须在该列表视图上显示一个图像。我可以从JSON对象中检索图像URL。但是当图片网址包含null时,我想在列表视图中显示一个defult图像。我知道下面的代码是用于它的代码段。但是因为我要在我的适配器类中处理这个(由BaseAdapter类扩展)我不能使用它..请指导我如何处理这个...

这是我的适配器类

public class NewsRowAdapter extends BaseAdapter  {

static Dialog dialogs;
private static final String STIME = "StartTime";
private static final String END = "EndTime";
private static final String DATE = "Date";
private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
public ImageLoader imageLoader; 

    //String response;
    //Context context;
    //Initialize adapter
    public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d) {
        super();
        this.resource=resource;
        this.data = d;
        this.activity = act;
        this.mContext = ctx;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());


    }

    public void showFirstDialog(final ArrayList<HashMap<String, String>> list){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Confirm your Action!");

        // set dialog message
        alertDialogBuilder
            .setMessage("You Have Similar Kind of Appoinments!! Do you wanna Show them ?")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {

                    Toast.makeText(mContext, "Showing", Toast.LENGTH_LONG).show();
                    dialogpop(list);


                }
              })
              .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();
                    }
                });
        alertDialogBuilder.show();

    }

    public void dialogshow(final String Date,final String Start,final String End){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Confirm your Action!");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click yes Confirm!!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    //MainActivity.this.finish();

                    Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();

                    //check similer records






                    //if duplicates > 1 then show the popup list
                    //if(duplicateList.size()>1){

                        /*final Dialog dialogs = new Dialog(activity);
                        dialogs.setContentView(R.layout.dialog_list);
                        dialogs.setTitle("Select One");

                        ListView listView = (ListView) dialogs.findViewById(R.id.dialogList);
                        NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, duplicateList);
                        listView.setAdapter(nw);

                        listView.setOnItemClickListener(new OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> arg0,
                                    View arg1, int arg2, long arg3) {
                                // TODO Auto-generated method stub
                                dialogs.dismiss();
                            }
                        });
                        dialogs.show();*/




                //  }





                }
              })
              .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();
                    }
                });
        alertDialogBuilder.show();

    }





    public void showDuplicateDialog(ArrayList<HashMap<String, String>> list){

        //CharSequence[] cs = list.toArray(new CharSequence[list.size()]);


        AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
        LayoutInflater infl = activity.getLayoutInflater();
        View view = infl.inflate(R.layout.dialog_list, null);

        ListView lv = (ListView) view.findViewById(R.id.dialogList);

        //NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, list);

        SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row,  new String[] { STIME,END, DATE }, new int[] {
                R.id.stime2,R.id.etime2, R.id.blank2});
        lv.setAdapter(sim);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        });



        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.two_line_list_item, android.R.id.text1, Names);*/

        alertDialogBuilder2.setView(view)
        /*alertDialogBuilder2.setAdapter(sim, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
            }
        })
        */


        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

        alertDialogBuilder2.show();
    }



@Override
public View getView(final int position, View convertView, final ViewGroup parent) {


    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row,null);


        final TextView firstname = (TextView) vi.findViewById(R.id.fname);
        final TextView lastname = (TextView) vi.findViewById(R.id.lname);
        final TextView startTime = (TextView) vi.findViewById(R.id.stime);
        final TextView endTime = (TextView) vi.findViewById(R.id.etime);
        final TextView date = (TextView) vi.findViewById(R.id.blank);
        final TextView hidID = (TextView) vi.findViewById(R.id.hidenID);
        final ImageView img = (ImageView) vi.findViewById(R.id.list_image);


        HashMap<String, String> song = new HashMap<String, String>();
        song =data.get(position);

        firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        hidID.setText(song.get(MainActivity.TAG_HID));


        String theUrl = song.get(MainActivity.TAG_IMG);

        if(theUrl.equalsIgnoreCase("null")){
            /*Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.propic);
            profPic.setImageBitmap(bImage);


            ViewList v = new ViewList();
            v.handleImage(theUrl, img);*/
        }
        else{
        imageLoader.DisplayImage(song.get(MainActivity.TAG_IMG), img);
        }
        Button accept = (Button) vi.findViewById(R.id.btnAccepted);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int x = (int) getItemId(position);
                /*Intent zoom=new Intent(mContext, Profile.class);
                zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(zoom);*/

                // get the intent from the hashmap check if there is similar date and time.
                //then store them in a list or array.

                String getDate = (String) date.getText();
                String getStartTime = startTime.getText().toString();
                String getEndTime = endTime.getText().toString();


                ShortList sh = new ShortList();

                ArrayList<HashMap<String, String>> duplicateList; 
                duplicateList=sh.getDuplicated(getDate, getStartTime, getEndTime);

                if(duplicateList.size()>1){
                    //dialogshow(getDate,getStartTime,getEndTime);
                    showFirstDialog(duplicateList);
                }
                else{
                    dialogshow(getDate, getStartTime, getEndTime);

                }




            }
    });


        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String getPname = hidID.getText().toString();

                Toast.makeText(parent.getContext(), "view clicked: "+getPname , Toast.LENGTH_SHORT).show();

                //get the id of the view
                //check the id of the request
                //call the web service acording to the id





                Intent zoom=new Intent(parent.getContext(), Profile.class);  
                zoom.putExtra("PatientID", getPname);
                parent.getContext().startActivity(zoom);



            }
        });

        return vi;


}

public void dialogpop(ArrayList<HashMap<String, String>> list){

    dialogs = new Dialog(activity);
    dialogs.setContentView(R.layout.dialog_list);
    dialogs.setTitle("Select One");



    ListView listView = (ListView) dialogs.findViewById(R.id.dialogList);

    //SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row,  new String[] { STIME,END, DATE }, new int[] {
    //        R.id.stime2,R.id.etime2, R.id.blank2});

    Adapter_For_Dialog nw = new Adapter_For_Dialog(mContext,activity, R.layout.dialog_row, list);
    listView.setAdapter(nw);



    dialogs.show();

}





@Override
public int getCount() {
    // TODO Auto-generated method stub

        return data.size();

}



@Override
public Object getItem(int possision) {
    // TODO Auto-generated method stub
    return possision;
}



@Override
public long getItemId(int possision) {
    // TODO Auto-generated method stub
    return possision;
}

}

请帮帮我:)。

我的问题是我无法在我的适配器类

中使用此代码段
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.propic);
            profPic.setImageBitmap(bImage);

1 个答案:

答案 0 :(得分:0)

替换

if(theUrl.equalsIgnoreCase("null")){

if(theUrl==null || theUrl.equals("")){

并检查它是否有效