0

我想设计 ListView 中的每个列表项都可以点击,并触发点击哪个列表项。但它不能。我在我的代码上尝试了两种方法:setOnItemClickListener() 和 setOnItemSelectedListener()。我已经在谷歌上搜索了一些关于这篇文章的参考资料,但它仍然无法工作(可点击)。
我想发布下面的代码:代码可以显示列表项,我可以看到Log.d(" mListView01.getCount()=",""+vc);行的 Log.d 内容 在 LogCat 上很好。但是,如果我单击列表项,则没有任何响应。如果你不介意,你能帮我指出我错在哪里,谢谢!

使用 Activity Widget 创建 listView 的代码:

......
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();

            }
        });
......

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:我在这里找到一篇关于布局上的焦点设置会导致可点击工作或不工作的文章。所以,我删除了(我不在这里编码)在布局的 xml 文件中。然后 setOnItemSelectedListener() 方法在滚动列表列表时使用橙色焦点更改。但它仍然没有达到我的预期。

4

1 回答 1

3

问题解决了!经过几家谷歌搜索/搜索和try_eror。如果你很有趣,我想分享它。

问题的主要原因是:我使用ScrollView作为 row.xml 的基本布局(包含每个 listview 行的内容)。然后,我使用 LinearLayout(Vertial)代替它。setOnItemClickedListener() 方法现在可以正常工作。我对此一无所知,这会导致 ListView 不可点击。如果有人知道,请告诉我们,

于 2012-06-01T13:04:04.840 回答