0

I have designed a ListFragment. On itemLongClick(), an activity starts. onActivityResult(),I set the adapter again and a new text view is being created dynamically. onClick of each views in an item, a Toast shoud appear. If I implement onClick() on each views in adapter then itemLongClick() is not working.

I have attached my code below:

inside ListFragment:

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                clickPosition = position;
                Intent intent = new Intent(parent.getContext(),AddEventActivity.class );
                startActivityForResult(intent, CREATE_EVENT);
                return true;
            }
        });
    }

inside adapter getView():

@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = (View)inflater.inflate(R.layout.event_listview_items, null);
        TextView hour = (TextView)view.findViewById(R.id.hour);
        LinearLayout layout = (LinearLayout)view.findViewById(R.id.layout);


        hour.setText(hourAL.get(position));
        if(eventAL.size() != 0){
            for(int i=0;i<eventAL.size();i++){
                if(eventAL.get(i).getPosition() == position){
                    for(int j =0;j<eventAL.get(i).eventName.size();j++){
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LayoutParams.FILL_PARENT,1.0f);
                        params.setMargins(3, 3, 3, 3);
                        final TextView txt = new TextView(parent.getContext());
                        txt.setId(++textid);
                        txt.setBackgroundColor(Color.GRAY);
                        txt.setLayoutParams(params);
                        txt.setText(eventAL.get(i).eventName.get(j));
                        layout.addView(txt);

                        txt.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(parent.getContext(), txt.getId() + " clicked", Toast.LENGTH_SHORT).show();

                            }
                        });
                    }
                }
            }
        }
        return view;
    }

Please someone guide me how to implement both functions.

13
  • Megha, You can write your LongClick inside getView() method. on Clicking on convertView you can do task whatever you want. Commented Nov 13, 2014 at 11:10
  • Either you have given on lick to your item child or whole item click,both are not work in your case. Commented Nov 13, 2014 at 11:19
  • try txt.getId().toString in Toast... Commented Nov 13, 2014 at 11:23
  • @pratt I cannot do that as there are other ListViews using the same adapter. Commented Nov 13, 2014 at 11:31
  • @MSGadag this doesn't make any change Commented Nov 13, 2014 at 11:33

1 Answer 1

1

I have found out the solution. For my dynamic views, I have made setLongClickable() as true.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.