リストビューを使用しています。Efficient アダプターを使用してアイテムを追加します。各行の onclick イベントにアラート ダイアログを使用しています。しかし、それは私に2つの選択肢しか与えません. もっとオプションが欲しいので、リストビューの各行を長押しするとコンテキストメニューが欲しいです。これが私のコードです:
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(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;
}
}