Android:View在Listview / Adapter中不再显示?

时间:2012-12-14 07:24:31

标签: android listview baseadapter

我有一个listview用CustomAdapter填充数据,如下面的代码所示。

1。)列表视图行是可点击的,onItemClick我显示两个按钮重新/回顾。

2。)当我点击其他行时,上一行显示的按钮应隐藏。

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

我已经达到了这一点。 1由此代码,但我怎么能实现2,3。如何修改适配器的getView()方法或onItemClick(),以便工作正常。

//带适配器的初始化列表视图

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

//用于放置值的不同适配器类

public class 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;
}

}

//列表视图的项目点击监听器

公共类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);
                }
            });
        }
    }
    }

2 个答案:

答案 0 :(得分:4)

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

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

在你的点击监听器中,你应该记录所选行的位置,在getView中,你需要根据视图的位置设置视图的粘度状态。

答案 1 :(得分: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中添加init。