2

我正在使用列表视图。我使用高效适配器添加项目。我正在为每一行的 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);,但它说不能从 Activity 类型对非静态方法 registerForContextMenu(View) 进行静态引用

更新代码:

    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;             
    }
    }
4

2 回答 2

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是一个 Activity 对象。请注意 Context 对象在这种情况下将不起作用。您需要传递您的 Activity 对象来执行此操作。

于 2012-12-06T07:04:18.680 回答
1
@Override
     public void onListItemClick(ListView l, View view, int position, long id)
        {
            item_pos= (Integer) getListAdapter().getItem(position);     
            registerForContextMenu(l);
            openContextMenu(l);//           
          }

上面的代码有效。以前我使用视图代替列表视图。现在它完美地工作了。

于 2012-12-07T10:28:25.467 回答