java - 在自定义 ListView 中单击长时创建上下文菜单

标签 java android listview android-listview

我想在使用自定义列表适配器创建的自定义列表中显示带有删除和编辑选项的上下文菜单(OnClickLong)。我将发布代码

SpotListFragment

package com.pap.myspots.fragments;

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

import com.pap.myspots.R;
import com.pap.myspots.R.layout;
import com.pap.myspots.database.DBAdapter;
import com.pap.myspots.listView.SpotList;
import com.pap.myspots.listView.SpotListAdapter;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class SpotListFragment extends Fragment implements OnClickListener{

    String nome;
    String local;
    Button createToast;
    List<String> nomes ;
    List<String> locais ;
    ListView listView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_spotlist, container, false);

        DBAdapter db = new DBAdapter(getActivity());
        db.open();
        Cursor cursor = db.getAllTitles();
        nomes = new ArrayList<String>();
        while(cursor.moveToNext()){
            String uname = cursor.getString(cursor.getColumnIndex("nome"));
            nomes.add(uname);
        }

        Cursor cursor2 = db.getAllTitles();
        locais = new ArrayList<String>();
        while(cursor2.moveToNext()){
            String ulocal = cursor2.getString(cursor.getColumnIndex("local"));
            locais.add(ulocal);
        }

       SpotListAdapter adapter = new SpotListAdapter(getActivity(), generateData());

       // 2. Get ListView from activity_main.xml
       listView = (ListView) rootView.findViewById(R.id.spotList);

       // 3. setListAdapter
       listView.setAdapter(adapter);

        return rootView;

        }

    private ArrayList<SpotList> generateData(){
        ArrayList<SpotList> items = new ArrayList<SpotList>();
        int i = 0;
        while(nomes.size()>i){
            items.add(new SpotList(new String(nomes.get(i)),new String(locais.get(i))));
            i++;
        }
        return items;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    //Deleted individual cart items



}

SpotListAdapter
package com.pap.myspots.listView;

import java.util.ArrayList;

import com.pap.myspots.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SpotListAdapter extends ArrayAdapter<SpotList> {
     private final Context context;
     private final ArrayList<SpotList> itemsArrayList;

     public SpotListAdapter(Context context, ArrayList<SpotList> itemsArrayList) {

         super(context, R.layout.list_row, itemsArrayList);

         this.context = context;
         this.itemsArrayList = itemsArrayList;
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

         // 1. Create inflater 
         LayoutInflater inflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // 2. Get rowView from inflater
         View rowView = inflater.inflate(R.layout.list_row, parent, false);

         // 3. Get the two text view from the rowView
         TextView labelView = (TextView) rowView.findViewById(R.id.title);
         TextView valueView = (TextView) rowView.findViewById(R.id.place);

         // 4. Set the text for textView 
         labelView.setText(itemsArrayList.get(position).getNome());
         valueView.setText(itemsArrayList.get(position).getLocal());

         // 5. retrn rowView
         return rowView;
     }
}

SpotList( bean 类)
package com.pap.myspots.listView;

public class SpotList {

    public SpotList(String nome, String local) {
        super();
        this.nome = nome;
        this.local = local;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getLocal() {
        return local;
    }

    public void setLocal(String local) {
        this.local = local;
    }

    private String nome;
    private String local;


}

最佳答案

所以首先在 onCreate 方法中为你的 listView 注册一个上下文菜单:

registerForContextMenu(yourListView);

通过覆盖 onCreateContextMenu 创建上下文菜单:
@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    _listPosition = info.position;      // Get Index of long-clicked item

    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Choose Action");   // Context-menu title
    menu.add(0, v.getId(), 0, "Edit");  // Add element "Edit"
    menu.add(0, v.getId(), 1, "Delete");        // Add element "Delete"
}

对上下文菜单中的点击使用react:
 @Override  
 public boolean onContextItemSelected(MenuItem item)
 {
      if(item.getTitle() == "Edit") // "Edit" chosen
      {
         // Do stuff
      }
      else if(item.getTitle() == "Delete")  // "Delete" chosen
      {
          // Do stuff
      }
      else 
      {
         return false;
      }

      return true;  
 }  

关于java - 在自定义 ListView 中单击长时创建上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21283636/

相关文章:

java - Apache + Tomcat

java - 转换不带双括号的引用变量

java - Ùرحبا想知道字符的编码

android - 如何在对话框 ListView 中添加editText和图标?

java - 让用户输入整数,直到按下“X” Java

尝试在 IntelliJ Idea 中使用 proguard 登录时,将带有 android-support-v4.jar 的 Android 应用程序添加到 lib 面临错误

android - 如何获取属于进程的所有页面的列表(linux)

android - Audiomanager 和 MediaPlayer 的区别

Android ListView 未正确映射

android - 无法单击 fragment 中 ListView 中的项目