2013-08-23 160 views
0

這是我的ArrayAdapter:如何爲ListView編寫onClick監聽器?

public class ListArrayAdapter extends ArrayAdapter<String> { 


    public ArrayList<String> links = new ArrayList<String>(); 
    public Activity activity = new Activity(); 
    TextView linkText; 
    TextView linkDesc; 

    public Context c; 
    public ListArrayAdapter(Context context, int textViewResourceId, 
      ArrayList<String> fiokiList, Object o) { 

     super(context, textViewResourceId, fiokiList); 
     this.links = fiokiList; 
     activity = (Activity) (o); 
     c = context; 
     //notifyDataSetChanged(); 

    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(
        R.layout.list_item, parent, false); 



      convertView.setOnClickListener(new OnClickListener() { 



        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Toast.makeText(activity, String.valueOf(position), Toast.LENGTH_LONG).show(); 
        } 
       }); 

     } 

     final String link = this.links.get(position); 


     linkText = (TextView) convertView.findViewById(R.id.linkTitle0001); 
    // linkImageView = (SmartImageView) convertView.findViewById(R.id.faviconView0001); 
     linkText.setText(link); 


     //ImageView imgView = (ImageView)convertView.findViewById(R.id.imageView1); 

    // PieMenu pieMenu = new PieMenu(imgView, activity); 
     //fl.addView(pieMenu); 



     return convertView; 



    } 

} 

我知道如何寫一個onClickListener的一個ListView的整個項目?我明白如何爲每個項目編寫一個項目,但是我不知道如何爲視圖完成這項工作?

+0

你的意思是說,比如說,你想爲'linkText'的onClick監聽器? –

+0

或者我認爲他意味着點擊整個'ListView'? –

+0

我的意思是單擊列表視圖的整個項目,而不僅僅是linktext – Hick

回答

3

刪除您在轉接器內做了什麼,添加從中調用adatper

setOnItemClickListener

主要活動在這個代碼是你要調用的函數。

list_view.setOnItemClickListener(new OnItemClickListener() { 

@Override 
    public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) { 
// TODO Auto-generated method stub 

    } 
}); 
0

我明白,如果我沒看錯,你想爲位於每一個的ListView行內的意見設置onClickListener()。如果這是真的,那麼您只需在getView()方法中設置OnCliCkListener()即可。

例如:你在

if (convertView == null) { 

       convertView = inflater.inflate(R.layout.single_row_home, null); 

       tv_title = (TextView) convertView 
         .findViewById(R.id.textViewSingleRowHomeTitle); 
       tv_time = (TextView) convertView 
         .findViewById(R.id.textViewSingleRowHomeTime); 
       iv_checkbox = (ImageView) convertView 
         .findViewById(R.id.imageViewSingleRowHomeCheckbox); 

       convertView.setTag(new TaskViewHolder(tv_title, tv_time, 
         iv_checkbox)); 

      } else { 

       TaskViewHolder viewHolder = (TaskViewHolder) convertView 
         .getTag(); 
       tv_title = viewHolder.get_tv_task_title(); 
       tv_time = viewHolder.get_tv_task_time(); 
       iv_checkbox = viewHolder.get_iv_task_checkbox(); 
      } 

在此之後發現你的意見,你可以設置如下OnClickListener()

tv_title.setClickable(true); 
tv_title.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

       } 
      }); 

如果你想要的ListView行內部意見的單擊事件,您需要要在您的自定義適配器的getView()方法中實現它,否則您的ListView的onItemClickListener僅在OnCreate()內部實現。

+0

,但只有在單擊文本時纔會單擊它。如果文本爲空,則onClick將不起作用。我正在考慮長按onClickView項目的按鈕。 – Hick

0

你是指點擊listview時的事件嗎?如果是的話使用::

name of your listview.setOnItemClickListener(new AdapterView.OnItemClicklistener(){ 
@override 
public void onItemClick(AdapterView<?> adapter,View view,int position,long id){ 
//event up to you 
//my event is 
Toast.makeText(Context,text,duration) 
} 

});