我正在使用列表視圖。我使用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;
}
}
您的上下文菜單是否根據所點擊的行更改其項目? –
不是每行的項目都是相同的,但它們的onlcik事件取決於相應的行。 – Khushboo