3

我得到我的SD卡的文件列表,並在ListView像自定義適配器的幫助一樣,它顯示:setOnItemClickListener上的ListView不燒

adapter = new ArrayAdapter<Item>(this, 
      R.layout.file_manager, R.id.checkedTextItem, 
      fileList) 
      { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // creates view 
      LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = inflater.inflate(R.layout.check_item, null); 

      CheckedTextView textView = (CheckedTextView) view 
        .findViewById(R.id.checkedTextItem); 

      // put the image on the text view 
      textView.setCompoundDrawablesWithIntrinsicBounds(
        fileList[position].icon, 0, 0, 0); 

      textView.setTextColor(Color.WHITE); 
      textView.setText(fileList[position].file); 
      if(fileList[position].icon == R.drawable.directory_icon) 
       textView.setCheckMarkDrawable(null); 


      // add margin between image and text (support various screen 
      // densities) 
      int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); 
      textView.setCompoundDrawablePadding(dp5); 

      return view; 

     } 
    }; 

我想要實現setonitemclicklistener什麼像那樣收聽檢測項目的點擊事件。我的onCreate()方法中的活動:

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.file_manager); 
    lv = (ListView)findViewById(R.id.fileManagerList); 
    loadFileList(); 
    file_list = findViewById(R.id.filesList); 
    lv.setAdapter(adapter); 

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) { 
      //String selectedFromList = (lv.getItemAtPosition(myItemInt).toString()); 
      Toast toast = Toast.makeText(getApplicationContext(), "Hello world!", Toast.LENGTH_LONG); 
      toast.show(); 
      }     
    }); 

} 

我的動態的XML:

<?xml version="1.0" encoding="UTF-8"?> 

<RelativeLayout 

       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/fileManager" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       > 
    <ListView 

     android:background="#000000" 
     android:focusable="false" 
     android:id="@+id/fileManagerList" 
     android:layout_width="fill_parent" 
     android:layout_above="@+id/closecalmlayout"  
     android:layout_height="wrap_content" > 
    </ListView> 


     <LinearLayout 
      android:id="@+id/closecalmlayout" 
      android:layout_alignParentBottom="true" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:weightSum="1.0" > 

       <Button 
        android:id="@+id/btnOk" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginLeft="5dip" 
        android:layout_marginTop="5dip" 
        android:layout_weight=".50" 
        android:text="Attach files" 
        /> 

       <Button 
        android:id="@+id/btnCancel" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginLeft="5dip" 
        android:layout_marginRight="5dip" 
        android:layout_marginTop="5dip" 
        android:layout_weight=".50" 
        android:text="Do not attach" 
        /> 

      </LinearLayout> 
</RelativeLayout> 

而且我CheckedTextView活動

<?xml version="1.0" encoding="UTF-8"?> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp"> 

     <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/checkedTextItem" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical" 
      android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
      android:textColor="#000000" 
      android:focusable="false" 
      android:paddingLeft="10dip" 
      android:paddingRight="6dip" 
      android:typeface="sans" android:textSize="16dip"/> 

</LinearLayout> 

但是當我點擊的項目,沒有任何反應。我嘗試在onResume()方法中創建setOnItemClickListener,但效果相同。我也嘗試onclicklistener - 同樣的效果。這是什麼原因?

+1

好像在列表視圖的列東西把焦點 – Carnal

+0

也許CheckedTextView返回true,嘗試刪除它,看看它是否有效! – Carnal

+0

你想從列表中獲得點擊的項目,我是對的。 – Harish

回答

6

在我看來,一些視圖正在從你的列表視圖中獲取焦點,當你知道它是哪個視圖時,在你的xml中使用該視圖上的android:focusable="false",它應該做到這一點。

我想你的代碼,並onItemClicked被調用,這是我getView():

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

     CheckedTextView textView = (CheckedTextView) view.findViewById(R.id.checked); 
     textView.setText("Hello"); //test, you can do whatever you want with this 

     int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); 
     textView.setCompoundDrawablePadding(dp5); 

     return view; 
    } 

如何設置適配器(MyAdapter):

MyAdapter adapter = new MyAdapter(this, 0, arrayList); 
lv.setAdapter(adapter); 
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(PlayingAroundActivity.this, "Hello", Toast.LENGTH_LONG).show(); 
     }     
    }); 
+0

我試圖用我的listView,也用CheckedTextView,但它並沒有幫助... –

+0

android:focusable不應該被列表視圖使用,而是在每個行中的視圖需要焦點。你還有什麼? – Carnal

+0

我怎麼才能知道現在使用什麼視圖? –

0

獲取所選項目從列表視圖。

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) { 
      //String selectedFromList = (lv.getItemAtPosition(myItemInt).toString()); 
      Toast toast = Toast.makeText(getApplicationContext(), "Hello world!"+fileList(myItemInt), Toast.LENGTH_LONG); 
      toast.show(); 
      }     
    }); 
+0

我使用相同的方法,你可以在onCreate()方法中看到它我的帖子。 它沒有爲我工作。 –

0

@覆蓋 公共布爾areAllItemsEnabled(){ 回報真正; //如果使用ListAdapter }

@Override 
public boolean isEnabled(int position) { 
    return **true**; //return True if using ListAdapter 
} 
相關問題