如果加载图像,则onListItemClick无法正常工作

时间:2012-05-21 06:06:05

标签: android android-listview

我有一个带有两个textview和一个图像视图的自定义列表视图。点击显示自定义对话框的每一行iam。打开列表视图并单击可以查看对话框的行,而如果我滚动并单击行对话框则不会显示。

我的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();

    }

我观察到的另一件事是,如果它是一个textview(而不是imageview),即使我滚动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" />

1 个答案:

答案 0 :(得分: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);

在适配器类的return convertView;行上方编写onclick监听器 onclick听众是这样的

root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//toast display dialog code here
}
});