简体   繁体   English

如何在Android ListView中动态隐藏/显示按钮

[英]How to hide/show button in Android ListView dynamically

I have a listview with two buttons. 我有一个带有两个按钮的listview

I want replace those two button with textview once I get the response from the server. 从服务器获得响应后,我想用textview替换这两个按钮。

How can I achieve this? 我该如何实现?

After server response: 服务器响应后:

public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);

                        if (obj.getString("response").equals("1")) {
                            JSONObject res = new JSONObject(obj.getString("data"));

                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

I have not created any adapter. 我尚未创建任何适配器。

you need to extend ArrayAdapter for customizing your listview items. 您需要扩展ArrayAdapter来自定义列表视图项。

On your xml, keep both button and text views and for the textview, keep 在xml上,同时保留按钮和文本视图,对于textview,保留

android:visibility="gone"

initialize both textview and button in your adapter class. 初始化适配器类中的textview和button。 when you get the response, enable textview visibility and disable button visibility 收到响应后,启用textview可见性并禁用按钮可见性

textView.setVisilibity(View.VISIBLE);
button.setVisilibity(View.GONE);

ok i understand your problem and the best way is using recyclerview instead of listview , first of all see this example or recyclerview recyclerview example this is simple and best than normal listview , second in onBindViewHolder function you can create your own listner like this 好的,我了解您的问题,最好的方法是使用recyclerview而不是listview,首先查看此示例或recyclerview recyclerview示例,这比普通的listview简单,最好,其次,在onBindViewHolder函数中,您可以像这样创建自己的列表器

 @Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.your_sender_button.setonclickLinstener(new View.OnClickListener { @Override
    public void onClick(View view) {
      yourActivity.sendTimer(holder.btn1, your_sender_button, holder.your_textview_name);
    }
});
}

sendTimer is the function used service to send the timer and the parameters is your views, so when you get the response set the visibility for these views you passed in your function sendTimer是用于发送计时器的函数,参数是您的视图,因此当您获得响应时,请设置函数中传递的这些视图的可见性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM