检测ListView单击自定义适配器

时间:2015-07-27 16:03:24

标签: java android listview android-listview parse-platform

我创建了一个ListView,其中包含一个自定义布局,显示Parse.com上保存的用户 这是我创建的自定义适配器的代码

public class StatusAdapter extends ArrayAdapter {
    protected Context mContext;
    protected List mStatus;

    public StatusAdapter(Context context, List status) {
        super(context, R.layout.usertem, status);
        mContext = context;
        mStatus = status;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.usertem, null);
            holder = new ViewHolder();
            holder.usernameHomepage = (TextView) convertView
                    .findViewById(R.id.userName);
            holder.statusHomepage = (TextView) convertView
                    .findViewById(R.id.userEmail);
            holder.proPic = (ParseImageView) convertView
                    .findViewById(R.id.medopic);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ParseUser statusObject = (ParseUser) mStatus.get(position);

        // title
        String username = statusObject.getString("name");
        holder.usernameHomepage.setText(username);

        // content
        String status = statusObject.getEmail();
        holder.statusHomepage.setText(status);
        ParseFile picc = statusObject.getParseFile("profile_picture");
        holder.proPic.setParseFile(picc);
        holder.proPic.loadInBackground();

        return convertView;
    }

    public static class ViewHolder {
        TextView usernameHomepage;
        TextView statusHomepage;
        ParseImageView proPic;

    }
}

但我遇到的问题是我想听点击项目点击,其中我的活动扩展了简单活动而非List活动这是我的活动

public class UsersList extends Activity {
private ParseQueryAdapter<ParseObject> mainAdapter;
protected List<ParseUser> mUsers;
ListView listhope;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users_list);
    listhope = (ListView) findViewById(R.id.userlistlist);
    ParseQuery<ParseUser> query = new ParseQuery<ParseUser>("_User");
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> list, ParseException e) {
            if (e == null) {
                mUsers = list;

                StatusAdapter adapter = new StatusAdapter(listhope.getContext(), mUsers);
                listhope.setAdapter(adapter);
            } else {

            }

        }
    });

}
[...]

请快速帮助

1 个答案:

答案 0 :(得分:1)

在onCreate中写

listhope.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


  //position is the row on which the user has clicked


     ParseUser parseUser=mUsers.get(position);
      //now you have the objext parseUser and this objects is what you needed.


        }


    });