如何获得微调的ListView里面工作在Android的? [英] How to get Spinner inside ListView work in Android?

查看:203
本文介绍了如何获得微调的ListView里面工作在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序中,我需要一个ListView的行具有一个TextView,2 CheckBox和一个微调。

不过,我遇到的问题与微调的onItemSelected(),因为它得到每次显示每行时间调用。在这种方法中,我更新数据库记录所选择的选项,但由于Android会自动调用它,每一次的项目得到重置,因为Android的与位置0调用它,这是在数据库中更新的价值。

我已经阅读了很多有关问题与链接 onItemSelected()和一些黑客,但他们是没有一个ListView使用。这里的任何点?

我曾尝试在其中的位置实际上是显示,使工作列表追踪,但事实并非如此。我想,这是因为在Android的回收,导致该故障的排除方法被调用的纱厂已经显示出!

所以问题是:我怎样才能显示微调时differenciate因为从Android呼叫的用户选择为 onItemSelected() A实际通话

下面是我的适配器的code扩展SimpleCursorAdapter。

感谢你这么多提前。

 公共ParticipationAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []到){
    超(背景下,布局,C,从,到);
    mActivity =(活动)范围内;
    ParticipationComment.ParticipationCommentManager commentManager =新ParticipationComment.ParticipationCommentManager(mActivity);
    mParticipationCommentsCursor = commentManager.get();
    mActivity.startManagingCursor(mParticipationCommentsCursor);
    commentManager.detach();
    mPositionsOfCursorIds = getPositionsOfCursorIds(mParticipationCommentsCursor);
    mSpinnerPositionsDisplayed =新的ArrayList<整数GT;();
}@覆盖
公共查看getView(最终诠释participationPosition,查看convertView,父母的ViewGroup){
    最终光标participationsCursor = getCursor();
    mActivity.startManagingCursor(participationsCursor);
    participationsCursor.moveToPosition(participationPosition);
    查看participationRow;
    如果(convertView == NULL){
        participationRow = LayoutInflater.from(mActivity).inflate(R.layout.participation_row_student,NULL);
    }其他{
        mSpinnerPositionsDisplayed.remove((整数)convertView.getTag());
        participationRow = convertView;
    }
    participationRow.setTag(participationPosition);
    微调commentSpinner =(微调)participationRow.findViewById(R.id.participation_comment_id_spinner);
    SimpleCursorAdapter commentSpinnerAdapter =新SimpleCursorAdapter(
            mActivity,
            android.R.layout.simple_spinner_item,
            mParticipationCommentsCursor,
            新的String [] {} DatabaseManager.NAME,
            新的INT [] {} android.R.id.text1
    );
    commentSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    commentSpinner.setAdapter(commentSpinnerAdapter);
    长participationCommentId = participationsCursor.getLong(participationsCursor.getColumnIndex(DatabaseManager.PARTICIPATION_COMMENT_ID));
    如果(participationCommentId!= 0){
        commentSpinner.setSelection(mPositionsOfCursorIds.get(participationCommentId));
    }
    commentSpinner.setOnItemSelectedListener(新OnItemSelectedListener(){
        @覆盖
        公共无效onItemSelected(适配器视图<>母公司,观景,INT位置,长的id){
            participationsCursor.moveToPosition(participationPosition);
            如果(!mSpinnerPositionsDisplayed.contains(participationPosition)){
                // Android的调用此方法的第一次显示微调,
                //从一个真实的用户点击区分我们检查,如果当前微调的位置
                //在ListView正在上映
                mSpinnerPositionsDisplayed.add(participationPosition);
            }其他{
                ParticipationComment participationComment =新ParticipationComment((光标)parent.getItemAtPosition(位置));
                Participation.ParticipationManager participationManager =新Participation.ParticipationManager(mActivity);
                参参=新的参与(participationsCursor);
                participation.setConnectionProfileParticipationCommentId(participationComment.getConnectionProfileId());
                participation.setParticipationCommentId(participationComment.getIdOpenErp());
                participation.setChanged(真);
                participationManager.update(参与);
                participationManager.detach();
            }
        }        @覆盖
        公共无效onNothingSelected(适配器视图<>母公司){
            // 不曾用过
        }
    });
    TextView的studentName =(TextView中)participationRow.findViewById(R.id.participation_student_name);
    studentName.setText(participationsCursor.getString(participationsCursor.getColumnIndex(DatabaseManager.NAME)));
    预计复选框present =(复选框)participationRow.findViewById(R.id.participation_expected_ present_value);
    expected$p$psent.setChecked(participationsCursor.getInt(participationsCursor.getColumnIndex(DatabaseManager.EXPECTED_$p$pSENT)) == 1);
    复选框present =(复选框)participationRow.findViewById(R.id.participation_ present_value);
    $p$psent.setChecked(participationsCursor.getInt(participationsCursor.getColumnIndex(DatabaseManager.$p$pSENT)) == 1);
    返回participationRow;
}


