2014-10-19 32 views
2

工作,我嘗試在自定義列表視圖中使用OnClickListener,但是當TextView的點擊(Onclicklistener運行)它乳寧代碼在幾個TextView的ListView中! 看到holder.tvComment.setOnClickListener在我ListAdapter.class更新OnClickListener不定製列表視圖

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import com.skyline.jimmy.widget.LikeToast; 
import com.skyline.jimmy.widget.MyToast; 

import android.annotation.SuppressLint; 
import android.app.ActionBar; 
import android.app.Activity; 
import android.content.ClipData; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
//import android.text.ClipboardManager; 
import android.content.ClipboardManager; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.RatingBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class VoteListAdapter extends BaseAdapter { 

    protected static final String CLIPBOARD_SERVICE = null; 
    private final String TAG = "*** VoteListAdapter ***"; 
    private Context context; 
    private ArrayList<Vote> votes; 
    int showHideBtn = 0; 
    public VoteListAdapter(Context context,List<Vote> voteList) { 
     this.context =context; 
     this.votes =(ArrayList<Vote>) voteList; 

    } 

    @Override 
    public int getCount() { 
     return votes.size(); 
    } 

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

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


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

     if (convertView == null) { 
      holder  = new ViewHolder(); 
      convertView = LayoutInflater.from(context).inflate(R.layout.row, null); 
      holder.tvName  = (TextView) convertView.findViewById(R.id.user); 
      holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment); 
      holder.tvDate  = (TextView) convertView.findViewById(R.id.tDate); 
      holder.tvLikes  = (TextView) convertView.findViewById(R.id.tvLikes); 
      holder.ratingBar = (RatingBar)convertView.findViewById(R.id.ratingBar); 
      holder.share = (ImageButton) convertView.findViewById(R.id.sharebtn); 
      holder.copyJoke = (ImageButton) convertView.findViewById(R.id.copyJoke); 
      holder.btnLike = (Button) convertView.findViewById(R.id.likebtn); 
      holder.btnLiked = (Button) convertView.findViewById(R.id.likedbtn); 
      holder.btns = (ImageButton) convertView.findViewById(R.id.btns); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Typeface yaghut=Typeface.createFromAsset(context.getAssets(), "font/Far_Casablanca.ttf"); 
     String likes = Integer.parseInt(votes.get(position).getRate()) + " likes"; 
     holder.tvName.setText(votes.get(position).getName()); 
     holder.tvComment.setText(votes.get(position).getComment()); 
     holder.tvDate.setText(getFormatedDate(votes.get(position).getPublishDate())); 
     holder.tvLikes.setText(likes); 
     holder.ratingBar.setRating(Integer.parseInt(votes.get(position).getRate())); 
     holder.tvComment.setTypeface(yaghut); 
     if (convertView == null) { 

      holder.tvComment.setOnClickListener(new OnClickListener() { 
       int clicks = 0; 
       @Override 
       public void onClick(View v) { 

        clicks = clicks + 1; 
        if(clicks == 1){ 

        }else if(clicks == 2) { 
         LikeToast.makeText(context, "Liked" , Toast.LENGTH_SHORT).show(); 
         holder.btnLike.setText("Liked"); 
         Drawable image = context.getResources().getDrawable(R.drawable.ic_action_liked); 
         int hlike = image.getIntrinsicHeight(); 
         int wlike = image.getIntrinsicWidth(); 
         image.setBounds(0, 0, wlike, hlike); 
         holder.btnLike.setCompoundDrawables(image, null, null, null); 
         //holder.btnLikes.setBackgroundDrawable(R.drawable.ic_action_liked); 
        } else{ 
         clicks = 2; 
        } 
       } 
      }); 
       convertView.setTag(holder); 
      } 
     holder.btns.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 



       if(showHideBtn == 1){ 

        holder.share.setVisibility(View.INVISIBLE); 
        holder.btns.setVisibility(View.VISIBLE); 
        holder.copyJoke.setVisibility(View.INVISIBLE); 

        showHideBtn = 0; 

       } else { 

        holder.share.setVisibility(View.VISIBLE); 
        holder.btns.setVisibility(View.VISIBLE); 
        holder.copyJoke.setVisibility(View.VISIBLE); 
        showHideBtn = 1; 
       } 
      } 
     }); 



     holder.copyJoke.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
       String Joke = votes.get(position).getComment(); 
       clipboard.setText(Joke); 

       Toast toast3 = MyToast.makeText(context, "جک مورد نظر کپی شد", Toast.LENGTH_SHORT); 
       toast3.show(); 

      } 
     }); 

     holder.share.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       /* Intent i = new Intent(Intent.ACTION_SEND); 
       i.setType("text/plain"); 
       i.putExtra(Intent.EXTRA_TEXT,votes.get(position).getComment()); 
       try { 
        context.startActivity(Intent.createChooser(i, "Share")); 
       } catch (android.content.ActivityNotFoundException ex) { 
        ex.printStackTrace(); 
       } 
       */ 

       Toast toast = MyToast.makeText(context, "این قسمت در نسخه بتا در حال طراحی است", Toast.LENGTH_LONG); 
       toast.show(); 
       Toast toast2 = MyToast.makeText(context, "منتظر آپدیت باشید", Toast.LENGTH_SHORT); 
       toast2.show(); 
      } 
     }); 


     return convertView; 
    } 




    private String getFormatedDate(String date) { 
     String myDate = null; 

     try { 
      Date oldDate = new SimpleDateFormat("yy-mm-dd hh:mm:ss").parse(date); 
      myDate = new SimpleDateFormat("dd MMM yyyy").format(oldDate); 
     } catch (ParseException e) { 
      myDate = ""; 
      e.printStackTrace(); 
     } 

     return myDate; 
    } 



    static class ViewHolder { 
     ImageButton copyJoke; 
     ImageButton share; 
     ImageButton btns; 
     Button btnLike; 
     Button btnLiked; 
     TextView tvName; 
     TextView tvLikes; 
     TextView tvComment; 
     TextView tvDate; 
     RatingBar ratingBar; 
    } 
} 
+0

