Twice opening activity with an null value - android

i have an activity with some buttons in which when you click on any of buttons First with Intent send a value to another activity that contains a list view is filled with database. And I want to use this value as a parameter for where command select for filling list view with special Content
now i have a problem that when run program and when click on a button, first open a empty list view and after clicking back-button, list view is correctly displayed data.
Where is my mistake?
source MainActivity
final Intent i = new Intent(MainActivity.this,ListActivity.class);
btn_irani.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
i.putExtra("position","ایران");
startActivity(i);
startActivity(GoToList);
}
});
btn_turkie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
i.putExtra("position","ترکیه");
startActivity(i);
startActivity(GoToList);
}
});
source ListActivity
public class ListActivity extends Activity {
String value = "";
MovieDB myDbHelper;
SQLiteDatabase db;
ListAdapter adapter;
ArrayList<HashMap<String, String>> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
value = getIntent().getStringExtra("position");
ListView lst=(ListView) findViewById(R.id.listView1);
Load_Database();
db = myDbHelper.getReadableDatabase();
Cursor c = db.rawQuery("select * from movie_list where product = '"+value+"'", null);
data = new ArrayList<HashMap<String, String>>();
for (; c.moveToNext();) {
HashMap<String, String> map = new HashMap<String, String>();
String img = c.getString(c.getColumnIndex("img"));
String name = c.getString(c.getColumnIndex("name"));
map.put("img", img);
map.put("name", name);
data.add(map);
}
adapter = new ListMovie(this, data);
lst.setAdapter(adapter);
}
Please Help me!

I think you have called startActivity() twice by mistake. If you want to send a value to your next Activity while calling it once startActivity() is sufficient.

Related

Pass Values to ListView by Clicking a Button

I want to pass values to list view by clicking a button. Problem is I want to make a list by gaining values from different activities with several buttons.
For Example :
In EnglandActivity if I click Button Visit I want to pass "England" to ListView in MainActivity,
In MalaysiaActivity pass "Malaysia" to ListView in MainActivity.
I dont know how to do that, Can you help me??
First Of All, You should write this on your onCreate() method of MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String data;/*For Storing Country Name*/
ListView listview = (ListView)findViewById(R.id.listView); /*Finding ListView From Layout*/
ArrayList<String> list = new ArrayList<String>(); /*ArrayList To Store All The Data Of ListView*/
ArrayAdapter adapter= new ArrayAdapter<String>(this,R.layout.android.R.layout.simple_list_item_1,list);/*Defining ArrayAdapter For ListView*/
listview.setAdapter(adapter); /*Setting Adapter To ListView*/
Bundle intentExtras = getIntent().getExtras(); /*Getting The Intent Extras Sent By The Activity Which You Had Navigated From*/
if(intentExtras != null) {/*Checking For Null*/
data= intentExtras.getString("countryName");/*Extracting The Data From The Intent Extras*/
list.add(0,data);/*You Can Replace 0 With The Position Of Your Wish*/
} else {
data=null;
}
}
Now Write This In The OnClickListener() Method Of Button On Each CountryNameActivity.java
btn.setOnClickListener(new OnClickListener() {/*Setting The Click Listener*/
#Override
public void onClick(View v) {
Intent intent = new Intent(this,MainActivity.this)/*Defining The Intent*/
intent.putExtra("countryName","CountryName");/*Putting The Data To Pass To The Next Activity*/
startActivity(intent);/*Starting The Activity*/
}
});
Make an object shared by all of your activity, put your data there and read them when you must put data into your ListView's adapter
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent listIntent = new Intent(getApplicationContext(), YourActivity.class);
listIntent.putExtra("country", yourList.get(position));
startActivity(listIntent);
}
});
in next activity onCreate:
String country;
Bundle extras = getIntent().getExtras();
if(extras == null) {
country= null;
} else {
country= extras.getString("country");
}

get user inputs from an editText and populate listView

How can i get user inputs from one activity and populate the listView with user data in another activity. I am able to get user input and populate the listView in the same activity. but now i want to get user inputs in one form and populate the list in another activity.
the code that i used to populate the listView by getting user input is as follows
public class MainActivity extends ListActivity {
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnAdd);
/** Defining the ArrayAdapter to set items to ListView */
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
/** Defining a click event listener for the button "Add" */
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
String name=edit.getText().toString();
list.add(name);
edit.setText("");
adapter.notifyDataSetChanged();
}
};
/** Setting the event listener for the add button */
btn.setOnClickListener(listener);
you can store your user input / data into a local database; that will allow you to access your data anywhere in the app
(recommended since you are dealing with listview).
you can use shared preferences to store data if your data is relatively small.
In your current Activity (activity contains your button), create a new Intent:
String name = "";
name = edit.getText().toString();
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("keyword",name);
startActivity(i);
Then in the NewActivity (activity contains your Listview), retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString("keyword");
if(name != ""){
// adapter.notifyDataSetChanged();
}
}
It is simple way, hope this help
Declare a public method in second Activity like
SecondActivity.class
public static ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter
onCreate()
{
...
;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
...
}
public static void ModifyList()
{
adapter.notifyDataSetChanged();
}
FirstActivity.class
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.txtItem);
String name=edit.getText().toString();
SecondActivity.list.add(name);
edit.setText("");
SecondActivity.ModifyList();
}
};
Send your ArrayList like this from FirstActivity :
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putStringArrayListExtra("Datalist",list);
startActivity(intent);
In secondActivity Recieve the list using :
Intent i = getIntent();
list = i.getStringArrayListExtra("Datalist");
Then display it in your SecondActivitys listview

