cmh
cmh

Reputation: 271

Android ListView Item is not Clickable

I want to design each list item in the ListView can be clickable, and triger out which list item be clicked. But it can not. I tried the two methods: setOnItemClickListener() and setOnItemSelectedListener() on my code. I have had googled couple of references about the article, however it still can not work(clickable).
I would like post the code below: The code can display the list items and I can see the Log.d content for the line of Log.d(" mListView01.getCount()="," "+vc); on LogCat well. But, there is no any response if I clicked on the list Item. if you don't mind, could you help point me out where I was wrong, thanks !

Code for creating the listView using the Activity Widget:

......
setContentView(R.layout.main_open);
    TextView itemText = (TextView) findViewById(R.id.itemText);
    TextView codeText = (TextView) findViewById(R.id.codeText);
    itemText.setText(selectedItem);
    codeText.setText(selectedCode);
    ListView mListView01 = (ListView)findViewById(R.id.main_open_listview1);
    String[] keys = new String[] {"title","title_image", "content",
            "title1","title1_image","content1","title2","title2_image","content2"};
    int[] resValues =  new int[] { R.id.title, R.id.title_image, R.id.content,
            R.id.title1, R.id.title1_image, R.id.content1,R.id.title2, R.id.title2_image, R.id.content2};
    openDocAdapter opendoc = new openDocAdapter(this,localdcoumentlist, R.layout.main_open_content, keys, resValues );
    mListView01.setSelected(true);
    mListView01.setClickable(true);
    mListView01.setAdapter(opendoc);       

    int vc = mListView01.getCount();
    Log.d(" mListView01.getCount()="," "+vc);

    mListView01.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Log.d("Selected From setOnItemSelectedListener, arg2=", " "+ arg2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


    mListView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                selectedViewPos = arg2;
                Log.d("TitlesSelectionDialog(),selectedViewPos= "," "+ selectedViewPos);
                Toast.makeText(getApplicationContext(), "selectedViewPos= "+ selectedViewPos, Toast.LENGTH_LONG).show();

            }
        });
......

Code for openDocAdapter:

   private class openDocAdapter extends SimpleAdapter
    {
      private Context _con;
      private List _List;
      private int _listviewId;
      private String[] _keys;
      private int[] _resValues;


      public openDocAdapter(Context context, ArrayList<HashMap<String,Object>> List , int listviewId, String[] keys, int[] resValues ) 
      {
        super(context, List, listviewId, keys, resValues);
        _con =context;
        _List = List;
        _listviewId = listviewId;
        _keys = keys;
        _resValues = resValues;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent)
      {
          View v = convertView;
          if (v == null) {
              LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              v = vi.inflate(R.layout.main_open_content, null);
          }

          TextView title = (TextView) v.findViewById(R.id.title);
          (...Similiar codes define  textView, imageViewsd.)                            

          return v;
      }


      @Override
      public int getCount()
      {
        // TODO Auto-generated method stub
        return super.getCount();
      }
      @Override
      public Object getItem(int position)
      {
        // TODO Auto-generated method stub
        return super.getItem(position);
      }
      @Override
      public long getItemId(int position)
      {
        // TODO Auto-generated method stub
        return super.getItemId(position);
      }
    }  

Edit1: I found an article here About the Focus setting on the layout will cause clickable work or not work. So, I remove the lines of (I don't while it be coded here) in the xml file of layout. Then the setOnItemSelectedListener() method is worked while scrolling the list list with orange focus change. But it still not meet my expection.

Upvotes: 0

Views: 1567

Answers (1)

cmh
cmh

Reputation: 271

Provlem Solved ! After couple hous googling/search and try_eror. And I would like share it if you are interesting.

The main cause of the problem is: I used the ScrollView as the basic layout for the row.xml(containing the content for each listview row). Then, I used the LinearLayout(Vertial) instead of it. The setOnItemClickedListener() method works fine now. I do not have any idea regarding this that will cause the ListView to be not clickable. If somebody know it, please tell us,

Upvotes: 3

Related Questions