刪除setFocusable和setFocusableInTouchMode爲btns ..並嘗試.. – 2014-10-19 09:22:43

+0

@ArpitPatel我嘗試,但它並不重要 – 2014-10-19 09:24:58

回答

0

寫setOnClickListener代碼放到if條件

if (convertView == null) { 
    ..... 
    ..... 
    // write here your holder.tvComment.setOnClickListener 

    convertView.setTag(holder); 
} 

這行之前:

convertView.setTag(holder); 

您的全適配器將像這樣:

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import com.skyline.jimmy.widget.LikeToast; 
import com.skyline.jimmy.widget.MyToast; 

import android.annotation.SuppressLint; 
import android.app.ActionBar; 
import android.app.Activity; 
import android.content.ClipData; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
//import android.text.ClipboardManager; 
import android.content.ClipboardManager; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.RatingBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class VoteListAdapter extends BaseAdapter { 

    ViewHolder holder; 
    protected static final String CLIPBOARD_SERVICE = null; 
    private final String TAG = "*** VoteListAdapter ***"; 
    private Context context; 
    private ArrayList<Vote> votes; 
    int showHideBtn = 0; 
    public VoteListAdapter(Context context,List<Vote> voteList) { 
     this.context =context; 
     this.votes =(ArrayList<Vote>) voteList; 

    } 

    @Override 
    public int getCount() { 
     return votes.size(); 
    } 

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

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

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

     if (convertView == null) { 
      holder  = new ViewHolder(); 
      convertView = LayoutInflater.from(context).inflate(R.layout.row, null); 
      holder.tvName  = (TextView) convertView.findViewById(R.id.user); 
      holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment); 
      holder.tvDate  = (TextView) convertView.findViewById(R.id.tDate); 
      holder.tvLikes  = (TextView) convertView.findViewById(R.id.tvLikes); 
      holder.ratingBar = (RatingBar)convertView.findViewById(R.id.ratingBar); 
      holder.share = (ImageButton) convertView.findViewById(R.id.sharebtn); 
      holder.copyJoke = (ImageButton) convertView.findViewById(R.id.copyJoke); 
      holder.btnLike = (Button) convertView.findViewById(R.id.likebtn); 
      holder.btnLiked = (Button) convertView.findViewById(R.id.likedbtn); 
      holder.btns = (ImageButton) convertView.findViewById(R.id.btns); 


      holder.btns.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 



        if(showHideBtn == 1){ 

         holder.share.setVisibility(View.INVISIBLE); 
         holder.btns.setVisibility(View.VISIBLE); 
         holder.copyJoke.setVisibility(View.INVISIBLE); 

         showHideBtn = 0; 

        } else { 

         holder.share.setVisibility(View.VISIBLE); 
         holder.btns.setVisibility(View.VISIBLE); 
         holder.copyJoke.setVisibility(View.VISIBLE); 
         showHideBtn = 1; 
        } 
       } 
      }); 



     holder.copyJoke.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 
       String Joke = votes.get(position).getComment(); 
       clipboard.setText(Joke); 

       Toast toast3 = MyToast.makeText(context, "جک مورد نظر کپی شد", Toast.LENGTH_SHORT); 
       toast3.show(); 

      } 
     }); 

     holder.share.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       /* Intent i = new Intent(Intent.ACTION_SEND); 
       i.setType("text/plain"); 
       i.putExtra(Intent.EXTRA_TEXT,votes.get(position).getComment()); 
       try { 
        context.startActivity(Intent.createChooser(i, "Share")); 
       } catch (android.content.ActivityNotFoundException ex) { 
        ex.printStackTrace(); 
       } 
       */ 

       Toast toast = MyToast.makeText(context, "این قسمت در نسخه بتا در حال طراحی است", Toast.LENGTH_LONG); 
       toast.show(); 
       Toast toast2 = MyToast.makeText(context, "منتظر آپدیت باشید", Toast.LENGTH_SHORT); 
       toast2.show(); 
      } 
     }); 


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

     Typeface yaghut=Typeface.createFromAsset(context.getAssets(), "font/Far_Casablanca.ttf"); 
     String likes = Integer.parseInt(votes.get(position).getRate()) + " likes"; 
     holder.tvName.setText(votes.get(position).getName()); 
     holder.tvComment.setText(votes.get(position).getComment()); 
     holder.tvDate.setText(getFormatedDate(votes.get(position).getPublishDate())); 
     holder.tvLikes.setText(likes); 
     holder.ratingBar.setRating(Integer.parseInt(votes.get(position).getRate())); 
     holder.tvComment.setTypeface(yaghut); 


     return convertView; 
    } 

    private String getFormatedDate(String date) { 
     String myDate = null; 

     try { 
      Date oldDate = new SimpleDateFormat("yy-mm-dd hh:mm:ss").parse(date); 
      myDate = new SimpleDateFormat("dd MMM yyyy").format(oldDate); 
     } catch (ParseException e) { 
      myDate = ""; 
      e.printStackTrace(); 
     } 

     return myDate; 
    } 



    static class ViewHolder { 
     ImageButton copyJoke; 
     ImageButton share; 
     ImageButton btns; 
     Button btnLike; 
     Button btnLiked; 
     TextView tvName; 
     TextView tvLikes; 
     TextView tvComment; 
     TextView tvDate; 
     RatingBar ratingBar; 
    } 
} 
+0

現在OnCliclListener不工作,偏食秀'死Code'! :( – 2014-10-19 09:40:48

+0

刪除此行:holder.tvComment.setFocusableInTouchMode(假);對於我這個代碼工作 – 2014-10-19 09:43:16

+0

我嘗試,但它說死碼!!我更新我的帖子PLZ再次看到我的代碼:-( – 2014-10-19 09:54:05