0

我有一个带有两个文本视图和一个图像视图的自定义列表视图。单击每一行时,我会显示一个自定义对话框。打开列表视图并单击行后,我能够查看对话框,而如果我滚动并单击行,则不会显示对话框。

我的 onListItemclick 代码如下`

final Dialog password = new Dialog(VCStegoInboxWithOutCheckboxes.this);
        password.setContentView(R.layout.dialog);
        password.setTitle("Enter your Password");

        final EditText getPassword = (EditText) password
                .findViewById(R.id.edconfirmpwd);
        Button okButton = (Button) password.findViewById(R.id.ok);
        okButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                String ConfirmCode = getPassword.getText().toString();
                SharedPreferences settingsActivity = getSharedPreferences(
                        "UserName", Activity.MODE_PRIVATE);
                String Password = settingsActivity.getString("Password", null);

                if (ConfirmCode.equals(Password)) {
                                        Intent showMsgIntent = new Intent();
                        showMsgIntent.setClass(getApplicationContext(),
                                VCStegoShowMessageScreen.class);

                        startActivity(showMsgIntent);


                } else {
                    Toast.makeText(getBaseContext(), "InvalidPassword",
                            Toast.LENGTH_SHORT).show();

                }

                password.dismiss();

            }
        });
        Button cancelButton = (Button) password.findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Entered on cancel button clicked");
                password.dismiss();

            }
        });
        password.show();

    }

我观察到的另一件事是它是否是文本视图(而不是图像视图),即使我滚动 iam 能够为每一行显示对话框。

我的适配器方法是

public class InboxAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private TextView Tvname, TVtimeStamp;
    private ImageView IVmsg;

    public InboxAdapter(Context context, List<VCStegoInboxList> inboxList) {
        inflater = LayoutInflater.from(context);
        VCStegoInboxWithOutCheckboxes.Inboxlist = inboxList;
    }
    public class ViewHolder{
           private TextView Tvname;
           private TextView TVtimeStamp;              
           private ImageView IVmsg;
          }

    public int getCount() {
        return VCStegoInboxWithOutCheckboxes.Inboxlist.size();
    }

    // @Override
    public Object getItem(int position) {
        return VCStegoInboxWithOutCheckboxes.Inboxlist.get(position);
    }

    // @Override
    public long getItemId(int position) {
        return position;
    }

    // @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder;

           if (convertView==null){

            holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.inboxtextviews, null);
        Tvname = (TextView) convertView.findViewById(R.id.tvinboxname);

        IVmsg = (ImageView) convertView.findViewById(R.id.tvinboxmsg);



        TVtimeStamp = (TextView) convertView.findViewById(R.id.tvtimeStamp);

        convertView.setTag(holder);
       }else{

        holder = (ViewHolder) convertView.getTag();
       }
           Tvname.setText(VCStegoInboxWithOutCheckboxes.Inboxlist
                .get(position).getname());
           IVmsg.setImageBitmap(VCStegoInboxWithOutCheckboxes.Inboxlist.get(
                    position).getBitmap());
           TVtimeStamp.setText(VCStegoInboxWithOutCheckboxes.Inboxlist.get(
                    position).getTimeStamp());
        return convertView;
    }

}

我的 xml 文件是

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/tvinboxname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/tvinboxmsg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:layout_below="@+id/tvinboxname"

    android:contentDescription="@string/contentDes" />

<TextView
    android:id="@+id/tvtimeStamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/tvinboxmsg"
    android:textAppearance="?android:attr/textAppearanceSmall" />

4

1 回答 1

1

为您的根布局 id 分配 id,例如

  <RelativeLayout 
     android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<your TextView/>
<your ImageView/>
<your TextView/>

</RelativeLayout>

在适配器类中获取根,例如

root = (RelativeLayout) convertView.findViewById(R.id.root);

在你的适配器类onclick监听器上面写上onclick监听return convertView;器是这样的

root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//toast display dialog code here
}
});
于 2012-05-21T06:46:38.110 回答