내 앱에는 하나의 목록보기가있는 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을 사용하고 있습니다.
이 사실 나는이 시도 match_parent 이전에 한 번, 이제 다시 했어요. (목록보기와 연결된 모든 항목을 제거하고 OnListItemClicked (...) 만 남겨 둡니다.)하지만 여전히 작동하지 않습니다. 내 항목의 레이아웃에이 작업과 관련이 있습니까? –
@ Michał - 아마도 그렇게 편집하십시오. 귀하의 질문 및'getView'에 대한 어댑터에서 사용하는 코드와 관련된 모든 레이아웃 XML 파일 –
내가 말한 내용으로 인해 코드가 변경되었고 목록보기에서 항목과 연결된 모든 항목이 업로드되었습니다. –