1

我有一个列表视图来使用 CustomAdapter 填充数据,如下面的代码所示。

1.) listview 行是可点击的,并且 onItemClick 我显示了两个按钮 retake/review。

2.)当我点击其他行时,前一行可见的按钮应该隐藏。

3.)当我滚动列表时,所有按钮都再次不可见,这不应该发生。

我已经达到了没有。1 通过这段代码,但我怎么能达到 2,3。我如何修改getView()适配器的方法或onItemClick()使事情正常工作。

//使用适配器初始化列表视图

AttempListView.setAdapter(new AttemptedExcerciseAdapter(mAttempRecord,AttemptedExercise.this));

//一个不同的适配器类来放置值

公共类 AttemptedExcerciseAdapter 扩展 BaseAdapter{

HashMap<Integer, AttemptedRecord> mHashMap;
Context mContext;
LinearLayout mLLButton;

public AttemptedExcerciseAdapter() {
    // TODO Auto-generated constructor stub
}

public AttemptedExcerciseAdapter(HashMap<Integer, AttemptedRecord> mAttempRecord,Context mContext) {
    this.mHashMap = mAttempRecord;
    this.mContext=mContext;
}
@Override
public int getCount() {
    return mHashMap.size();
}
@Override
public Object getItem(int arg0) {
    return null;
}
@Override
public long getItemId(int arg0) {
    return 0;
}


@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
    if (convertView == null) {
        @SuppressWarnings("static-access")
        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(AttemptedExercise.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.exerciselistlayout, null);
    }

    TextView attempChapter_name = (TextView) convertView.findViewById(R.id.TVchapterexercisechapterName);
    TextView attemptQues = (TextView) convertView.findViewById(R.id.tvexercisesuccessrate);
    TextView attemptSR = (TextView) convertView.findViewById(R.id.tvexerciseperquestiontime);

    Button ReviewButton = (Button) convertView.findViewById(R.id.ReviewButton);
    Button RetakeButton = (Button) convertView.findViewById(R.id.RetakeButton);
    LinearLayout mLLtext = (LinearLayout) convertView.findViewById(R.id.LLText);
    mLLButton = (LinearLayout) convertView.findViewById(R.id.LLButton);

    mLLButton.setVisibility(View.INVISIBLE);
    mLLtext.setVisibility(View.VISIBLE);
    System.out.println("data value is..."+position+mHashMap.get(position + 1).getChapter_name());

    attempChapter_name.setText(mHashMap.get(position+1).getChapter_name());
    attemptQues.setText(" " + mHashMap.get(position+1).getTimePerQues() + " sec/ques");
    attemptSR.setText(" " + mHashMap.get(position+1).getSuccess_rate() + " %");

    return convertView;
}

}

//listview的item点击监听

公共类 ExcerciseItemClickListener 实现 OnItemClickListener {

    ArrayList<Integer> rowNo=new ArrayList<Integer>();

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        System.out.println("click working..."+arg2);

        arg1.findViewById(R.id.LLButton).setVisibility(View.INVISIBLE);

        rowNo.clear();

        rowNo.add(arg2);

        if(rowNo.size()==1)
        {   
        AttemptedRecord mRecordExcerciseItem = mAttempRecord.get(arg2 + 1);

        final int chapter_id = mRecordExcerciseItem.getChapter_id();
        final int test_id = mRecordExcerciseItem.getTest_id();
        final int subject_id = mRecordExcerciseItem.getSubject_id();

        System.out.println("attempted  list size is..."+mAttempRecord.size());

            arg1.findViewById(R.id.LLText).setVisibility(View.INVISIBLE);
            arg1.findViewById(R.id.LLTake).setVisibility(View.INVISIBLE);
            arg1.findViewById(R.id.LLButton).setVisibility(View.VISIBLE);

            Button review=(Button) arg1.findViewById(R.id.ReviewButton);
            Button retake=(Button) arg1.findViewById(R.id.RetakeButton);

            review.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DBHelper mDbHelper = new DBHelper(AttemptedExercise.this);
                    mDbHelper.createOrOpenDatabase("Dashboard");
                    Cursor chpater_exercise_Cursor = mDbHelper.rawQuery("select current_test_id from practice_test_summary where test_id="+test_id+" order by test_datetime desc limit 1");

                    chpater_exercise_Cursor.moveToFirst();

                    Long current_test_id =chpater_exercise_Cursor.getLong(0);
                    chpater_exercise_Cursor.close();

                    System.out.println("value of current test id is...."+current_test_id);

                    Intent reviewIntent = new Intent(AttemptedExercise.this, PracticeReview.class);
                    reviewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                    if (current_test_id > 0) {
                        reviewIntent.putExtra("current_test_id", current_test_id);
                        startActivity(reviewIntent);
                    }
                }
            });

            retake.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("test id value when test starts is... "+test_id);
                    Toast.makeText(AttemptedExercise.this, "chapter_id" + chapter_id + " course_id"  + " test_id" + test_id + " subject_id" + subject_id, Toast.LENGTH_LONG).show();
                    StartTest(4, subject_id, chapter_id, test_id);
                }
            });
        }
    }
    }
4

2 回答 2

4

在 getView 中,您隐藏了按钮和文本,这就是滚动时视图消失的原因。</p>

mLLButton.setVisibility(View.INVISIBLE);
mLLtext.setVisibility(View.VISIBLE);

在您的点击监听器中,您应该记录所选行的位置,而在 getView 中,您需要根据视图的位置设置视图的可见性状态。

于 2012-12-14T07:34:02.490 回答
2

如果您可以像这样添加ClickListeners列表getView

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            curruntButtonClickPosition=position;
                            //Visible you button here
                            notifyDataSetChanged();
        }
    });

并且在getView

 if(curruntButtonClickPosition=position)
   //mLLButton visible
else
  //mLLButton  invisible

在类中添加curruntButtonClickPosition全局变量AttemptedExcerciseAdapter并使用-1初始化。

于 2012-12-14T07:41:28.133 回答