사용자 지정 어댑터가있는 목록 뷰가 있습니다. listView의 사용자 정의 행에는 클릭 할 때 무언가를 처리해야하는 2 개의 이미지 버튼이 있습니다. 하지만 응용 프로그램을 시작하고 imagebutton을 클릭하면 아무 일도 일어나지 않지만, 먼저 행을 클릭 한 다음 imageButton을 클릭하면 내 클릭이 처리되기 때문에 원하는대로 잘 작동하지 않습니다. 다른 사용자에게 도움이되는 몇 가지 솔루션을 시도했지만, 아무도 그저 행 클릭 후 처리되는 것과 똑같은 문제가 없었습니다.사용자 지정 목록보기 내에서 imageButton 클릭 처리
FavoriteActivity
public class FavoriteActivity extends Activity {
private List<Favorite> favoriteItem = new ArrayList<Favorite>();
ViewHolder holder = new ViewHolder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
populateFavoriteList();
populateListview();
registerClickCallBack();
}
private void populateFavoriteList() {
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 1"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 2"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 3"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 4"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 5"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 6"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 7"));
favoriteItem.add(new Favorite(R.drawable.icon_camera, "item 8"));
}
private void populateListview() {
ArrayAdapter<Favorite> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.favoriteList);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Favorite> {
public MyListAdapter() {
super(FavoriteActivity.this, R.layout.favorite_row, favoriteItem);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// To be sure we have a view, because null is possible
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.favorite_row, parent, false);
}
// Find the item to work with
Favorite currentItem = favoriteItem.get(position);
// Filling the View
holder.thumbnail = (ImageButton) itemView.findViewById(R.id.favoriteThumbnail);
holder.thumbnail.setBackgroundResource(currentItem.getThumbnail());
holder.name = (TextView) itemView.findViewById(R.id.favoriteName);
holder.name.setText(currentItem.getName());
return itemView;
}
}
private void registerClickCallBack() {
ListView list = (ListView) findViewById(R.id.favoriteList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
final Favorite favoriteClicked = favoriteItem.get(position);
String msg = "Clicked position " + position + " which is " + favoriteClicked.getName();//Can I use my holder here to get the name back?? Because it wasn't working...
Toast.makeText(FavoriteActivity.this, msg, Toast.LENGTH_LONG).show();
ImageButton btnDirections = (ImageButton) viewClicked.findViewById(R.id.favoriteDirections);
ImageButton btnDelete = (ImageButton) viewClicked.findViewById(R.id.favoriteDelete);
btnDirections.setOnClickListener(new OnClickListener() {
String btnmsg = "getting directions for " + favoriteClicked.getName();
@Override
public void onClick(View v) {
Toast.makeText(FavoriteActivity.this, btnmsg, Toast.LENGTH_LONG).show();
}
});
btnDelete.setOnClickListener(new OnClickListener() {
String btnmsg = "getting delete for " + favoriteClicked.getName();
@Override
public void onClick(View v) {
Toast.makeText(FavoriteActivity.this, btnmsg, Toast.LENGTH_LONG).show();
}
});
}
});
}
static class ViewHolder {
ImageButton thumbnail;
TextView name;
}
}
FavoriteClass
public class Favorite {
private int favoriteThumbnail;
private String favoriteName;
public Favorite(int favoriteThumbnail, String favoriteName){
super();
this.favoriteThumbnail = favoriteThumbnail;
this.favoriteName = favoriteName;
}
public int getThumbnail(){
return favoriteThumbnail;
}
public String getName(){
return favoriteName;
}
}
favorite_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:descendantFocusability="blocksDescendants">
<ImageButton
android:layout_width="75dp"
android:layout_height="65dp"
android:contentDescription="thumbnail"
android:src="@drawable/icon_camera"
android:layout_weight="2"
android:id="@+id/favoriteThumbnail"
android:scaleType="fitXY"
style="@style/favoriteRowItems"
android:focusable="false"/>
<TextView
android:id="@+id/favoriteName"
android:layout_weight="2"
android:layout_gravity="center_vertical"
android:text="Name"
android:textSize="30sp"
style="@style/favoriteRowItems"/>
<ImageButton
android:contentDescription="Direction"
android:layout_weight="0.5"
android:src="@drawable/icon_directions"
android:id="@+id/favoriteDirections"
style="@style/favoriteRowItems"
android:focusable="false"/>
<ImageButton
android:contentDescription="Delete from favorite"
android:layout_weight="0.5"
android:src="@drawable/icon_delete"
android:id="@+id/favoriteDelete"
style="@style/favoriteRowItems"
android:layout_marginRight="5dp"
android:focusable="false"/>
</LinearLayout>
이것은 효과가 있습니다. 하지만 이미지 버튼을 클릭하면 이름이 부여되지 않습니다. 이것을 holder.name으로 변경했는데 더 이상 홀더가 필요하지 않습니까? 사용 된'String btnmsg = ""+ holder.name; "에 대한 삭제 가져 오기 – mXX