0

私は Android アプリ開発の初心者ですが、質問に行き詰まっています。私のアプリはSimpleAdapterをアダプターとして使用してリストビューを埋めます.AdapterView.OnItemClickListenerインターフェイスを実装してリストビューに登録し、長いクリックイベントを処理するためにAbsListView.MultiChoiceModeListenerインターフェイスも実装しました. しかし、リストビューでアイテムをシングルクリックすると、onItemClicked()メソッドがトリガーされ、CAB が表示されます。なぜですか?

以下は私のコードです:

package com.XXX.listviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private Button like = null;
    private ListView list = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        String ITEM1 = "Name", ITEM2 = "Age", ITEM3 = "Gender", ITEM4 = "Country";
        String[] names = {"Jim","Tim","Tom","Jack","James","Jason","Anna","Lucy","Lily","Venus"};
        String[] ages = {"21","22","23","23","21","37","34","26","28","29"};
        String[] genders = {"Male","Male","Male","Male","Male","Male","Female","Female","Female","Female"};
        String[] countries = {"UK","US","US","CN","AU","CA","CA","CA","US","UK"};
        for(int i=0;i<10;i++){
            Map<String,String> mapItem = new HashMap<String,String>();
            mapItem.put(ITEM1, names[i]);
            mapItem.put(ITEM2, ages[i]);
            mapItem.put(ITEM3, genders[i]);
            mapItem.put(ITEM4, countries[i]);
            data.add(mapItem);
        }
        list = (ListView)findViewById(R.id.listView);
        list.setClickable(true);
        list.setAdapter(new SimpleAdapter(MainActivity.this,data,R.layout.list,
                new String[]{ITEM1,ITEM2,ITEM3,ITEM4},new int[]{R.id.listName,R.id.listAge,R.id.listGender,R.id.listCountry}));
        list.setOnItemClickListener(new listItemClickListener());
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener(){
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch(item.getItemId()){
                case R.id.menu_delete:
                    System.out.println("trying to remove a list view item");
                    mode.finish();
                    return true;
                default:
                    return false;
                }
            }

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.activity_main, menu);
                return true;
            }

            public void onDestroyActionMode(ActionMode arg0) {}
            public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {return false;}
            public void onItemCheckedStateChanged(ActionMode arg0, int arg1,long arg2, boolean arg3) {}
        });
        list.setOnHoverListener(new View.OnHoverListener() {
            public boolean onHover(View arg0, MotionEvent arg1) {
                arg0.setBackgroundColor(Color.CYAN);
                return false;
            }
        });
    }

    class listItemClickListener implements OnItemClickListener{
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            System.out.println("The adapter view ID is====> "+arg0.getId());
            System.out.println("The clicked item ID is====> "+arg1.getId());;
            System.out.println("The clicked item Pos is====> "+arg2);
            System.out.println("The clicked Row is====> "+arg3);
            ((ListView)arg0).setItemChecked(arg2, true);
            Toast.makeText(MainActivity.this, "You selected "+((TextView)arg1.findViewById(R.id.listName)).getText(), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

onActionItemClicked()また、ユーザーがCABでロングクリックしてdeleteメニューをクリックしたときに、メソッドでリストビューの行を削除する方法を知りたいです。

4

1 に答える 1

0
  @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
if (adapter.getItem(position).getResult() != null) 
{
    Intent intent = new Intent(HistoryActivity.this, CaptureActivity.class);
    intent.putExtra(Intents.History.ITEM_NUMBER, position);
    setResult(Activity.RESULT_OK, intent);
    Toast.makeText(getApplicationContext(), "Long press on item to get Contextmenu", Toast.LENGTH_LONG).show();
  //finish();
}

こんにちは onListItemClick を確認し、アダプターを使用して項目の位置を取得し、長いクリック イベントの場合は以下のコードを実装してください

     listView.setOnItemLongClickListener (new OnItemLongClickListener() {
   public boolean onItemLongClick(AdapterView parent, View view, int position, long id)         
   {

   //do your stuff here
   }
   });
于 2013-03-01T16:14:42.647 に答える