创建上下文菜单时在自定义的ListView点击龙 [英] Create a Context Menu when Click Long in a Custom ListView

查看:78
本文介绍了创建上下文菜单时在自定义的ListView点击龙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示快捷菜单(OnClickLong)与删除和编辑选项,我用一个自定义列表适配器创建一个自定义列表。我会后的code

SpotListFragment

 包com.pap.myspots.fragments;

进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;

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

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

公共类SpotListFragment扩展片段实现OnClickListener {

    字符串诺姆;
    字符串地方;
    按钮createToast;
    名单<字符串> nomes;
    名单<字符串> locais;
    ListView控件的ListView;

    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        查看rootView = inflater.inflate(R.layout.fragment_spotlist,集装箱,假);

        DBAdapter DB =新DBAdapter(getActivity());
        db.open();
        光标光标= db.getAllTitles();
        nomes =新的ArrayList<字符串>();
        而(cursor.moveToNext()){
            字符串的uname = cursor.getString(cursor.getColumnIndex(诺姆));
            nomes.add(UNAME);
        }

        光标光标2 = db.getAllTitles();
        locais =新的ArrayList<字符串>();
        而(cursor2.moveToNext()){
            字符串ulocal = cursor2.getString(cursor.getColumnIndex(本地));
            locais.add(ulocal);
        }

       SpotListAdapter适配器=新SpotListAdapter(getActivity(),generateData());

       从activity_main.xml // 2.获取的ListView
       ListView控件=(ListView控件)rootView.findViewById(R.id.spotList);

       // 3。setListAdapter
       listView.setAdapter(适配器);

        返回rootView;

        }

    私人的ArrayList< SpotList> generateData(){
        ArrayList的< SpotList>项目=新的ArrayList< SpotList>();
        INT I = 0;
        而(nomes.size()I标记){
            items.add(新SpotList(新的字符串(nomes.get(I)),新的String(locais.get(I))));
            我++;
        }
        返回的项目;
    }


    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根

    }

    //删除个别车项目



}
 

SpotListAdapter

 包com.pap.myspots.listView;

进口的java.util.ArrayList;

进口com.pap.myspots.R;

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

公共类SpotListAdapter扩展ArrayAdapter< SpotList> {
     私人最终上下文的背景下;
     私人最终的ArrayList< SpotList> itemsArrayList;

     公共SpotListAdapter(上下文的背景下,ArrayList的< SpotList> itemsArrayList){

         超(背景下,R.layout.list_row,itemsArrayList);

         this.context =背景;
         this.itemsArrayList = itemsArrayList;
     }

     @覆盖
     公共查看getView(INT位置,查看convertView,ViewGroup中父){

         // 1.创建气筒
         LayoutInflater充气=(LayoutInflater)上下文
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         // 2.获取rowView从吹气
         查看rowView = inflater.inflate(R.layout.list_row,父母,假);

         // 3.从rowView两个文本视图
         TextView的LABELVIEW =(TextView中)rowView.findViewById(R.id.title);
         TextView的valueView =(TextView中)rowView.findViewById(R.id.place);

         // 4。设置文字的TextView
         labelView.setText(itemsArrayList.get(位置).getNome());
         valueView.setText(itemsArrayList.get(位置).getLocal());

         // 5. retrn rowView
         返回rowView;
     }
}
 

SpotList(豆)

 包com.pap.myspots.listView;

公共类SpotList {

    公共SpotList(诺姆字符串,字符串本地){
        超();
        this.nome =诺姆;
        this.local =地方;
    }

    公共字符串getNome(){
        返回诺姆;
    }

    公共无效setNome(字符串诺姆){
        this.nome =诺姆;
    }

    公共字符串中的getLocal(){
        回到地方;
    }

    公共无效SETLOCAL(字符串本地){
        this.local =地方;
    }

    私人字符串诺姆;
    私人字符串地方;


}
 

解决方案

所以首先注册您的ListView在onCreate方法的上下文菜单:

  registerForContextMenu(yourListView);
 


由overridig的onCreateContextMenu创建文本菜单:

  @覆盖
公共无效onCreateContextMenu(文本菜单菜单,视图V,ContextMenuInfo menuInfo)
{
    AdapterContextMenuInfo信息=(AdapterContextMenuInfo)menuInfo;
    _listPosition = info.position;长点击的项目//获取指数

    super.onCreateContextMenu(菜单,V,menuInfo);
    menu.setHeaderTitle(选择操作); //上下文菜单标题
    menu.add(0,v.getId(),0,编辑); //加入元素的编辑
    menu.add(0,v.getId(),1,删除); //添加元素删除
}
 


在上下文菜单阵营点击次数:

  @覆盖
 公共布尔onContextItemSelected(菜单项项)
 {
      如果(item.getTitle()==编辑)//编辑选择
      {
         //做的东西
      }
      否则,如果(item.getTitle()==删除)//删除选择
      {
          //做的东西
      }
      其他
      {
         返回false;
      }

      返回true;
 }
 

I want to show a context menu (OnClickLong)with delete and edit option, in a custom list i created with a custom list adapter. i will post the code

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(Beans)

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;


}

解决方案

So first of all register your listView for a context menu in the onCreate method:

registerForContextMenu(yourListView);


Create the contextmenu by overridig the 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 on clicks in context-menu:

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

这篇关于创建上下文菜单时在自定义的ListView点击龙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