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()当用户长按并单击deleteCAB 中的菜单时,如何在方法中删除一行列表视图。

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 回答