Activity missing data on return

So I have my main activity which holds a list view and has a map which holds all my data for the list. Upon clicking on an item you are taken to a details display. When I press the back button to get back to the main activity from the detail activity, if I set a break point, my map keys are still intact, but all the strings in the objects are "" and the ints are -1. Here is what my main activity looks like:
public class MainActivity extends Activity {
private Map<String, Stunt> stunts = new LinkedHashMap<String, Stunt>();
private StuntsDao stuntsDao;
private ListAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stuntsDao = new StuntsDao(getApplicationContext());
stunts = stuntsDao.getAllStunts();
listAdapter = new ListAdapter(this, R.layout.list_layout, new ArrayList<Stunt>(stunts.values()));
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String stuntName = (String)((ViewHolder)view.getTag()).stuntName.getText();
Intent myIntent = new Intent(getApplicationContext(), StuntDetails.class);
getStuntDetailsIfNeeded(stuntName);
Stunt stunt = stunts.get(stuntName);
myIntent.putExtra("STUNT", stunt);
startActivity(myIntent);
}
});
}
...
}
Why would the objects in my map be basically empty?
onCreate() doesn't get called when you click back. You need to init your data again in onStart or onResume. See: http://developer.android.com/training/basics/activity-lifecycle/starting.html

How to reach variables in an activity file from another activity file?

i have two activity files in my code, and the first activity file loads the layout search, and the second file loads layout list. I have a textbox in layout search and enter some text. I want to use this text in my second activity file but i can not reach it since it is in the layout search. How can i do this? Here the first activity file, here there is an EditText item called searchedText, and i want to use it in the second activity file.
public class SearchActivity extends Activity{
public EditText searchedText;
public RadioGroup radioGroup;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
}
public void onStart(){
super.onStart();
searchedText = (EditText) findViewById(R.id.searchText);
Button searchinSearchButton = (Button)findViewById(R.id.searchInSearch);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
searchinSearchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String searched=searchedText.getText().toString();
Intent myIntent = new Intent(v.getContext(),
SearchListActivity.class);
startActivityForResult(myIntent, 1);
}
});
}
}
And here is the second activity file:
public class SearchListActivity extends Activity{
public DatabaseAdapter db;
public ArrayList<String> myList;
public ListView listview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
db = new DatabaseAdapter(this);
myList = new ArrayList<String>();
getContacts();
// Example of retrieving tweets of the user "mashable" and adding them to
myList
/*
ArrayList<Tweet> tweets= new ArrayList<Tweet>();
tweets.addAll(Twitter.getTimeline("mashable", 10));
for(Tweet t: tweets){
myList.add(t.username + ": " + t.message + " Tweet id: "+ t.id);
}
*/
printList();
}
public void printList(){
listview = (ListView)findViewById(R.id.contactcListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listview.setAdapter(adapter);
}
public void getContacts() {
db.open();
Cursor c = db.getContactbyName("y");
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
db.close();
}
public void DisplayContact(Cursor c) {
String entry = "";
// if you add another attribute to your table, you need to change 3 into x
for (int i=1; i<5;i++){
entry += c.getString(i) + "\n";
}
myList.add(entry);
}
}
In this second activity file, you can see the getContacts() method. There, i search by Cursor c = db.getContactbyName("y"); but instead of "y", i want to search whatever user enters the texbox, whic is in the 1st activity file called searchedText. How can i do this?
Thanks
Send the text as an extra in your Intent when you start your second activity.
searchinSearchButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String searched=searchedText.getText().toString();
Intent myIntent = new Intent(v.getContext(),
SearchListActivity.class);
myIntent.putExtra("SEARCH_STRING",searched);
startActivityForResult(myIntent, 1);
}
});
And in your onCreate get the extra. You could use Intent.getStringExtra(String name)
In other words:
mySearched = getIntent().getStringExtra("SEARCH_STRING");
Just make sure to see if anything is null before using it.

how to display path in our next activity on itemselect in listview

public class Lucenconcept extends Activity {
Button btn1;
EditText mEdit;
String txt2;
public ListAdapter adapter;
private ListView lv;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
File index = new File("/sdcard/index/");
index.mkdir();
mEdit = (EditText)findViewById(R.id.editText1);
lv=(ListView) findViewById(android.R.id.list);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Analyzer analyzer = new StandardAnalyzer();
IndexSearcher indexSearcher;
try {
indexSearcher = new IndexSearcher("/sdcard/index/");
QueryParser parser = new QueryParser("text", analyzer);
Hits hits = indexSearcher.search( parser.parse("("+ "text:" +mEdit.getText().toString() + ")"));
String txt2[] =new String[100];
String txt="";
for (int i = 0; i < hits.length(); i++) {
Document hitDoc = hits.doc(i);
Log.i("TestAndroidLuceneActivity", "Lucene: " +hitDoc.get("title")+ hitDoc.get("path"));
txt=hitDoc.get("title");
txt2[i]=txt;
String location=hitDoc.get("path");
}
lv.setAdapter(new ArrayAdapter
<String>
(Lucenconcept.this,android.R.layout.simple_list_item_1 ,txt2));
indexSearcher.close();
}
});
}
}
I m able show title on list view I want to display path hitDoc.get("path") from this string on item selct in next activity ..
Cud any one plz help meam not able to display url in next activty while I have put all the think manifest and all the I think there is mistake in postion plz help me..
Use Intent for passing data between activities
Refer Intent example

Categories

Resources