如何添加longclick posiljkica

时间:2019-02-13 08:34:29

标签: android

您好,我试图添加一个长按posiljkica,在运行这段代码时遇到未知的问题。

欢迎任何帮助,谢谢。

该项目是使用Java和xml在Android Studio中完成的。

public class PretragaKorisnici extends DialogFragment  {

    private static final String Korisnik_Key = "Korisnik_Key";
    private MyRunnable<KorisnikVM> callback;
    private List<KorisnikVM> podaci;
    private SearchView pretraga;
    private ListView listaKorisnika;
    private Button noviKorisnik;

    public PretragaKorisnici() {
        // Required empty public constructor
    }


    public static PretragaKorisnici newInstance(MyRunnable korisnikVM) {
        PretragaKorisnici fragment = new PretragaKorisnici();
        Bundle args = new Bundle();
        args.putSerializable(Korisnik_Key, korisnikVM);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            callback = (MyRunnable<KorisnikVM>) getArguments().getSerializable(Korisnik_Key);
        }
        setStyle(STYLE_NORMAL, R.style.MojDijalog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pretraga_korisnici, container, false);

        pretraga = view.findViewById(R.id.searchView);
        listaKorisnika = view.findViewById(R.id.listaKorisnika);
        noviKorisnik = view.findViewById(R.id.NoviKorisnik);


        popuniPodatke("");
        pretraga.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                popuniPodatke(s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                popuniPodatke(s);
                return false;
            }
        });
        noviKorisnik.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                do_BtnNoviKorisnik();
            }
        });

        return view;
    }



    private void popuniPodatke(String s) {
        podaci = Storage.getKorisniciByName(s);

        listaKorisnika.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return podaci.size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                view = inflater.inflate(R.layout.stavka_korisnik, viewGroup, false);

                TextView txtIme = view.findViewById(R.id.prvaLinija);
                TextView txtAdresa = view.findViewById(R.id.drugaLinija);

                KorisnikVM k = podaci.get(i);

                txtIme.setText(k.getIme() + " " + k.getPrezime());
                txtAdresa.setText(k.getAdresaOpstina());
                return view;
            }
        });

        listaKorisnika.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                KorisnikVM k = podaci.get(i);

                getDialog().dismiss();
                callback.run(k);
            }
        });
    }

    private void do_BtnNoviKorisnik() {
        getDialog().dismiss();

        Util.otvoriFragmentKaoDijalog(getActivity(), NoviKorisnikDialog.newInstance(callback, pretraga.getQuery().toString()));
    }


}

1 个答案:

答案 0 :(得分:0)

根据您的代码段,listaKorisnika是您的ListView实现ListView长按事件。

  listaKorisnika.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
                 Log.d("Listview long  click");
                 String selectedString = listView.getItemAtPosition(index).toString();
                 Log.d("long click:" +selectedString);
                return true;
            }
});