ArrayAdapter使用,佈局設置內容,listview 設置setOnItemClickListener無響應

代碼如下:
<pre name="code" class="java">myList=(ListView)findViewById(R.id.mylist);  <span style="font-family: Arial, Helvetica, sans-serif;">   </span>

ArrayAdapter<String> mAdapter=new ArrayAdapter<String>(this,R.layout.myitem);
        mAdapter.add(getResources().getString(R.string.my_makemoney_record));
        mAdapter.add(getResources().getString(R.string.my_exchange_record));
        mAdapter.add(getResources().getString(R.string.my_update_password));
        //添加消息處理  
        myList.setOnItemClickListener(new OnItemClickListener() 
        {

<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onItemClick(AdapterView<?> parent, View view,
<span style="white-space:pre">					</span>int position, long id) {
<span style="white-space:pre">				</span>Log.e("MainActivity","click.position="+position);<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>}
        <span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>});
        myList.setAdapter(mAdapter);  
			}
未修改R.layout.myitem佈局如下:
<span style="font-family: Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="utf-8"?></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><TextView xmlns:android="http://schemas.android.com/apk/res/android"</span>
    android:id="@+id/my_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:paddingTop="5dip"
    android:paddingBottom="5dip"
    android:textColor="@color/blue"
    >
</TextView>
運行發現不進入 <span style="font-family: Arial, Helvetica, sans-serif;">onItemClick事件裏面,沒有打印響應的log</span>
<span style="font-family:Arial, Helvetica, sans-serif;">錯誤原因:</span><span style="font-family: Arial, Helvetica, sans-serif;">myList在設置</span><span style="font-family: Arial, Helvetica, sans-serif;">ArrayAdapter<String>時,自定義的佈局文件內部最外層需要填充父類佈局纔會響應onItemClick事件</span>
<span style="font-family:Arial, Helvetica, sans-serif;"></span><pre name="code" class="java">修改的R.layout.myitem佈局如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:paddingTop="5dip"
    android:paddingBottom="5dip"
    android:textColor="@color/blue"
    >
</TextView>
再次運行ok!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章