问题描述
我有一个ListView由code下面显示了CustomAdapter来填充数据。
1)在ListView行是可以点击的onItemClick我出现两个按钮夺回/审查。
2)。当我点击其他行这是在previous行应隐藏可见的按钮。
3)。当我滚动列表中的所有按钮都看不见再次,这是不应该的。
我已经实现了点没有。 1本code,但我怎么能达到2,3。我怎么能修改适配器 getView()
方法或 onItemClick()
使事情工作正确的。
//初始化列表视图与适配器
AttempListView.setAdapter(新AttemptedExcerciseAdapter(mAttem precord,AttemptedExercise.this));
//不同的适配器类放置值
公共类AttemptedExcerciseAdapter延伸BaseAdapter {
的HashMap<整数,AttemptedRecord> mHashMap;
上下文mContext;
的LinearLayout mLLButton;公共AttemptedExcerciseAdapter(){
// TODO自动生成构造函数存根
}公共AttemptedExcerciseAdapter(HashMap的<整数,AttemptedRecord> mAttem precord,上下文mContext){
this.mHashMap = mAttem precord;
this.mContext = mContext;
}
@覆盖
公众诠释的getCount(){
返回mHashMap.size();
}
@覆盖
公共对象的getItem(INT为arg0){
返回null;
}
@覆盖
众长getItemId(INT为arg0){
返回0;
}
@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中ARG2){
如果(convertView == NULL){
@燮pressWarnings(静态访问)
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); 按钮ReviewButton =(按钮)convertView.findViewById(R.id.ReviewButton);
按钮RetakeButton =(按钮)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(数据值是...+职位+ mHashMap.get(位置+ 1).getChapter_name()); attempChapter_name.setText(mHashMap.get(位置+1).getChapter_name());
attemptQues.setText(+ mHashMap.get(位置+ 1).getTimePerQues()+秒/疑问句);
attemptSR.setText(+ mHashMap.get(位置+1).getSuccess_rate()+%); 返回convertView;
}
}
//项目点击监听器列表视图
公共类ExcerciseItemClickListener实现OnItemClickListener {
的ArrayList<整数GT; rowNo =新的ArrayList<整数GT;(); @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){ 的System.out.println(点击工作...+ ARG2); arg1.findViewById(R.id.LLButton).setVisibility(View.INVISIBLE); rowNo.clear(); rowNo.add(ARG2); 如果(rowNo.size()== 1)
{
AttemptedRecord mRecordExcerciseItem = mAttem precord.get(ARG2 + 1); 最终诠释chapter_id = mRecordExcerciseItem.getChapter_id();
最终诠释test_id = mRecordExcerciseItem.getTest_id();
最终诠释subject_id = mRecordExcerciseItem.getSubject_id(); 的System.out.println(企图列表的大小是......+ mAttem precord.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); 按钮复习=(按钮)arg1.findViewById(R.id.ReviewButton);
按钮重拍=(按钮)arg1.findViewById(R.id.RetakeButton); review.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){ DBHelper mDbHelper =新DBHelper(AttemptedExercise.this);
mDbHelper.createOrOpenDatabase(仪表盘);
光标chpater_exercise_Cursor = mDbHelper.rawQuery(选择practice_test_summary current_test_id那里test_id =+ test_id +的顺序由test_datetime递减限制1); chpater_exercise_Cursor.moveToFirst(); 长current_test_id = chpater_exercise_Cursor.getLong(0);
chpater_exercise_Cursor.close(); 的System.out.println(当前测试的id值是......+ current_test_id); 意图reviewIntent =新意图(AttemptedExercise.this,PracticeReview.class);
reviewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
如果(current_test_id大于0){
reviewIntent.putExtra(current_test_id,current_test_id);
startActivity(reviewIntent);
}
}
}); retake.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
的System.out.println(测试值id试验开始时是...+ test_id);
Toast.makeText(AttemptedExercise.thischapter_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 列表的
ClickListeners
code>像这样
convertView.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
curruntButtonClickPosition =位置;
//可见你在这里按钮
notifyDataSetChanged();
}
});
和 getView
如果(curruntButtonClickPosition =位置)
// mLLButton可见
其他
// mLLButton无形
添加 curruntButtonClickPosition
在 AttemptedExcerciseAdapter
类地球仪变量和-1初始化。
I have a listview to populate data with a CustomAdapter as shown below by code.
1.) The listview rows are clickable and onItemClick I show up two buttons retake/review.
2.) When I click on other row the buttons which were visible on the previous row should hide.
3.) When I scroll the list all buttons are again invisible , that should not happen.
I have achieved the point no. 1 by this code , but how could I achieve 2,3. How could I modify getView()
method of adapter or onItemClick()
so that things work proper.
//Initialized listview with adapter
AttempListView.setAdapter(new AttemptedExcerciseAdapter(mAttempRecord,AttemptedExercise.this));
//A different adapter class to place values
public class AttemptedExcerciseAdapter extends 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;
}
}
//Item click listener for listview
public class ExcerciseItemClickListener implements 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);
}
});
}
}
}
if you can mange to add ClickListeners
of the list in getView
like this
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
curruntButtonClickPosition=position;
//Visible you button here
notifyDataSetChanged();
}
});
and in getView
if(curruntButtonClickPosition=position)
//mLLButton visible
else
//mLLButton invisible
add curruntButtonClickPosition
globle variable in AttemptedExcerciseAdapter
class and init with -1.
这篇关于安卓:查看不列表视图/适配器再次现身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!