megha
megha

Reputation: 301

ListView on click listener

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.

Upvotes: 0

Views: 114

Answers (1)

megha
megha

Reputation: 301

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

Upvotes: 1

Related Questions