2012-12-14 35 views
1

我有一个listview来填充数据与CustomAdapter,如下面的代码所示。Android:View在Listview/Adapter中不再显示?

1.)listview行是可点击的和onItemClick我显示了两个按钮重拍/审查。

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

}

//项目点击李的侦听器stview

公共类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

,如果你能疥癣添加列表ClickListenersgetView这样

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初始化。

+1

MAN你真是太棒了....这工作..对我... upvote和接受。 – Prateek

4

在getView,你隐藏的按钮和文本,这就是为什么当你滚动视图dispears。

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

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

+0

我这样做是明智的,因为如果我不这样做,我将如何隐藏其他按钮,因为每次调用getView方法时没有任何固定的位置值,即它根据滚动取得位置。 – Prateek

+0

如果要更新ListView中的视图,正确的方法是修改适配器中的数据并调用notifyDataSetChanged。然后getView将被调用,一切都很好。 –

+0

您应该拥有适配器的引用,然后您只需调用它。 –