1

OnLongClick 機能の実装に苦労しています。どこにリスナーを追加し、結果のメソッドを定義するかがわかりません。

私が使用した実装はアダプターを使用します-onClickListenerはありませんが、うまく動作します。OnLongClickリスナーを実装する場所/方法を誰でも提案できますか

リスト内のすべてのアイテムでさまざまなアクションを実行する必要はありません-画面上で長押しするためだけに

public class CombChange extends ListActivity {
    @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

setListAdapter(new ListEdit(this, symbols));

@Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

  String selectedValue = (String) getListAdapter().getItem(position);
  if (lastPressed.equals(selectedValue) ){
   count++;}
}

public class ListEdit extends ArrayAdapter<String> {
 private final Context context;
 private final String[] values;

 public ListEdit(Context context, String[] values) {
  super(context, R.layout.activity_comb_change, values);
  this.context = context;
  this.values = values;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  LayoutInflater inflater = (LayoutInflater) context
   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View rowView = inflater.inflate(R.layout.activity_comb_change, parent, false);
  TextView textView = (TextView) rowView.findViewById(R.id.label);
  ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
  textView.setText(values[position]);

  // Change icon based on name
  String s = values[position];

  if (s.equals("a")) {
   imageView.setImageResource(R.drawable.a);

return rowView;
}
}
4

3 に答える 3

2

これを試して:

@Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

v.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub

                String selectedValue = (String) getListAdapter().getItem(position);
                if (lastPressed.equals(selectedValue) ){
                count++;}

                return false;
            }
        });


}
于 2013-04-02T18:50:36.837 に答える
1

onListItemClick()関数に似ListActivityた保護されたメソッドがないのは残念です。onListItemLongClick()

代わりに、アダプターの関数でsetOnLongClickListener()最上位のレイアウト項目 (または任意の) に追加できます。ViewgetView()

myView.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // Do something here.
        return true;
    }
});

警告、OnLongClickListenerあなたがあなたのリスト項目に置くことは、onListItemClick()あなたがすでにリストのために働いている機能への露出を隠すかもしれません. その場合は、代わりに追加setOnClickListener()getView()て使用する必要もあります。

于 2013-04-02T18:29:27.723 に答える