如何传递和检索两个二维数组作为意图

时间:2019-04-19 07:23:39

标签: android android-intent

我需要将2D数组作为意图传递给另一个活动。

此代码可用于发送2D数组,问题是在下一个活动中将其检索。

i++;
Intent tryAgain = getIntent();
tryAgain.putExtra("index", i);
tryAgain.putExtra("from", "next");

int [][] previousValues = getSelectedIndices();
Toast.makeText(AnotherActivity.this, "Bang " + previousValues[3][3], Toast.LENGTH_SHORT).show();
Bundle mBundle = new Bundle();
mBundle.putSerializable("array", previousValues);

tryAgain.putExtras(mBundle);

AnotherActivity.this.finish();
startActivity(tryAgain);

我已经在下面尝试使用此代码来检索2D数组,它不起作用。

else if(getIntent().getStringExtra("from").equals("next")){
i=index;
int [][] arrayReceived=null;
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("array");
if(objectArray!=null){
    arrayReceived = new int[objectArray.length][];
    for(int i=0;i<objectArray.length;i++){
       arrayReceived[i]=(int[]) objectArray[i];
   }
}

1 个答案:

答案 0 :(得分:1)

您可以做一些接收数组的操作,例如:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use App\User;

class AuthController extends Controller
{


    public function login(Request $request) {

        // Get current user.
        $user = User::where('phone', $request->phone)
                       ->first();

        if ( Hash::check($request->password, $user['password']) ) {

            Auth::login($user, true);
            Auth::logoutOtherDevices($request->password);
            return redirect()->back();
        }   
    }

}