Android - Problem with two activities
I've got a problem with my project. I'm trying to communicate between two activities but the problem is at the intent of an activity. I have a search activity that has a ListView, when I click on an item of the ListView I'd like to see details about that item but it doesn't happen. Is there someone can help me? This is the link about my project. http://www.mediafire.com/?b4zcqtbeah8wlx9 Thank you very much.
Edit:
This is Details activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Details extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.last_layout);
final Intent intent = getIntent();
if (intent.hasExtra("selectedChild"))
{
final Child selectedChild = (Child) intent.getSerializableExtra("selectedChild");
if (selectedChild != null)
{
(开发者_开发百科(TextView) findViewById(R.id.Title)).setText(selectedChild.getName());
((ImageView) findViewById(R.id.imageView1)).setImageResource(getResources().getIdentifier(selectedChild.getImage(),
"drawable", "com.test.com"));
}
}
}
}
This is Search activity:
import java.util.ArrayList;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.app.Activity;
import android.content.Intent;
import android.widget.ArrayAdapter;
public class Search extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[] = {
"America",
"Busta",
"Cactus",
"Fire",
"Garden",
"Hollywood",
"King",
"Laboratorio",
"Malta",
"Nespola",
"Pera",
"Rosa",
"Sapone",
"Verano",
};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
lv1=(ListView)findViewById(R.id.ListView1);
ed=(EditText)findViewById(R.id.EditText1);
lv1.setClickable(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent intent = new Intent(Search.this, Details.class);
startActivity(intent);
}
{
}});
I suggest reading through documentation to figure out what putExtra()
does.
I have no idea what Child
is supposed to be, however there is no putExtra(String name, Child child)
method, so you are using this method wrong.
As suggested you can use a database for your data.
Other option:
You appear to be pulling two Strings from your Child
class, selectedChild.getName()
and selectedChild.getImage()
. Instead you can pass in two strings as extras and pull those out of the Intent in your Details activity:
Search Activity:
intent.putExtra("NAME", selectedChild.getName());
intent.putExtra("IMAGE", selectedChild.getImage());
startActivity(intent);
Details Activity:
String name = getIntent().getStringExtra("NAME");
String image = getIntent().getStringExtra("IMAGE");
((TextView) findViewById(R.id.Title)).setText(name);
((ImageView) findViewById(R.id.imageView1)).setImageResource(getResources().getIdentifier(image,
"drawable", "com.test.com"));
I didn't take a look in your code, as you did not straightly point out where you think your problem is.
However, more generally in regard of passing/using same data in two activities: I think it might be a wise idea to use a small database where you read and write your data. Every Activity could use it and so always has access to all and the latest version.
If you want to use your approach, Intents can pass data when you start a Intent via startActivityForResult()
.
精彩评论