解决方案

一个更好的办法是使用AlertDialog变..喜欢的这个 ..和创造最初拥有第一选择作为其文本其变化基础上,AlertDialog的选择。

按钮

I am developing an app in which I need a ListView whose rows have a TextView, 2 CheckBox and a Spinner.

However, I am experiencing issues with onItemSelected() of the Spinner, as it gets called each time it is displayed for each row. In this method I am updating database records with the selected option, but as Android calls it automatically, every time the items get reset because Android calls it with position 0 and this is the value updated in the database.

I have read a lot of links about the issue with onItemSelected() and some hacks, but all of them are to use without a ListView. Any points here?

I have tried to track in a List which positions are actually displayed to make it work but it does not. I think it is because of the recycling in Android that causes the troubleshooting method get called for Spinners already shown!

So the point is: How can I differenciate a real call to onItemSelected() because of a user selection from the Android call when displaying the Spinner?

Here is the code of my adapter that extends SimpleCursorAdapter.

Thank you so much in advance.

public ParticipationAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    mActivity = (Activity)context;
    ParticipationComment.ParticipationCommentManager commentManager = new ParticipationComment.ParticipationCommentManager(mActivity);
    mParticipationCommentsCursor = commentManager.get();
    mActivity.startManagingCursor(mParticipationCommentsCursor);
    commentManager.detach();
    mPositionsOfCursorIds = getPositionsOfCursorIds(mParticipationCommentsCursor);
    mSpinnerPositionsDisplayed = new ArrayList<Integer>();
}

@Override
public View getView(final int participationPosition, View convertView, ViewGroup parent) {
    final Cursor participationsCursor = getCursor();
    mActivity.startManagingCursor(participationsCursor);
    participationsCursor.moveToPosition(participationPosition);
    View participationRow;
    if (convertView == null) {
        participationRow = LayoutInflater.from(mActivity).inflate(R.layout.participation_row_student, null);
    } else {
        mSpinnerPositionsDisplayed.remove((Integer)convertView.getTag());
        participationRow = convertView;
    }
    participationRow.setTag(participationPosition);
    Spinner commentSpinner = (Spinner)participationRow.findViewById(R.id.participation_comment_id_spinner);
    SimpleCursorAdapter commentSpinnerAdapter = new SimpleCursorAdapter(
            mActivity,
            android.R.layout.simple_spinner_item,
            mParticipationCommentsCursor,
            new String[] {DatabaseManager.NAME},
            new int[] {android.R.id.text1}
    );
    commentSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    commentSpinner.setAdapter(commentSpinnerAdapter);
    long participationCommentId = participationsCursor.getLong(participationsCursor.getColumnIndex(DatabaseManager.PARTICIPATION_COMMENT_ID));
    if (participationCommentId != 0) {
        commentSpinner.setSelection(mPositionsOfCursorIds.get(participationCommentId));
    }
    commentSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            participationsCursor.moveToPosition(participationPosition);
            if (!mSpinnerPositionsDisplayed.contains(participationPosition)) {
                // Android calls this method the first time a Spinner is displayed,
                // to differentiate from a real user click we check if the current Spinner's position
                // in the ListView is being shown
                mSpinnerPositionsDisplayed.add(participationPosition);
            } else {
                ParticipationComment participationComment = new ParticipationComment((Cursor)parent.getItemAtPosition(position));
                Participation.ParticipationManager participationManager = new Participation.ParticipationManager(mActivity);
                Participation participation = new Participation(participationsCursor);
                participation.setConnectionProfileParticipationCommentId(participationComment.getConnectionProfileId());
                participation.setParticipationCommentId(participationComment.getIdOpenErp());
                participation.setChanged(true);
                participationManager.update(participation);
                participationManager.detach();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // Not used
        }
    });
    TextView studentName = (TextView)participationRow.findViewById(R.id.participation_student_name);
    studentName.setText(participationsCursor.getString(participationsCursor.getColumnIndex(DatabaseManager.NAME)));
    CheckBox expectedPresent = (CheckBox)participationRow.findViewById(R.id.participation_expected_present_value);
    expectedPresent.setChecked(participationsCursor.getInt(participationsCursor.getColumnIndex(DatabaseManager.EXPECTED_PRESENT)) == 1);
    CheckBox present = (CheckBox)participationRow.findViewById(R.id.participation_present_value);
    present.setChecked(participationsCursor.getInt(participationsCursor.getColumnIndex(DatabaseManager.PRESENT)) == 1);
    return participationRow;
}

解决方案

A better way is to use a AlertDialog Variant.. like this.. and create a button which initially has the first selection as its Text and its changed based on the AlertDialog choice..

这篇关于如何获得微调的ListView里面工作在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