我有像Car,Home适配器这样的特定类,它们都扩展到Adapter类,我将它们的实例传递给setAdapterForList方法,但是,所有类都有自己的特殊方法,比如buyCar,getHome。
我想要做的是,通过在var valArray = new[] { "Val0", "Val1", "Val2", "Val3" };
方法
var valArray = new[] { "Val0", "Val1", "Val2", "Val3" };
var source = "hello{1}from{2}my{3}world";
var substr = source;
string pattern = $"{Regex.Escape(start)}{".+?"}{Regex.Escape(end)}";
foreach (Match m in Regex.Matches(source, pattern))
{
var value = m.Groups[0].Value;
var ind = Convert.ToInt32(m.Groups[0].Value.TrimStart(start.ToCharArray()).TrimEnd(end.ToCharArray()));
substr = substr.Replace(value, valArray[ind]);
}
return substr;
这就是我想做的事,
setAdapterForList
答案 0 :(得分:1)
我不知道是否可以自动完成。 但手动你可以这样做:
public class GameFragment extends Fragment {
private Theme currentTheme;
private Circle currentCircle;
private int currentOrganisationId;
private List<Card> circleCards;
private List<ImageButton> circleButtons;
private int viewHeight;
private int viewWidth;
private int marginCard;
private int[] tableStarts;
private RelativeLayout background;
private ViewTreeObserver vto;
public GameFragment() {
circleCards = new ArrayList<>();
circleButtons = new ArrayList<>();
tableStarts = new int[9];
}
//...
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_game, container, false);
...
background = setBackground(rootView);
circleCards = currentCircle.getCards();
return rootView;
}
//...
private void drawCards() {
circleButtons = new ArrayList<>();
for (Card card : circleCards) {
ImageButton myImageButton = new ImageButton(this.getContext()); //generate ImageButton
myImageButton.setId(card.getCardId()); //Set Id of button
myImageButton.setBackgroundResource(R.drawable.circle_card_icon);
//set random color to shape
...
}
}
}
答案 1 :(得分:0)
您可以使用instanceof
来检查它是一个还是另一个
public void setAdapterForList(Adapter adapter) {
if (adapter instanceof Car) {
Car c = (Car)adapter;
// stuff
return;
}
if (adapter instanceof Home) {
Home h = (Home)adapter;
// stuff
return;
}
// and so on...
}