Answer the question
In order to leave comments, you need to log in
What is the correct way to pass data from one Activity to another Activity?
I have a simple application that interacts with firebase
In the firebase base, there are only two values \u200b\u200bthis name and url
My Item class
public class ItemFirebase {
String name, url;
public String getName() {
return name;
}
public String getUrl() {
return url;
}
}
public class MyAdapter extends RecyclerView.Adapter <MyAdapter.MyViewHolder> {
MainActivity context;
ArrayList<ItemFirebase> list;
public MyAdapter(MainActivity context, ArrayList<ItemFirebase> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
ItemFirebase itemFirebase = list.get(position);
holder.name.setText(itemFirebase.getName());
holder.data.setText(itemFirebase.getUrl());
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, data;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
data = itemView.findViewById(R.id.url);
}
}
}
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference database;
MyAdapter myAdapter;
ArrayList<ItemFirebase> list;
Button view;
TextView data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.materialList);
view = findViewById(R.id.view);
data = findViewById(R.id.url);
database = FirebaseDatabase.getInstance().getReference("Items");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
myAdapter = new MyAdapter(this,list);
recyclerView.setAdapter(myAdapter);
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
ItemFirebase itemFirebase = dataSnapshot.getValue(ItemFirebase.class);
list.add(itemFirebase);
}
myAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
view.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
intent.putExtra("data", data.getText().toString());
startActivity(intent);
finish();
});
}
}
public class WebViewActivity extends AppCompatActivity {
TextView url;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
url.findViewById(R.id.url);
String data = getIntent().getStringExtra("data");
webView = findViewById(R.id.webView);
// включаем поддержку JavaScript
webView.getSettings().setJavaScriptEnabled(true);
// указываем страницу загрузки
webView.loadUrl(url.getText().toString() + " " + data);
}
}
view.setOnClickListener(view -> {
Process: com.buratinoapps.newairdrop, PID: 18901
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.buratinoapps.newairdrop/com.buratinoapps.newairdrop.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question