2014-09-08 5 views
0

내 앱에는 하나의 목록보기가있는 Main Activity (List Activity를 확장)가 있습니다. 이 목록보기의 항목을 클릭 할 수있게 만들고 클릭 이벤트를 처리하려고합니다. 여기에 내 코드입니다 :ListActivity의 ListView 항목이 unclickable 상태로 유지됩니다.

public class MainActivity extends ListActivity { 
    private ArrayList<Item> m_parts = new ArrayList<Item>(); 
    private ItemAdapter m_adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    (…) 
    m_adapter = new ItemAdapter(this, R.layout.item_layout, m_parts); 
    setListAdapter(m_adapter); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     (…) 
    } 


} 

그리고 여기에 주요 활동 레이아웃 내 XML의 :

public class Item { 
private Bitmap image; 
private Uri uri; 
private String title; 
private String date; 
private String latitude; 
private String longitude; 
public CheckBox checkBox = null; 

public Item(){} 

public Item(Bitmap bi, Uri ur, String ti, String da, String la, String lo){ 
    this.image = bi; 
    this.uri = ur; 
    this.title = ti; 
    this.date = da; 
    this.latitude = la; 
    this.longitude = lo; 
} 
public Bitmap getImage() {return image;} 

public String getTitle(){ 
    return title; 
} 

public String getDate(){ 
    return date; 
} 

public String getLatitude(){ 
    return latitude; 
} 

public String getLongitude(){ 
    return longitude; 
} 

public Boolean isChecked(){ 
    return checkBox.isChecked(); 
}; 

public Uri getUri(){ 
    return uri; 
} 
} 

항목의 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight"> 

<ImageView 
    android:id="@+id/photo" 
    android:layout_width="60dp" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:scaleType="fitXY"/> 

<TextView 
    android:id="@+id/place" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/photo" 
    android:textColor="@color/white" 
    android:textColorHighlight="@color/white" 
    android:textColorHint="@color/white" 
    android:textColorLink="@color/white" 
    android:hint="@string/founding_place_text" /> 

<TextView 
    android:id="@+id/date" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@+id/photo" 
    android:textColor="@color/white" 
    android:textColorHighlight="@color/white" 
    android:textColorHint="@color/white" 
    android:textColorLink="@color/white" 
    android:hint="@string/founding_date_text" /> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/photo" 
    android:maxLines="1" 
    android:textColor="@color/white" 
    android:textColorHighlight="@color/white" 
    android:textColorHint="@color/white" 
    android:textColorLink="@color/white" 
    android:hint="@string/founding_title_text_2" /> 

<CheckBox 
    android:id="@+id/checkBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true"/> 
</RelativeLayout> 

이 내 항목 클래스는

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/main_background" 
tools:context=".MainActivity"> 

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
</ListView> 


</LinearLayout> 

입니다 그리고 결국 내 상품 어댑터 :

목록보기에서 항목을 클릭하면 onListItemClick (...)이 호출되지 않습니다. 게다가 강조 표시조차되지 않습니다. 클릭 이벤트를 처리하기 위해 코드에 무엇을 추가해야하는지 알 수 있습니까? Android 4.3을 사용하고 있습니다.

답변

0

목록보기를 클릭 가능으로 설정하지 말고 OnItemClickListener을 설정하지 마십시오. 즉, setlistAdapter에 대한 호출로 onCreate() 메서드를 종료하십시오. onListItemClicked을 재정 의하여해야합니다. ListActivity 인프라가 나머지를 처리합니다. 리스너를 직접 설정하면 프레임 워크의 메커니즘을 방해하게됩니다.

+0

이 사실 나는이 시도 match_parent 이전에 한 번, 이제 다시 했어요. (목록보기와 연결된 모든 항목을 제거하고 OnListItemClicked (...) 만 남겨 둡니다.)하지만 여전히 작동하지 않습니다. 내 항목의 레이아웃에이 작업과 관련이 있습니까? –

+0

@ Michał - 아마도 그렇게 편집하십시오. 귀하의 질문 및'getView'에 대한 어댑터에서 사용하는 코드와 관련된 모든 레이아웃 XML 파일 –

+0

내가 말한 내용으로 인해 코드가 변경되었고 목록보기에서 항목과 연결된 모든 항목이 업로드되었습니다. –

0

제거 안드로이드 : 클릭 = 코드에서 레이아웃과 listView.setClickable (참)에서 "true"로하고 listView.setOnItemClickListener 설정하고 넣어 안드로이드 : layout_height = "wrap_content"안드로이드 : layout_width = "

+0

답장을 보내 주셔서 감사합니다 :) 불행히도 그것은 여전히 ​​작동하지 않습니다. –

관련 문제