点击事件

时间:2016-02-24 06:22:53

标签: android gridview android-adapter

我有一个片段,其中我将适配器设置为:

ChannelListAdapter adapter = new ChannelListAdapter(getActivity(),
            R.layout.channellistrow_livetv, channelList, flag_for_layout);
    adapter.notifyDataSetChanged();
    gv.setAdapter(adapter);

和ChannelListAdapter:

    public class ChannelListAdapter extends ArrayAdapter<Channel> {

    private Context context;
    private ArrayList<Channel> channelList;
    private int rowLayoutResourceId;
    private boolean flag_for_layout;
    public static ViewHolder holder;

    public ChannelListAdapter(Context context, int rowLayoutResourceId,
                              ArrayList<Channel> channelList ,boolean flag_for_layout ) {
        super(context, rowLayoutResourceId, channelList);
        this.flag_for_layout = flag_for_layout;
        this.context = context;
        this.channelList = channelList;
        this.rowLayoutResourceId = rowLayoutResourceId;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {



        if( convertView == null ) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            convertView = inflater.inflate( rowLayoutResourceId, null);

            holder = new ViewHolder();

            holder.channelNameTextView = (TextView) convertView.findViewById( R.id.channel_name );
            holder.channelLogoImageView = (ImageView) convertView.findViewById( R.id.channel_logo );
            holder.tagImageChannel = (ImageView) convertView.findViewById(R.id.tagChannels);


            if(flag_for_layout){

                LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.highlight);
                ll.setPadding(8, 0, 0, 0);


                DisplayMetrics displaymetrics = new DisplayMetrics();
                ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                int height = displaymetrics.heightPixels;
                int width = displaymetrics.widthPixels;

                holder.channelNameTextView.setLayoutParams(
                        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                holder.channelNameTextView.setGravity(Gravity.CENTER);

                holder.channelLogoImageView.setLayoutParams(new FrameLayout.LayoutParams(width/5, height/8));
                ll.setGravity(Gravity.CENTER);

            }
            convertView.setTag( holder );
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        Channel currentChannel = channelList.get( position );

        holder.channelNameTextView.setText( currentChannel.getChannelName() );

        if(holder.channelLogoImageView!=null){

            try {
                UrlImageViewHelper.setUrlDrawable(holder.channelLogoImageView, currentChannel.getChannelLogoLink(), R.drawable.placeholder_logo);
            }catch( Exception e ) {
                e.printStackTrace();
                holder.channelLogoImageView.setImageDrawable( context.getResources().getDrawable( R.drawable.placeholder_logo ) );
            }
        }

        try {
            if (currentChannel.getChannelPurchaseStatus() == Channel.PURCHASE_BOUGHT) {
                //holder.tagImageChannel.setBackground(context.getResources().getDrawable(R.drawable.tag_upgrade));
                holder.tagImageChannel.setBackgroundResource(0);

            } else if (currentChannel.getChannelPurchaseStatus() == Channel.PURCHASE_BUY) {
                holder.tagImageChannel.setBackground(context.getResources().getDrawable(R.drawable.tag_buy));

            } else if (currentChannel.getChannelPurchaseStatus() == Channel.PURCHASE_DEFAULT) {
                holder.tagImageChannel.setBackgroundResource(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    static class ViewHolder {

        TextView channelNameTextView;
        public ImageView channelLogoImageView ,tagImageChannel;
    }


}

主要问题在于在click事件中是否有一个条件,其中用户被定向到asyn任务。在asyn任务的成功中,我必须将tagImageChannel更改为holder.tagImageChannel.setBackgroundResource(0); 一旦成功条件得到满足,我怎么能从Asyn任务中做到这一点。

这是一个单独的课程。这是我的Asyn任务:

    public class BuyCheck extends AsyncTask<String, Void, String> {
    private static final String TAG = "com.newitventure.nettv.reusables.BuyCheck";
    Channel channel;
    CustomDialogManager progress;
    Context context;
    int durationToBuy;
    SharedPreferences pref;
    String code , channelCategory;


    public BuyCheck(Context context, Channel channel, String durationToBuy, String channelCategory) {
        this.channel = channel;
        this.context = context;
        this.durationToBuy = Integer.parseInt(durationToBuy);
        this.channelCategory = channelCategory;

    }

    @Override
    protected void onPreExecute() {
        progress = new CustomDialogManager(context,
                CustomDialogManager.LOADING);
        progress.build();
        progress.setMessage("Please wait...");
        progress.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        DownloadUtil getUtc = new DownloadUtil(LinkConfig.GET_UTC, context);
        String utc = getUtc.downloadStringContent();
        pref = context.getSharedPreferences(Util.APP_SHARED_PREFERENCES, Context.MODE_PRIVATE);
        String email = pref.getString(Util.USER_EMAIL,"");
        if (!utc.equals(DownloadUtil.NotOnline)
                && !utc.equals(DownloadUtil.ServerUnrechable)) {
            DownloadUtil dUtil = new DownloadUtil(params[0] + "&userEmail="
                    + email+ "&"
                    + LinkConfig.getHashCode(context, utc), context);

            Logger.d("BUY URL WITH PARAMETERS",
                    params[0] + "&userEmail="
                            + email+"&"
                            + LinkConfig.getHashCode(context,utc));

            return dUtil.downloadStringContent();
        } else
            return utc;

        /************ the following is another process to hit the url *******************/
        /*
         * List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
         * nameValuePairs.add(new BasicNameValuePair("type", "singleMovie"));
         * nameValuePairs.add(new BasicNameValuePair("duration", "1"));
         * nameValuePairs.add(new BasicNameValuePair("product_id", "1"));
         * Logger.d("name value pairs", nameValuePairs + ""); return
         * dUtil.postDataAndGetStringResponse(nameValuePairs);
         */

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (progress.isShowing())
            progress.dismiss();

        Logger.d("the result in BUYCHECK--> ", result);
        if (DownloadUtil.checkdownloadStringContent(context, result)) {

            try {
                JSONObject jobj = new JSONObject(result);
                String message = "" ;

                try {
                    String userBalance = pref.getString(Util.USER_BALANCE, "");
                    Double balance = Double.parseDouble(userBalance);

                    String successCode = jobj.getString("success_code");
                    code =successCode;
                    message = jobj.getString("success_message");
                    channel.setChannelPurchaseStatus(Channel.PURCHASE_BOUGHT);
                     balance-= channel.getchannelPrice()
                            * durationToBuy;


                    MultipleUsedMethods.updateUserBalance(context,balance+"");

                } catch (JSONException je) {
                    je.printStackTrace();
                    try {
                        message = jobj.getString("error_message");
                    } catch (JSONException je2) {
                        je2.printStackTrace();
                        CustomDialogManager.ReUsedCustomDialogs.dataNotFetched(context);
                    }
                }

                final CustomDialogManager bought_dialog = new CustomDialogManager(
                        context, channel.getChannelName(), message,
                        CustomDialogManager.MESSAGE);
                bought_dialog.build();
                bought_dialog.show();

                bought_dialog.setNegativeButton("OK",
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                bought_dialog.dismiss();
                                if(code.equalsIgnoreCase("200")){

                                    LinkConfig.loadChannelLink(context, channel, channelCategory);

                                }

                              //  ((MainActivity)context).getFragment(1);

                            }
                        });

            } catch (JSONException e) {
                e.printStackTrace();
                final CustomDialogManager bought_dialog = new CustomDialogManager(
                        context, channel.getChannelName(), result,
                        CustomDialogManager.MESSAGE);
                bought_dialog.build();
                bought_dialog.show();

                bought_dialog.setNegativeButton("OK",
                        new View.OnClickListener() {

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

                            }
                        });

            }
        }
    }
}

我想更改tagImageChannel,因为符合200的情况。我怎样才能做到这一点。

0 个答案:

没有答案