gpt4 book ai didi

java - 我在有意发送整数数据时遇到一些麻烦。 (安卓)

转载 作者:行者123 更新时间:2023-12-02 02:30:05 26 4
gpt4 key购买 nike

我正在尝试使用 Intent 将整数数据从一个 Activity 发送到另一个类,但我的整数数据只是自行变为零。

所以我做了这个 Intent ,将有关用户想要执行多少时间间隔的数据发送到一个类,该类根据用户为回收器 View 选择的数量来创建 View 组/ View 持有者。问题是 int 本身会自动变成零,这导致回收者列表没有被创建。我尝试使用随机数创建列表,并且它有效,因此这部分代码没有任何问题。

另外,经过一些调试后,我将问题范围缩小到了 getintent 部分,因为在发送数据之前,数字是正确的,但在发送数据之后,数字总是变成零。


public class MainActivity extends AppCompatActivity {
Button decrementButton;
TextView intervalCountTextView;
Button incrementButton;
int intervalCount = 0;
Button intervalButtonSetter;
private int numberOfIntervals = 0;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



decrementButton = findViewById(R.id.decrementButton);
intervalCountTextView = findViewById(R.id.intervallCount);
incrementButton = findViewById(R.id.incrementButton);
intervalButtonSetter = findViewById(R.id.intervalButtonSetter);

decrementButton.setVisibility(View.INVISIBLE);

decrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intervalCount--;
intervalCountSetter();

}
});

// make sure you can't decrement past zero

incrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intervalCount++;
intervalCountSetter();
}
});

intervalButtonSetter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendDataToAdapter();
}
});
}



private void intervalCountSetter() {
String intervalCountString = Integer.toString(intervalCount);
intervalCountTextView.setText(intervalCountString);

if (intervalCount > 0) {
decrementButton.setVisibility(View.VISIBLE);
} else {
decrementButton.setVisibility(View.INVISIBLE);
}
}

private void sendDataToAdapter() {
String numberOfIntervalsString = intervalCountTextView.getText().toString();
numberOfIntervals= Integer.parseInt(numberOfIntervalsString);
setNumberOfIntervals(numberOfIntervals);
if (numberOfIntervals > 0) {
Log.d(TAG, "sendDataToAdapter: We have the numbers");
Log.d(TAG, "sendDataToAdapter: " + numberOfIntervals);
startTimeActivity();

} else {
Toast.makeText(this, "You have to have at least 1 interval.", Toast.LENGTH_LONG).show();
}

}

private void startTimeActivity() {
startActivity(new Intent(this, TimeActivity.class));
Log.d(TAG, "startTimeActivity: TimeActivity has been started" + numberOfIntervals);


}

public int getNumberOfIntervals() {
return numberOfIntervals;
}

public void setNumberOfIntervals(int numberOfIntervals) {
this.numberOfIntervals = numberOfIntervals;
}
}


public class TimeActivity extends AppCompatActivity {

public static final String TAG = TimeActivity.class.getSimpleName();
private MainActivity mainActivityObject = new MainActivity();
private int numberOfIntervals = mainActivityObject.getNumberOfIntervals();
private ArrayList<Integer> WTV = new ArrayList<>();
private ArrayList<Integer> WET = new ArrayList<>();
private ArrayList<Integer> RTV = new ArrayList<>();
private ArrayList<Integer> RET = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
Log.d(TAG, "onCreate: Started");



Toast.makeText(this, "" + numberOfIntervals, Toast.LENGTH_LONG).show();


//Intent intent = getIntent();
//numberOfIntervals = intent.getIntExtra("Interval Count", 0);
//Log.d(TAG, "" + numberOfIntervals);

initializeViews();






}

private void initializeViews() {
Log.d(TAG, "initializeViews: Preparing views");
//Make sure they can change through the R.strings


WTV.add(R.string.work_text_view);
WET.add(R.string.default_time_value);
RTV.add(R.string.rest_text_view);
RET.add(R.string.default_time_value);

initializeRecyclerView();
}

private void initializeRecyclerView() {
Log.d(TAG, "initializeRecyclerView: Initialize RecyclerView");
RecyclerView intervalRecyclerView = findViewById(R.id.intervalRecyclerView);
CustomAdapter adapter = new CustomAdapter(this, WTV, WET, RTV, RET);
intervalRecyclerView.setAdapter(adapter);
intervalRecyclerView.setLayoutManager(new LinearLayoutManager(this));

}
}


private static final String TAG = "CustomAdapter";

private ArrayList<Integer> mWorkTW = new ArrayList<>();
private ArrayList<Integer> mWorkET = new ArrayList<>();
private ArrayList<Integer> mRestTW = new ArrayList<>();
private ArrayList<Integer> mRestET = new ArrayList<>();
private Context mContext;
private MainActivity mainActivityObject = new MainActivity();
private int numberOfIntervals = mainActivityObject.getNumberOfIntervals();








