android-section-list icon indicating copy to clipboard operation
android-section-list copied to clipboard

Unable to setOnItemClick listener

Open gkapusta opened this issue 11 years ago • 0 comments

From [email protected] on September 02, 2013 13:22:59

What steps will reproduce the problem? 1. It does not recognizes which item is clicked. I set listView.setOnItemClickListener and implemented the OnItemClickListener method of android, which simply toasts the item position. To my surprise, if I tap on the section item, the toast is shown. But it does not recognizing the onItemClick for the listView rows. How to fix this? What is the expected output? What do you see instead? What version of the product are you using? On what operating system? Please provide any additional information below. Here is my sample code: package pl.polidea.sectionedlist;

import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast;

/**

  • Example activity. */ public class SectionListActivity extends Activity {

    private class StandardArrayAdapter extends ArrayAdapter<SectionListItem> {

    private final SectionListItem[] items;
    
    public StandardArrayAdapter(final Context context,
            final int textViewResourceId, final SectionListItem[] items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }
    
    @Override
    public View getView(final int position, final View convertView,
            final ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.example_list_view, null);
        }
        final SectionListItem currentItem = items[position];
        if (currentItem != null) {
            final TextView textView = (TextView) view
                    .findViewById(R.id.example_text_view);
            final TextView textView2 = (TextView) view
            .findViewById(R.id.example_text_view2);
            if (textView != null) {
                textView.setText(currentItem.item.toString());
            }
            if (textView2 != null) {
                textView2.setText(currentItem.item.toString());
            }
        }
        return view;
    }
    

    }

    SectionListItem[] exampleArray = { // Comment to prevent re-format new SectionListItem("Test 1 - A", "A"), // new SectionListItem("Test 2 - A", "A"), // new SectionListItem("Test 3 - A", "A"), // new SectionListItem("Test 4 - A", "A"), // new SectionListItem("Test 5 - A", "A"), // new SectionListItem("Test 6 - B", "B"), // new SectionListItem("Test 7 - B", "B"), // new SectionListItem("Test 8 - B", "B"), // new SectionListItem("Test 9 - Long", "Long section"), // new SectionListItem("Test 10 - Long", "Long section"), // new SectionListItem("Test 11 - Long", "Long section"), // new SectionListItem("Test 12 - Long", "Long section"), // new SectionListItem("Test 13 - Long", "Long section"), // new SectionListItem("Test 14 - A again", "A"), // new SectionListItem("Test 15 - A again", "A"), // new SectionListItem("Test 16 - A again", "A"), // new SectionListItem("Test 17 - B again", "B"), // new SectionListItem("Test 18 - B again", "B"), // new SectionListItem("Test 19 - B again", "B"), // new SectionListItem("Test 20 - B again", "B"), // new SectionListItem("Test 21 - B again", "B"), // new SectionListItem("Test 22 - B again", "B"), // new SectionListItem("Test 23 - C", "C"), // new SectionListItem("Test 24 - C", "C"), // new SectionListItem("Test 25 - C", "C"), // new SectionListItem("Test 26 - C", "C"), // };

    private StandardArrayAdapter arrayAdapter;

    private SectionListAdapter sectionAdapter;

    private SectionListView listView;

    @Override public void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.main);
    
    arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view,
            exampleArray);
    sectionAdapter = new SectionListAdapter(getLayoutInflater(),
            arrayAdapter);
    listView = (SectionListView) findViewById(getResources().getIdentifier(
            "section_list_view", "id",
            this.getClass().getPackage().getName()));
    listView.setAdapter(sectionAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView< ? > arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(SectionListActivity.this, "Item : " + arg2, Toast.LENGTH_SHORT).show();
        }
    });
    

    }

    @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.test_menu, menu); return true; }

    @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.standard_list: arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view, exampleArray); sectionAdapter = new SectionListAdapter(getLayoutInflater(), arrayAdapter); listView.setAdapter(sectionAdapter); return true; case R.id.empty_list: arrayAdapter = new StandardArrayAdapter(this, R.id.example_text_view, new SectionListItem[] {}); sectionAdapter = new SectionListAdapter(getLayoutInflater(), arrayAdapter); listView.setAdapter(sectionAdapter); return true; default: return super.onOptionsItemSelected(item); } } }

Original issue: http://code.google.com/p/android-section-list/issues/detail?id=14

gkapusta avatar Apr 16 '14 09:04 gkapusta