在特定行上单击按钮时,textview在列表视图中不可见

时间:2017-10-13 05:39:11

标签: android listview android-arrayadapter android-adapter

我有一个自定义listview,其中包含buttontextviewtextview知名度已消失。当button文字是"电子邮件"如果我点击buttontextview应显示在button下方。但最后textview始终可见。

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {

    wantedUsers du = wusers.get(i);

    final String mcontact = du.mcontact;

    if(view==null) {
        viewholder = new viewholder();
        view = LayoutInflater.from(ws).inflate(R.layout.wantedmoviecustom, null);
         viewholder.mcontwcustom = (Button)view.findViewById(R.id.mcontwcustom);

        viewholder.mdisplayemail = (TextView) view.findViewById(R.id.mdisplayemail);
        view.setTag(viewholder);
    } else {
        viewholder = (viewholder) view.getTag();
    }

    viewholder.mdisplayemail.setVisibility(View.GONE);

    if(mcontact.contains("@")) {
        viewholder.mcontwcustom.setText("Show Email");
        viewholder.mdisplayemail.setText(mcontact);
    } else {
        viewholder.mcontwcustom.setText("Call");
    }

    viewholder.mcontwcustom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(mcontact.contains("@")) {
                int p=(Integer)view.getTag();

                viewholder.mdisplayemail.setVisibility(View.VISIBLE);
               // viewholder.mdisplayemail.setText(mcontact);

            }
            else
            {
                Uri call = Uri.parse("tel:" + mcontact);
                Intent surf = new Intent(Intent.ACTION_DIAL, call);
                ws.startActivity(surf);
            }
        }
    });
    viewholder.mcontwcustom.setTag(i);
    return view;
} 
下面的

来自自定义getview的{​​{1}}。那么当adapter点击特定行时,如何制作textview visbile

2 个答案:

答案 0 :(得分:1)

首先,您需要创建全局变量

int positionOfButtonClick = -1;

然后您需要在button点击时更新您的位置,并且需要致电notifyDataSetChanged();

喜欢 - :

 viewholder.mcontwcustom.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       positionOfButtonClick = i;  
       notifyDataSetChanged();
     //your other code    
});

然后,在 getView

您需要检查位置并需要VISIBLE textView喜欢 - :

if(i==positionOfButtonClick ){
viewholder.mdisplayemail.setVisibility(View.VISIBLE);
}

现在,您的 getView 应为 - :

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {

wantedUsers du = wusers.get(i);

final String mcontact = du.mcontact;



if(view==null) {
    viewholder = new viewholder();
    view = LayoutInflater.from(ws).inflate(R.layout.wantedmoviecustom, null);
     viewholder.mcontwcustom = (Button)view.findViewById(R.id.mcontwcustom);

    viewholder.mdisplayemail = (TextView) view.findViewById(R.id.mdisplayemail);


if(i==positionOfButtonClick ){
viewholder.mdisplayemail.setVisibility(View.VISIBLE);
}else{
    viewholder.mdisplayemail.setVisibility(View.GONE);
  }


    view.setTag(viewholder);
} else {
    viewholder = (viewholder) view.getTag();
}



if(mcontact.contains("@")) {
    viewholder.mcontwcustom.setText("Show Email");
    viewholder.mdisplayemail.setText(mcontact);
} else {
    viewholder.mcontwcustom.setText("Call");
}

viewholder.mcontwcustom.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if(mcontact.contains("@")) {
            int p=(Integer)view.getTag();



       positionOfButtonClick = i;  
       notifyDataSetChanged();

        }
        else
        {
            Uri call = Uri.parse("tel:" + mcontact);
            Intent surf = new Intent(Intent.ACTION_DIAL, call);
            ws.startActivity(surf);
        }
    }
});
viewholder.mcontwcustom.setTag(i);
return view;
}

答案 1 :(得分:0)

如下所述更改代码,我认为它对您有帮助。

viewholder.mcontwcustom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(mcontact.contains("@")) {
                int p=(Integer)view.getTag();

               viewholder.mdisplayemail.setVisibility(View.VISIBLE);
               // viewholder.mdisplayemail.setText(mcontact);

            }