2012-12-14 32 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

您應該擁有適配器的引用,然後您只需調用它。 –