[英]Send a variable from one class in a parameter to another class. Simple error
我想發送給另一個類的變量是int position。 它位於InstantiateItem方法的參數中。 這基本上表明了滑動布局的位置。
現在,變量位置僅在onClick方法中可用。 一旦在方法之外,它就始終為0。將示例放入代碼中。
public class Csa{
private int getposition;
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
imageView = (ImageView) item_view.findViewById(R.id.image_view);
TextView textView = (TextView) item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText(names[position]);
container.addView(item_view);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(position){
getposition = position;
case 1:
System.out.println(position); //Outputs 1. Correct.
Log.d("Exercise", "Deadlift!");
Intent intent1 = new Intent(CustomSwipeAdapter.this.ctx, AnalysisPage.class);
CustomSwipeAdapter.this.ctx.startActivity(intent1);
break;
case 2:
getposition = position;
System.out.println(position); // Outputs 2. Correct.
System.out.println(getposition()); // Outputs 2. Correct.
Log.d("Exercise", "Squats");
Intent intent2 = new Intent(CustomSwipeAdapter.this.ctx, AnalysisPage.class);
CustomSwipeAdapter.this.ctx.startActivity(intent2);
break;
}
}
});
return item_view;
}
public int getposition() {
return getposition;
}
public class AnotherClass{
Csa chooser = new Csa(this);
private int command = chooser.getPosition();//Debugged. This is 0 always 0. Should be 1/2. Wrong.
public static void main(String args[]){
System.out.println(command);// Always prints out 0.
機箱/開關內的變量始終正確。 但是,如果我正在創建全局變量,或者甚至將位置變量分配給另一個變量,則輸出始終為0。 (請參見代碼示例)。
最終,我希望position變量位於另一個類中,但輸出始終為0。我嘗試了Getter / Setter方法,但它們仍然不起作用。
匿名類只能訪問封閉類的最終變量。 為了解決此問題,請將以下類(在單獨的文件中)添加到您的項目中:
public class ValueHolder<T> {
private T value = null;
public ValueHolder() {
setValue(null);
}
public ValueHolder(final T newValue) {
setValue(newValue);
}
public T getValue() {
return value;
}
public void setValue(final T newValue) {
value = newValue;
}
}
然后在代碼的頂部,將int x = 0替換為:
final ValueHolder<Integer> xHolder = new ValueHolder<Integer>(0);
在您的匿名班級的每個位置:
case 0:
System.out.println(position); // Prints out 0 Correct
xHolder.setValue(position);
...
case 1:
System.out.println(position); // Prints out 1 Correct
xHolder.setValue(position);
...
然后,當您想使用存儲在持有人中的值時:
int storedX = xHolder.getValue();
編輯:添加一個觀察者
創建以下界面:
public interface OptionObserver {
void notifyOptionChange(final int option);
}
然后使您需要將值發送到的另一個類,實現此接口。
然后,您的CustomWipeAdapter應該具有一個屬性和一個setter:
OptionObserver optionObserver = null;
public void setOptionObserver(OptionObserver observer) {
optionObserver = observer;
}
然后在初始化期間,您調用setOptionObserver()
,將對其他類的實例的引用傳遞給它。
在您的CustomWipeAdapter中:
case 1:
System.out.println(position); //Outputs 1. Correct.
if(optionObserver != null) {
optionObserver.notifyOptionChange(position);
}
...
實際上,您正在做的是在偵聽器中聲明一個Java匿名類,實現將被調用的onClick
方法。 此封閉類的外部類具有不同的范圍(它們與繼承或友誼無關)。
現在, onClick
方法將被異步調用,因為onClick
是一個回調,即在發生其包含其類實例的事件注冊時調用的方法。
當事件發生時,回調將更改x的值,因為x的值在其作用域旁邊可見,但不在事件發生之前發生,並且不采用同步方式。
在一個簡單的示例中這樣想:
-你好傑克,我拿着一個空的咖啡杯。 (您在外部類中聲明的x var)
-Hello Joe,當咖啡壺准備就緒時,我希望您填充咖啡杯(發生事件時,更改x的值)。
-你的杯子現在裝什么? (在活動開始前打印x)
-什么都沒有(0,邏輯)。
-不,不,錯!
-嘿,但是咖啡壺還沒做完。 咖啡機准備好時,我將其裝滿。
您應該做的是始終在事件發生后處理數據。 我建議您不要使用outter類的屬性,而是在outter類中創建一個接受參數(要使用的變量)和其余代碼的方法,並確保將調用此方法在您的回調中。 例如:
public class CustomSwipeAdapter extends PagerAdapter {
//Rest of code....
public void NotifyWhenClickOccurs(int x){
System.out.println(x);
//Use x here.
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(position){
case 0:
System.out.println(position); // Prints out 0 Correct
//Notify your outter class instance.
Notify(position);
Log.d("Exercise", "Dumbell Press");
Intent intent0 = new Intent(CustomSwipeAdapter.this.ctx, AnalysisPage.class);
CustomSwipeAdapter.this.ctx.startActivity(intent0);
break;
//Rest of code.....
}
}
}
這類似於說:
-(喬)傑克,當我加滿杯子時,我會告訴你,以便你可以喝咖啡。
觀察者設計模式是一種很好的標准化方法,在這里您正在做類似的事情,將外部類以更草擬和更通用的方式視為“觀察者”。
您應該考慮的另一件事是您在回調和通知方法內部做的事情(它們使用什么線程?它們可以從那里對GUI / Dispatch線程進行更改嗎?),但這超出了問題的范圍,僅是一些思考的食物。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.