英文:
SetOnClickListener doesn't work in DialogFragment
问题
以下是您要翻译的内容:
我正在尝试在 DialogFragment 内部的 ListView 中单击时获取索引,但是我什么都没有得到... 我不知道为什么...
这是我在其中使用 setOnClickListener 的 OnCreatedDialog
:
这是我的适配器,我正在尝试在这里设置 setOnClickListener:
public class AdapterListviewChangeStartingKarateka extends ArrayAdapter {
Context context;
int item_Layaut;
ArrayList<Karateka> data;
ApiUtils apiUtils;
public AdapterListviewChangeStartingKarateka(Context context, int item_Layaut, ArrayList<Karateka> data) {
super(context, item_Layaut,data);
this.context = context;
this.item_Layaut = item_Layaut;
this.data = data;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(item_Layaut, parent, false);
}
String image = data.get(position).getPhoto_karateka();
ImageView elementImage = convertView.findViewById(R.id.change_starting_picture_karateka);
if(image!= null || !image.isEmpty() ) {
Picasso.get().load(apiUtils.BASE_URL_PICTURE + image).fit().into(elementImage);
} else {
elementImage.setImageResource(R.drawable.default_image);
}
Button buttonToChange= convertView.findViewById(R.id.item_button_to_change);
ListView list = convertView.findViewById(R.id.starting_listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("indexListview", String.valueOf(i));
}
});
}
}
英文:
I am trying to get the index when I click in the listview inside the DialogFragment, but I don't get anything... I don't know why...
This is the OnCreatedDialog
where I have the setOnClickListener
:
This is my Adapter, I am trying to put the setOnClickListener here:
public class AdapterListviewChangeStartingKarateka extends ArrayAdapter {
Context context;
int item_Layaut;
ArrayList<Karateka> data;
ApiUtils apiUtils;
public AdapterListviewChangeStartingKarateka(Context context, int item_Layaut, ArrayList<Karateka> data) {
super(context, item_Layaut,data);
this.context = context;
this.item_Layaut = item_Layaut;
this.data = data;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(item_Layaut, parent, false);
}
String image = data.get(position).getPhoto_karateka();
ImageView elementImage = convertView.findViewById(R.id.change_starting_picture_karateka);
if(image!= null || !!!image.isEmpty() ) {
Picasso.get().load(apiUtils.BASE_URL_PICTURE + image).fit().into(elementImage);
}else{ elementImage.setImageResource(R.drawable.default_image); }
Button buttonToChange= convertView.findViewById(R.id.item_button_to_change);
ListView list = convertView.findViewById(R.id.starting_listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("indexListview", String.valueOf(i));
}
});
}
答案1
得分: 0
尝试实现一个ListView适配器,并在适配器的getView()
方法中定义您的监听器。
您应该为每个视图元素设置监听器。
viewKarateka.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
Log.d("indexListview", String.valueOf(i));
}
});
英文:
Try to implement an ListView adapter and define your listener in adapter's getView()
method.
You should set listener for each view element.
viewKarateka.setOnClickListener(new View.OnClickListener() {
public void onClick(final View v) {
Log.d("indexListview", String.valueOf(i));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论