我想製作一款需要你猜數字的遊戲。我讓用戶選擇他們想要的數字號碼。如何根據您在哪些活動中隱藏CustomAdapter中的元素?
我的自定義適配器是最初爲3位數字構建的,當用戶選擇2位數字時,一切都正常,但是運行應用程序時digit_3總是0,我只想隱藏它,我該怎麼做?
繼承人的適配器:
package com.apphorizon.guessthenumber;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<List> {
public ListAdapter(Context context, ArrayList<List> arrayList) {
super(context, 0, arrayList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
List currentItem = getItem(position);
TextView num1View = (TextView)listItemView.findViewById(R.id.num_1_view);
num1View.setText(String.valueOf(currentItem.getNum1()));
TextView num2View = (TextView)listItemView.findViewById(R.id.num_2_view);
num2View.setText(String.valueOf(currentItem.getNum2()));
TextView num3View = (TextView) listItemView.findViewById(R.id.num_3_view);
num3View.setText(String.valueOf(currentItem.getNum3()));
TextView correctPositionView = (TextView)listItemView.findViewById(R.id.correct_position_view);
correctPositionView.setText(String.valueOf(currentItem.getCorrectPosition()));
TextView wrongPositionView = (TextView)listItemView.findViewById(R.id.wrong_position_view);
wrongPositionView.setText(String.valueOf(currentItem.getWrongPositionCorrectNumber()));
return listItemView;
}
}
而這裏的List類
package com.apphorizon.guessthenumber;
public class List {
private int mNum1;
private int mNum2;
private int mNum3;
private int mCorrectPosition;
private int mWrongPositionCorrectNumber;
public List(int num1, int num2, int num3, int correctPosition, int wrongPositionCorrectNumber){
mNum1 = num1;
mNum2 = num2;
mNum3 = num3;
mCorrectPosition = correctPosition;
mWrongPositionCorrectNumber = wrongPositionCorrectNumber;
}
public List(int num1, int num2, int correctPosition, int wrongPositionCorrectNumber){
mNum1 = num1;
mNum2 = num2;
mCorrectPosition = correctPosition;
mWrongPositionCorrectNumber = wrongPositionCorrectNumber;
}
public int getNum1(){
return mNum1;
}
public int getNum2(){
return mNum2;
}
public int getNum3(){
return mNum3;
}
public int getCorrectPosition(){
return mCorrectPosition;
}
public int getWrongPositionCorrectNumber(){
return mWrongPositionCorrectNumber;
}
}
該應用程序目前只定制的2和3位數字,但增加更多!
是否允許在任何地方使用? – EdmDroid
是的,0是2/3數字中可能的數字。該示例本身在其他地方使用了0。 「01」是正確的號碼 – Abhijeet