2012-12-06 57 views
2

我正在使用列表視圖。我使用Efficient適配器來添加項目。我爲每行的onclick事件使用警報對話框。但它給了我兩個選擇。我想要更多的選項,所以我希望在列表視圖的每一行上長按上下文菜單。這裏是我的代碼:如何將上下文菜單添加到列表視圖項的onclick事件

public View getView(final int position, View convertView, ViewGroup parent) { 
     // A ViewHolder keeps references to children views to avoid unneccessary calls 
     // to findViewById() on each row. 
     final ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.list, null);    


      convertView.setOnClickListener(new OnClickListener() { 
        private int pos=position; 

        @Override 
        public void onClick(View v) {       

          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(curr_inst); 


         alertDialogBuilder.setTitle("Data selection");        
         alertDialogBuilder 
          .setMessage("Do you wish to capture new data or access already stored data?") 
          .setCancelable(false) 
          .setPositiveButton("Capture new data",new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog,int id) { 
            Bundle b=new Bundle(); 
            String array = DATA[pos]; 
            b.putString("user",array);          
            Intent intent = new Intent(curr_inst, BluetoothConnect.class); 
            intent.putExtras(b);    

            curr_inst.startActivity(intent); 


           } 
           }) 
          .setNegativeButton("Access stored data",new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog,int id) { 
            Bundle b=new Bundle(); 
            //String[] name={ "adi","MEGAN","DAVE","JOHN","HENDRIX"}; 
            String array = DATA[pos]; 
            b.putString("key",array); 
            Intent i =new Intent(curr_inst,List13.class); 
            i.putExtras(b);  

            curr_inst.startActivity(i) ;        
           } 
          }); 

          // create alert dialog 
          AlertDialog alertDialog = alertDialogBuilder.create(); 

          // show it 
          alertDialog.show();  
          alertDialog.setCancelable(true); 

        } 
       }); 
      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 

      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.text); 
      holder.text1 = (TextView) convertView.findViewById(R.id.text1); 
      holder.text2 = (TextView) convertView.findViewById(R.id.text2); 

我想在這個地方警告對話框中使用右鍵菜單,誰能告訴我該怎麼做。我試着用registerForContextMenu(convertView);但是,它說不能讓從活動類型的靜態引用非靜態方法registerForContextMenu(查看)

更新代碼:

public class List14 extends ListActivity { 
//; 
static List14 curr_inst; 
boolean FirstUSe1 = false; 
boolean FirstUSe2 = true; 
int count = 1; 

public int counter() { 
    return DATA.length; 
} 

public static void update() 
{ 
    EfficientAdapter k=new EfficientAdapter(curr_inst); 
    //convertView.setTag(holder); 
    k.notifyDataSetChanged();  
    curr_inst.setListAdapter(k); 
} 

private static class EfficientAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 

    public EfficientAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 

    } 

    /** 
    * The number of items in the list is determined by the number of speeches 
    * in our array. 
    * 
    * @see android.widget.ListAdapter#getCount() 
    */ 
    public int getCount() { 
     return DATA.length; 
    } 

    /** 
    * Since the data comes from an array, just returning the index is 
    * sufficent to get at the data. If we were using a more complex data 
    * structure, we would return whatever object represents one row in the 
    * list. 
    * 
    * @see android.widget.ListAdapter#getItem(int) 
    */ 
    public Object getItem(int position) { 
     return position; 
    } 

    /** 
    * Use the array index as a unique id. 
    * 
    * @see android.widget.ListAdapter#getItemId(int) 
    */ 
    public long getItemId(int position) { 
     return position; 
    } 

    /** 
    * Make a view to hold each row. 
    * 
    * @see android.widget.ListAdapter#getView(int, android.view.View, 
    *  android.view.ViewGroup) 
    */ 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // A ViewHolder keeps references to children views to avoid unneccessary calls 
     // to findViewById() on each row. 
     final ViewHolder holder; 
     pos = position; 

     // When convertView is not null, we can reuse it directly, there is no need 
     // to reinflate it. We only inflate a new View when the convertView supplied 
     // by ListView is null. 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.list, null);    

      convertView.setOnClickListener(new OnClickListener() { 

       public void onClick(View v1){ 
        pos = position; 
        curr_inst.registerForContextMenu(v1); 
        }      
      }); 
      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 

      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.text); 
      holder.text1 = (TextView) convertView.findViewById(R.id.text1); 
      holder.text2 = (TextView) convertView.findViewById(R.id.text2);    

      convertView.setTag(holder); 
     } else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      convertView.setOnClickListener(new OnClickListener() {      
        public void onClick(View v2){ 
         pos=position; 
         curr_inst.registerForContextMenu(v2); 
         } 
        }); 
      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 

      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.text); 
      holder.text1 = (TextView) convertView.findViewById(R.id.text1); 
      holder.text2 = (TextView) convertView.findViewById(R.id.text2);    
      } 

     // Bind the data efficiently with the holder. 
     holder.text.setText(DATA[position]); 
     holder.text1.setText(DATA_NAME[position]); 
     holder.text2.setText(DATA_AGE[position]); 
     //holder.del.setImageBitmap(mIcon3); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView text;   
     TextView text1; 
     TextView text2;    
    } 
    } 
+0

您的上下文菜單是否根據所點擊的行更改其項目? –

+0

不是每行的項目都是相同的,但它們的onlcik事件取決於相應的行。 – Khushboo

回答

1

我不知道是什麼問題,但我嘗試了以下方法,並沒有向我顯示任何錯誤。

試試這個,

if (convertView == null) { 

      convertView = mInflater.inflate(R.layout.gallery_element, null); 

      holder = new ViewHolder(); 
      convertView.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
         m_Activity.registerForContextMenu(v); 

       } 
      }); 
     } 

這裏m_Activity是活動對象。請注意,在這種情況下,Context對象不起作用。你需要傳遞你的Activity對象來做到這一點。

+0

嘿,這工作正常。但我想將我的列表項的位置傳遞給上下文菜單。你能告訴我該怎麼做。 – Khushboo

+0

我不確定這是否可行。但是,您可以在全球範圍內存儲該位置,並以某種方式使用它。 –

+0

謝謝安德羅,它的工作很好:) – Khushboo

1
@Override 
    public void onListItemClick(ListView l, View view, int position, long id) 
     { 
      item_pos= (Integer) getListAdapter().getItem(position);  
      registerForContextMenu(l); 
      openContextMenu(l);//   
      } 

上述代碼有效。以前我用view來代替listview。現在它完美地工作。

相關問題