我正在开发小型益智Android应用程序,在数据库上有102个谜题。我将Adapter
与TextViews
和Button
用于ListView
。
在适配器中我有TextView
作为显示谜题的puzzl,回答为TextView
以显示答案(默认情况下,它在xml上显示GONE
)。当用户按下answer_button TextView
时,答案显示答案(answer.setVisibility(View.VISIBLE);
)。
一切正常,但我发现有问题。如果我按下第一个答案的按钮以查看ListView
,它显示第一个答案,但从下面也打开其他答案,例如5,11,16等。如果我按下第二个回答的按钮,则显示第二个回答和第7个,第12个回答,依此类推。一切都在开放。
我想让按钮必须只显示用户选择的答案。请帮帮我,告诉我我在适配器或ListView
上的错误在哪里?非常感谢你。
public class PuzzleAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
List<Puzzles> puzzles;
public PuzzleAdapter(Context context1, List<Puzzles> p1){
this.context=context1;
this.puzzles =p1;
inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return puzzles.size();
}
@Override
public Object getItem(int i) {
return puzzles.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
view = inflater.inflate(R.layout.puzzle_adapter, null);
}
final Puzzles puzz = this.puzzles.get(i);
TextView puzzl=(TextView)view.findViewById(R.id.puzzl);
puzzl.setText(puzz.getZagad());
final TextView answer=(TextView)view.findViewById(R.id.answer);
answer.setText(puzz.getOtveti());
final Button answer_button = (Button)view.findViewById(R.id.answer_button);
answer_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
answer.setVisibility(View.VISIBLE);
}
});
return view;
}
}
我的主要课程,我称之为适配器
public class MainActivity extends AppCompatActivity {
private DbHelper dbHelper;
List<Puzzles> puzzles = new ArrayList<Puzzles>();
ListView listView;
PuzzleAdapter puzzleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DbHelper(this);
dbHelper.createDB();
puzzles = dbHelper.zagadki(this);
puzzleAdapter = new PuzzleAdapter(this, puzzles);
listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(puzzleAdapter);
}
}
这里我从数据库中调用
// public List<Puzzles> zagadki(int id){
public List<Puzzles> zagadki(Context context){
SQLiteDatabase db = this.getReadableDatabase();
List<Puzzles> puzzles = new ArrayList<Puzzles>();
// String s = "select * from zagad where _id=" + id;
String s = "select * from zagad";
Cursor cursor = db.rawQuery(s, null);
if (cursor.moveToFirst()){
do {
Puzzles puzzle = new Puzzles();
puzzle.set_id(cursor.getInt(0));
puzzle.setZagad(cursor.getString(1));
puzzle.setOtveti(cursor.getString(2));
puzzles.add(puzzle);
}
while (cursor.moveToNext());
}
db.close();
return puzzles;
}
答案 0 :(得分:0)
由于您已直接查看并维护,因此会出现此问题,
所以使用holder类然后管理它检查我的下面的代码,
public class ViewHolder {
TextView puzzl;
TextView answer;
Button answer_button;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder= null;
if (row == null)
{
row = inflater.inflate(R.layout.puzzle_adapter, null);
holder = new ViewHolder();
holder.puzzl=(TextView)view.findViewById(R.id.puzzl);
holder.answer=(TextView)view.findViewById(R.id.answer);
holder.answer_button = (Button)view.findViewById(R.id.answer_button);
row.setTag(holder);
}
else
{
holder = (AppInfoHolder)row.getTag();
}
holder.puzzl.setText(puzz.getZagad());
holder.answer.setText(puzz.getOtveti());
holder.answer_button.setTag(holder);
holder.answer_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ViewHolder selectedHolder = (View Holder) view.getTag();
selectedHolder.answer.setVisibility(View.VISIBLE);
}
});
return row;
}