SetOnClickListener在DialogFragment中不起作用。

huangapple go评论77阅读模式
英文:

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&lt;Karateka&gt; data;
ApiUtils apiUtils;
public AdapterListviewChangeStartingKarateka(Context context, int item_Layaut, ArrayList&lt;Karateka&gt; 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&lt;?&gt; adapterView, View view, int i, long l) {
Log.d(&quot;indexListview&quot;, 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(&quot;indexListview&quot;, String.valueOf(i));
}
}

huangapple
  • 本文由 发表于 2020年5月5日 02:58:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61599620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定