我正在使用列表视图。我使用Efficient adapter来添加项目。我正在为每行的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
更新的代码:
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 :(得分: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对象才能执行此操作。
答案 1 :(得分: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。现在它完美无缺。