我正在尝试将一个Activity(LensActivity)中的ArrayList中的值传递给MainActivity上的TextView。在此站点上,我找到了Intent方法并进行了尝试,但似乎无法通过它,该信息已在String lensString中获取,并传递给了Intent,但在Main Activity中似乎没有传递或进入该方法。 TextView,在一些实验中,由于getIntent位于MainActivity上,所以我得到了一个空指针。
这是LensActivity的代码,其中包含发送信息的按钮。
package com.komorebiestudio.cam_report_funcionality;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class LensActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private LensAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private TextView LensChange;
private String lensString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lens_activity);
Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
lensIntent.putExtra("LensIntent",lensString);
final ArrayList <LensItem> lensList = new ArrayList<>();
lensList.add(new LensItem(R.drawable.zeiss,"24mm","Zeiss Compact Prime"));
lensList.add(new LensItem(R.drawable.ic_camera,"35mm","Angenieux"));
lensList.add(new LensItem(R.drawable.cooke,"50mm","Cooke S5I"));
mRecyclerView = findViewById(R.id.lens_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new LensAdapter(lensList);
LensChange = findViewById(R.id.lensinfo);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
final Intent ChangeLens = new Intent(this, MainActivity.class);
mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
//Crea String con la informacion de posicion y texto del lente
String lensPosition = lensList.get(position).getLens();
lensString = lensPosition;
Toast.makeText(getApplicationContext(),"this is " + lensString , Toast.LENGTH_SHORT).show();
startActivity(new Intent(LensActivity.this,MainActivity.class));
}
});
}
}
这是接收它的MainActivity的代码。
package com.komorebiestudio.cam_report_funcionality;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements FpsDialog.FpsDialogListener{
private TextView textViewinfo1;
private Button button1;
private Button lensButton;
private TextView lensInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lensInfo = findViewById(R.id.lensinfo);
lensInfo.setText(getIntent().getStringExtra("LensIntent"));
textViewinfo1 = findViewById(R.id.info1);
button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
openDialog();
}
});
lensButton = findViewById(R.id.lensbutton);
lensButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,LensActivity.class));
}
});
}
public void openDialog(){
FpsDialog fps_dialog = new FpsDialog();
fps_dialog.show(getSupportFragmentManager(),"Fps Dialog");
}
@Override
public void applyText(String fpsinfo) {
textViewinfo1.setText(fpsinfo);
}
}
答案 0 :(得分:0)
在LensActivity中,您正在创建一个从未使用过的Intent。目的是这样的:
Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
lensIntent.putExtra("LensIntent",lensString);
相反,您应该在“项目单击侦听器”中创建它。只需删除上面的代码,然后以这种方式修改监听器即可:
mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
//Crea String con la informacion de posicion y texto del lente
String lensPosition = lensList.get(position).getLens();
lensString = lensPosition;
Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
lensIntent.putExtra("LensIntent",lensString);
Toast.makeText(getApplicationContext(),"this is " + lensString , Toast.LENGTH_SHORT).show();
startActivity(lensIntent);
}
});
答案 1 :(得分:0)
在将值分配给变量lensString之后,需要使用putExtra方法
mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
//Crea String con la informacion de posicion y texto del lente
String lensPosition = lensList.get(position).getLens();
lensString = lensPosition;
lensIntent.putExtra("LensIntent",lensString);
Toast.makeText(getApplicationContext(),"this is " + lensString , Toast.LENGTH_SHORT).show();
// Notice that you are not using your previously created intent in you
// original code.
startActivity(lensIntent);
}
});