public CustomAdapter( Context context , ArrayList<Integer> mWorkTW, ArrayList<Integer> mWorkET, ArrayList<Integer> mRestTW, ArrayList<Integer> mRestET) {

this.mWorkTW = mWorkTW;
this.mWorkET = mWorkET;
this.mRestTW = mRestTW;
this.mRestET = mRestET;
this.mContext = context;
Log.d(TAG, "CustomAdapter: " + numberOfIntervals);
}





@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View customView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.time_row, viewGroup,false);
ViewHolder holder = new ViewHolder(customView);
return holder;

}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onBindViewHolder: called");
viewHolder.workTextView.setText(R.string.work_text_view);
viewHolder.restTextView.setText(R.string.rest_text_view);
viewHolder.workEditText.setHint(R.string.default_time_value);
viewHolder.restEditText.setHint(R.string.default_time_value);
}

@Override
public int getItemCount() {
Log.d(TAG, "" + numberOfIntervals);
return numberOfIntervals;
}

public class ViewHolder extends RecyclerView.ViewHolder {

TextView workTextView;
EditText workEditText;
TextView restTextView;
EditText restEditText;
ConstraintLayout parentLayout;


public ViewHolder(@NonNull View itemView) {
super(itemView);
workTextView = itemView.findViewById(R.id.workTextView);
workEditText = itemView.findViewById(R.id.workEditText);
restTextView = itemView.findViewById(R.id.restTextView);
restEditText = itemView.findViewById(R.id.restEditText);
parentLayout = itemView.findViewById(R.id.parentLayout);



}
}







}


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/intervallCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.533" />

<Button
android:id="@+id/decrementButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="-"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/intervallCount"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="-" />

<Button
android:id="@+id/incrementButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="+"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/intervallCount"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/intervalButtonSetter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Set intervall"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/intervallCount" />

</android.support.constraint.ConstraintLayout>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TimeActivity">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/intervalRecyclerView">

</android.support.v7.widget.RecyclerView>


</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parentLayout">

<TextView
android:id="@+id/workTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/work_text_view"
android:textSize="24sp"
tools:layout_editor_absoluteX="31dp"
tools:layout_editor_absoluteY="8dp" />

<EditText
android:id="@+id/workEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:ems="10"
android:hint="@string/default_time_value"
android:inputType="number"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/restTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:text="@string/rest_text_view"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/restEditText"
app:layout_constraintHorizontal_bias="0.425"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/workEditText" />

<EditText
android:id="@+id/restEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:ems="10"
android:hint="@string/default_time_value"
android:inputType="number"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/workEditText" />


</android.support.constraint.ConstraintLayout>

我希望从 Activity 发送到类的 int 是相同的,并且不会自动更改为零

最佳答案

目前,您有一个 Intent 作为 sendDataToAdapter() 中的局部变量,它获取额外的间隔数。由于您没有对此 Intent 执行任何其他操作,因此一旦该方法完成,它将被垃圾回收,额外的内容将无法从该方法之外的任何地方访问。

startTimeActivity() 中,您实例化一个新的 Intent 来启动 TimeActivity。此Intent没有任何额外内容。但这是 Intent,其额外内容将传递给 TimeActivity

因此,为了将间隔数作为正确的 Intent 的额外内容,您可以向 startTimeActivity() 添加参数 numberOfIntervals(请注意,我使用字符串常量以避免因拼写错误引起的错误)

public static final String INTERVAL_COUNT = "INTERVAL_COUNT";

private void startTimeActivity(int numberOfIntervals) {
Intent intent = new Intent(this, TimeActivity.class);
intent.putExtra(INTERVAL_COUNT, numberOfIntervals)
startActivity(intent);
Log.d(TAG, "startTimeActivity: TimeActivity has been started" + numberOfIntervals);
}

由于您需要知道CustomAdapter中的间隔数量,因此可以在构造函数中添加另一个参数:

public CustomAdapter(Context context, ArrayList<Integer> mWorkTW, ArrayList<Integer> mWorkET, ArrayList<Integer> mRestTW, ArrayList<Integer> mRestET, int numberOfIntervals) {
this.mWorkTW = mWorkTW;
// ...
this.numberOfIntervals = numberOfIntervals;
Log.d(TAG, "CustomAdapter: " + numberOfIntervals);
}

TimeActivity中,您可以通过调用getIntent()来访问int值:

private int numberOfIntervals;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
Log.d(TAG, "onCreate: Started");
numberOfIntervals = getIntent().getIntExtra(INTERVAL_COUNT, 0);
Toast.makeText(this, "" + numberOfIntervals, Toast.LENGTH_LONG).show();
Log.d(TAG, "" + numberOfIntervals);
initializeViews();
}

最后,您可以在 TimeActivity 中初始化 RecyclerView,如下所示:

private void initializeRecyclerView() {
Log.d(TAG, "initializeRecyclerView: Initialize RecyclerView");
RecyclerView intervalRecyclerView = findViewById(R.id.intervalRecyclerView);
CustomAdapter adapter = new CustomAdapter(this, WTV, WET, RTV, RET, numberOfIntervals);
intervalRecyclerView.setAdapter(adapter);
intervalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}

关于java - 我在有意发送整数数据时遇到一些麻烦。 (安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com