嵌套ListViews内的按钮上的OnClickListener

时间:2014-05-22 11:25:19

标签: android listview onclicklistener android-cursoradapter

我正在尝试在Android中创建包含评论的新闻Feed。我正在使用CursorAdapterListView来实现新闻Feed和每个项目的评论列表。但是,我不知道在按下相应的EditText时如何从正确的Button获取文字。

我唯一确定的是我做错了。

 W/View﹕ requestLayout() improperly called by android.widget.ListView{41e85210 V.ED.VC. ......I. 9,71-711,221 #7f07004a app:id/comments} during layout: running second layout pass

注意:下面的代码显然不起作用,因为我只对所有行使用一个变量commentText

database.getComments()会返回Cursor database.addUploadComment()只是执行一个sql插件来保存新创建的注释。

public class TimelineCursorAdapter extends CursorAdapter {
    public TimelineCursorAdapter(Context context, Cursor cursor, boolean autoRequery) {
        super(context, cursor, autoRequery);
    }

    private TimelineCommentCursorAdapter timelineCommentCursorAdapter;

    @Override
    public View newView(Context context, final Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View returnView = inflater.inflate(R.layout.timeline_item, parent, false);
        Log.v(this.getClass().getSimpleName(), "newView called");

        // get comments
        Database database = Database.getDatabase(context);
        timelineCommentCursorAdapter = new TimelineCommentCursorAdapter(context, database.getComments(cursor.getInt(cursor.getColumnIndex("_id"))), true);

        ListView listView = (ListView) returnView.findViewById(R.id.comments);
        listView.setAdapter(timelineCommentCursorAdapter);

        Log.d(this.getClass().getSimpleName(), "adapter: " + listView.getAdapter().toString());

        returnView.findViewById(R.id.commentText).setTag(cursor.getPosition());

        return returnView;
    }

    EditText commentText;

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Log.v(this.getClass().getSimpleName(), "bindView called");
        // here we are setting our data
        // that means, take the data from the cursor and put it in views

        TextView username = (TextView) view.findViewById(R.id.username);
        String usernameString = cursor.getString(cursor.getColumnIndex("username"));
        if (usernameString != null) {
            username.setText(usernameString);
        }

        TextView status = (TextView) view.findViewById(R.id.status);
        String statusString = cursor.getString(cursor.getColumnIndex("bodytext"));
        if (statusString != null) {
            status.setText(statusString);
        }

        TextView timestamp = (TextView) view.findViewById(R.id.timestamp);
        String timestampString = cursor.getString(cursor.getColumnIndex("datetime"));
        if (timestampString != null) {
            timestamp.setText(timestampString);
        }

        // update the comments section as well
        Database database = Database.getDatabase(context);
        timelineCommentCursorAdapter.changeCursor(database.getComments(cursor.getInt(cursor.getColumnIndex("_id"))));

        Log.v(TimelineCursorAdapter.class.getSimpleName(), "commentButton getView");
        Button commentButton = (Button) view.findViewById(R.id.commentButton);
        commentText = (EditText) view.findViewById(R.id.commentText);
        commentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.v(TimelineCursorAdapter.class.getSimpleName(), "commentButton onClick");

                Log.e("POTATO", view.toString());

                Database database = Database.getDatabase(view.getContext());
                database.addUploadComment(commentText.getText().toString(), (Integer) commentText.getTag());

                commentText.setText("");
            }
        });

    }

询问您是否需要更多代码。

0 个答案:

没有答案