为什么单次单击listview项就会触发CAB ?



我是android应用程序开发的新手,现在我被一个问题困住了。我的应用程序使用SimpleAdapter作为适配器来填充列表视图,我已经实现了AdapterView。OnItemClickListener接口并注册到listview,也实现了ablistview。multichoicemodellistener接口,用于处理长点击事件。但是当我在列表视图中单击项目时,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菜单时。

  @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();
}

嗨,请检查您的onlisttitemclick和使用适配器来获得项目位置和长点击事件实现下面的代码

     listView.setOnItemLongClickListener (new OnItemLongClickListener() {
   public boolean onItemLongClick(AdapterView parent, View view, int position, long id)         
   {
   //do your stuff here
   }
   });

相关内容

  • 没有找到相关文章

最新更新