Android:View 不再出现在 Listview/Adapter 中?

标签 android listview baseadapter

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

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

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

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

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

//用适配器初始化 ListView

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

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

公共(public)类 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点击监听

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

最佳答案

在getView中,你隐藏了按钮和文本,这就是为什么滚动时 View 消失的原因。

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

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

关于Android:View 不再出现在 Listview/Adapter 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13874376/

相关文章:

android - 从 ListView 中删除动态添加的布局扩展基本适配器

Android WebView navigator.getCurrentPosition 问题

listview - TListView 检测 ESC 或未更改的编辑

android - 如何切换 "Use Physical Keyboard"。

Android KeyBoard 不显示在 AlertDialog 中

Qt QML Listview positionViewAtIndex 不工作

android - 如何在 android 中使用基本适配器时过滤列表项?

android - 如何在 android 中关闭搜索 View 后重置 ListView ?

c# - 实现 Parse Unity 插件的应用程序在 Android 设备上崩溃但在编辑器中运行良好

android - 将列表添加到 Cloud Firebase - Android