ListView null pointer exception - android

I have two classes. The first creates an arraylist and an adapter.
Then passes the adapter to an other class to create the ListView.
The problem is that the Listview says it is null. any idea why?
Class 1:
package com.example.seth.greekproducts;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
SQLiteDatabase db;
Builder alert;
EditText etName;
EditText etCode;
String name;
String code;
ArrayList<String> resultslist;
Results results;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.editname);
etCode = (EditText) findViewById(R.id.editcode);
Button bSearch = (Button) findViewById(R.id.bSearch);
bSearch.setOnClickListener(this);
Button bAdd = (Button) findViewById(R.id.bAdd);
bAdd.setOnClickListener(this);
resultslist = new ArrayList<String>();
results = new Results();
db=openOrCreateDatabase("ProductsDB", Context.MODE_PRIVATE, null);
//db.execSQL("CREATE TABLE IF NOT EXISTS products(name VARCHAR, code VARCHAR);");
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bSearch:
name = etName.getText().toString();
code = etCode.getText().toString();
Cursor c=db.rawQuery("SELECT * FROM products WHERE code = '" + code + "'", null);
if(c.moveToFirst())
{
//etName.setText(c.getString(1));
//etCode.setText(c.getString(3));
resultslist.add(c.getString(0));
resultslist.add(c.getString(1));
resultslist.add(c.getString(1));
resultslist.add(c.getString(1));
adapter = new ArrayAdapter<String>(this, R.layout.simplerow, resultslist);
etName.setText(adapter.getItem(0).toString());
etCode.setText(adapter.getItem(1).toString());
results.showResults(adapter);
startActivity(new Intent(this, Results.class));
}
else
{
showInfo("Σφάλμα", "Δεν Βρέθηκε Αποτέλεσμα!");
}
break;
}
}
public void showInfo(String title, String msg){
alert = new Builder(this);
alert.setTitle(title);
alert.setMessage(msg);
alert.show();
}
}
Class 2
package com.example.seth.greekproducts;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Results extends AppCompatActivity{
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
list = (ListView) findViewById( R.id.listView );
}
public void showResults(ArrayAdapter adapter){
list.setAdapter(adapter);
System.out.println("Called!");
}}
The error is:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at Line:
list.setAdapter(adapter);
Activity_Results XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.seth.greekproducts.Results">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>

So your problem is that you are creating an activity with a constructor, like this:
results = new Results();
This won't work, you should always create activities using an Intent and startActivity(...), like you did a bit later in your code:
startActivity(new Intent(this, Results.class));
I also recommend the name suffix Activity for activities, so ResultsActivity in your case.
The main problem you will have is that you can't easily send data to your activity, because you don't have a reference to it.
You can send objects using your Intent the activity is created with.
You have the List<String> resultsList, which you want to pass onto the ResultsActivity. Do this by creating the activity with this intent:
Intent intent = new Intent(this, ResultsActivity.class);
intent.putStringArrayListExtra("results", (ArrayList<String>) resultsList);
startActivity(intent);
Now modify your ResultsActivity that it can take these results:
public class ResultsActivity extends AppCompatActivity {
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
list = (ListView) findViewById(R.id.listView);
ArrayList<String> resultsList = getIntent().getStringArrayListExtra("results");
adapter = new ArrayAdapter<String>(this, R.layout.simplerow, resultslist);
showResults(adapter);
}
public void showResults(ArrayAdapter adapter){
list.setAdapter(adapter);
System.out.println("Called!");
}
}

Activity work independent from each other. If you will call one of method of Activity into another you have to make sure that, first activity is running unless it will give you nullpointer.
So rather then sending adapter using method, Use Intent.putExtra to send data to another activity and initialize adapter there to show data in second activity.
Intent i = new Intent(MainActivity.this,Results.class );
i.putExtra("","");//put data to send to Results.class
startActivity(i);
in Result.class's onCreate method:
Intent i =getIntent();
Object obj=i.getExtra(""); // Change Object with your Parameter type
Second Method:
You can try to startActivity without sending any data from database and when you start Result Activity you can get data from database then show it into listview.
I recommended second solution as it will make you easy to code.

You called the showResults method before starting your activity
results.showResults(adapter);
startActivity(new Intent(this, Results.class));
so your list is null. You must do it after.
But the main problem is that you create a instance of an activity manually. See #Blackbelt answer

Related

int countervalue = i.getIntExtra("Count", 0); makes app crash

int countervalue = i.getIntExtra("Count", 10);
I have this line of code in a different activity when someone clicks a button they go to that activity. Without this line of code the app runs perfectly.
Here is the whole code for the activity:
package com.example.navjeevenmann.mytycoon;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecondActivity extends AppCompatActivity {
private ListView listView;
Intent i = getIntent();
int userchoice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
int countervalue = i.getIntExtra("Count", 0);
String[] values = {"Apple($20)- Generates $40/sec",
"Second", "Third"};
listView = (ListView) findViewById(R.id.List);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
}
initialize i = getIntent(); inside onCreate() method
right before int countervalue = i.getIntExtra("Count", 0);
you can use bundle such as
First Activity
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("int",20);
startActivity(intent);
Now in Second Activity use this
Bundle i = getIntent().getExtras();
int value = i.getInt("int");
Vyacheslav's answer is correct, but here's a little more info.
When you write:
public class SecondActivity extends AppCompatActivity {
Intent i = getIntent();
}
Your Intent i will be intialized as soon as your SecondActivity instance is created, and it will be set to the results of the getIntent() method.
If you look at the source for this method, you see:
public Intent getIntent() {
return mIntent;
}
At the time your SecondActivity instance is created, mIntent is null. So your activity is behaving as though you had written this instead:
public class SecondActivity extends AppCompatActivity {
Intent i = null;
}
The solution, as Vyacheslav said, is to initialize the i variable later on. Anywhere inside onCreate() (after the super call) is a fine place to do so, but waiting until just before your i.getIntExtra("Count", 0) call will work as well.

setting values from an json array to spinner on application level

I want to set Json values into a spinner list. The data I have:
JSONArray lang = jsonObject.getJSONArray("languages");
for (int i = 0; i < lang.length(); i++) {
String language = lang.getString(i);
}
How can i load String values into a spinner list?
ArrayAdapter<String> arrayAdapter = new ArrayAdapter(yourContext, android.R.layout.activity_list_item)
arrayAdapter.addAll(langList); // you need to add your lang.getString(i) to an array
yourSpinner.setAdapter(arrayAdapter);
and voila!
As i understand your problem, you have list of records and you want to show them in spinner in multiple activity.
For this you can create an Class that extends Application Class and create a list in that class.
once You receive response from you web api set data in list of application class.
Now where ever you want to display data in Spinner get it from Application class and render your spinner
See below Example -
public class MyApp extends Application{
private static ArrayList<String> mDataArrayList;
public static ArrayList<String> getmDataArrayList() {
return mDataArrayList;
}
public static void setmDataArrayList(ArrayList<String> mDataArrayList) {
MyApp.mDataArrayList = mDataArrayList;
}
#Override
public void onCreate() {
super.onCreate();
}
}
Call your web api and parse data as you code in your question -
ArrayList<String> mArrayList = new ArrayList<String>();
for (int i = 0; i < lang.length(); i++) {
String language = lang.getString(i);
mArrayList.add(language);
}
//Set Application class list
MyApp.setmDataArrayList(mArrayList);
Now when you want to display data in any class then get list by calling -
ArrayList<String> mArrayList = MyApp.getmDataArrayList();
//Code to display data in Spinner
Hope it will help you.
And dont forgot to add it to manifest of your application -
<application
android:allowBackup="true"
android:name=".MyApp"
....>
....
</application>
I'd suggest you to use Spinner class activity for future usages. Here is the example of spinner activity class (data receiver and displayer):
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class mySpinnerClass extends Activity implements
OnItemSelectedListener {
Spinner spinner;
int selectedValue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
selectedValue = -1;
Intent i = getIntent();
dataSelector = i.getStringExtra("MessageText");
// Spinner element
spinner = (Spinner) findViewById(R.id.mySpinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Loading spinner data
loadSpinnerData();
}
private void loadSpinnerData() {
// Spinner Drop down elements
List<String> lables = yourData.get(dataSelector);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String label = parent.getItemAtPosition(position).toString();
//get the label and do anything you want with it
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
In caller activity:
Intent i = new Intent(getApplicationContext(), mySpinnerClass.class);
i.putExtra("MessageText",yourData);
startActivity(i);
If you want to send non-primitive data (in your example, this is a list) you need to use Serializable type while sending and receiving it. The following link illustrates this technique well:
http://javatechig.com/android/pass-a-data-from-one-activity-to-another-in-android

iterating a global list from any activity

i have a list of object in an activity in which a button in the same activity adds an object to it on every click
i want to be capable to access that list and iterate it from any other activity
i made it public but it gave me an error !
package com.fawzyx.movie_rental_store;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MovieReg_activity extends Activity {
public List<movie> movies = new ArrayList<movie>();
String movName ;
int dvdNo ;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mov_reg_layout);
EditText etmovie_name = (EditText)findViewById(R.id.etmovname);
EditText etdvd_no = (EditText)findViewById(R.id.etdvds);
Button btMovie_submit = (Button)findViewById(R.id.btmovsubmit);
movName= etmovie_name.getText().toString();
dvdNo = Integer.parseInt(etdvd_no.getText().toString()); // to string then to int :)
btMovie_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
movies.add(new movie(movName , dvdNo) );
Toast.makeText(MovieReg_activity.this, "Movie Added", Toast.LENGTH_LONG).show();
}
});
}
}
is there a way to access and iterate the list movies from any other activity ?
You can use a static way to access to this list :
public static List<movie> movies = new ArrayList<movie>();
Then from the other activity :
int size = MovieReg_activity.movies.size(); // exp: check the size
for(movie m : MovieReg_activity.movies){
// do something with m
}
if you want to make a global list, create a static variable or put it in your application class and access it via context.
Search about creating a custom application class.
Maybe use a singleton class that holds your movie list.
Then you'll be able to access it everywhere.
http://androidresearch.wordpress.com/2012/03/22/defining-global-variables-in-android/

ListView: Null pointer exception

Good day. I'm having some issues with my android project specifically listview. I tried searching for other information here in this site, and implemented some of the answers. However, it is still not working.
The error specifically is
NullPointerException at line 76 at MainActivity
Here is the code of my MainActivity
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
final ArrayList<String> studentName = new ArrayList<String>();
ArrayAdapter<String> aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myList = (ListView) findViewById(R.id.listName);
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, studentName);
myList.setAdapter(aa);
//droid.R.id.list;
//add
Button bAdd = (Button) findViewById(R.id.addstudent);
bAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent("android.intent.action.ADDSTUDENTS"));
}
});
//edit
Button bEdit = (Button) findViewById(R.id.editstudent);
bEdit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View x) {
startActivity(new Intent("android.intent.action.EDITSTUDENTS"));
}
});
//edit
Button bDelete = (Button) findViewById(R.id.deletestudent);
bDelete.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View x) {
startActivity(new Intent("android.intent.action.DELETESTUDENTS"));
}
});
}
public ArrayList<String> getArray(){
return studentName;
}
public void notifyArray(){
aa.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and line 76 by the way is
aa.notifyDataSetChanged();
Here is my code for the AddStudents class
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddStudents extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_student);
Button bAddStudents = (Button) findViewById(R.id.add);
final EditText et = (EditText) findViewById(R.id.student_name);
bAddStudents.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
MainActivity as = new MainActivity();
as.getArray().add(et.getText().toString());
as.notifyArray();
finish();
}
});
Button bBack = (Button) findViewById(R.id.backadd);
bBack.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
and the xml part with the list view is
<ListView
android:id="#+id/listName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
I hope you can help me cause I want to also learn what my mistakes are. I can add other information if you want.
In your AddStudents class, you're calling notifyArray() right after you instantiated MainActivity. MainActivity.onCreate() will not be called just by instantiating it.
Instantiating your MainActivity there is probably not what you want anyway (because that object will be disposed directly after the onClick handler is done).
What you want instead is to access the existing instance of MainActivity. For that, add a reference to the current instance to a static member of your MainActivity class, e.g.
public class MainActivity extends Activity {
public static MainActivity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
activity = this;
}
}
Then in your AddStudent class access it via
MainActivity.activity.notifyArray()
This is not the most beautiful way to solve your issue, but it works as long as you can be sure to only have one MainActivity instance. (If not, you could make the array itself static; or create a Singleton wrapper class for it.)
notifyArray() is being called before onCreate.
Try calling getArray().add(et.getText().toString()); and notifyArray(); inside onResume() of MainActivity and NOT from AddStudentActivity( not recommended!)
So onResume() you would ideally want to add a new student to the list, so in your case, you can retrieve the student name using a common sharable object like a hashtable or somethiing similar, make it a singleton, and use it from anywhere in the applciation
The common class may go something like:
class CommonHashtable{
private static Hashtable<String, Object> commonHashtable = null;
public static getInstance(){
if(commonHashtable == null)
commonHashtable = new Hashtable<String, Object>();
return commonHashtable;
}
on getInstance(), it returns a commonHashtable which can be used to store values temporarily!
so, add this on addbutton click event
Hashtable hash = CommonHashtable.getInstance();
hash.put("NEW_STUDENT_NAME", et.getText().toString());
and add this in you onResume() of MainActivity
Hashtable hash = CommonHashtable.getInstance();
Object studentName = (String) hash.get("NEW_STUDENT_NAME");
if(studentName != null){
notifyArray();
}

Android: Is there another way to display data from a string array?

I am trying to display the steps to a recipe, the recipe is stored in a string array.
Im trying to get each step to be inserted into a text view, and have the following code:
public class MethodActivity extends ListActivity{
ListView recipes;
Intent myIntent;
String value;
TextView editvalue;
Intent intent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipemethod);
editvalue = (TextView)findViewById(R.id.method);
value = getIntent().getStringExtra("searchValue");
editvalue.setText(value);
final String[] words = getResources().getStringArray(R.array.Lasagna);
final TextView tw=(TextView)findViewById(R.id.method);
for(int item = 0;item<words.length;item++){
tw.setText(words [item]);
}
}
}
I am also wanting to make it dynamic so does anyone know how I can do that? (e.g. depending on what the user clicked that recipe is shown).
Thanks
It might be easier to actually use your ListActivity's ListView... You can even show your recipe in another ListView, as it will undoubtedly be more intuitive for the user...
A simple code that does this would go somewhat in the following lines:
File 1: RecipeListActivity.java (shows list of recipes)
package com.epichorns.testproject;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class RecipeListActivity extends ListActivity {
final Recipe[] mRecipesArray = { new Recipe("Lasagna", new String[]{"Some lasagna noodles", "Some tomato sauce"}),
new Recipe("PortalCake", new String[]{"GlaDOS' wit","Is a lie"}),
new Recipe("EarthDestruction", new String[]{"Asteroid", "Kinetic energy"})};
public class Recipe{
public String name;
public String[] steps;
Recipe(String name, String[] steps){
this.name = name;
this.steps = steps;
}
}
public ArrayList<String> FetchRecipesRawArray(Recipe[] recipes){
ArrayList<String> ret = new ArrayList<String>();
for(int i=0;i<recipes.length;i++){
ret.add(recipes[i].name);
}
return ret;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FetchRecipesRawArray(mRecipesArray));
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
Intent intent = new Intent(this, RecipeActivity.class);
intent.putExtra(RecipeActivity.EXTRA_RECIPEARRAY, mRecipesArray[position].steps);
startActivity(intent);
}
}
File 2: RecipeActivity.java (shows a recipe in a list)
package com.epichorns.testproject;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class RecipeActivity extends ListActivity {
final static public String EXTRA_RECIPEARRAY = "recipeArray";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] recipeArray = getIntent().getStringArrayExtra(EXTRA_RECIPEARRAY);
if(recipeArray!=null){
if(recipeArray.length>0){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, recipeArray);
setListAdapter(adapter);
}
}
}
}
Alternately, here is a code which composites a TextView below the recipes list in order to show the recipe content without opening another Activity:
File: RecipeListActivity.java
package com.epichorns.testproject;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class RecipeListActivity extends ListActivity {
final Recipe[] mRecipesArray = { new Recipe("Lasagna", new String[]{"Some lasagna noodles", "Some tomato sauce"}),
new Recipe("PortalCake", new String[]{"GlaDOS' wit","Is a lie"}),
new Recipe("EarthDestruction", new String[]{"Asteroid", "Kinetic energy"})};
TextView mTextView_recipeTitle;
TextView mTextView_recipe;
public class Recipe{
public String name;
public String[] steps;
Recipe(String name, String[] steps){
this.name = name;
this.steps = steps;
}
}
public ArrayList<String> FetchRecipesRawArray(Recipe[] recipes){
ArrayList<String> ret = new ArrayList<String>();
for(int i=0;i<recipes.length;i++){
ret.add(recipes[i].name);
}
return ret;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView_recipe = (TextView)findViewById(R.id.recipeText);
mTextView_recipeTitle = (TextView)findViewById(R.id.recipeTitle);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FetchRecipesRawArray(mRecipesArray));
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
mTextView_recipeTitle.setText(mRecipesArray[position].name);
mTextView_recipe.setText("");
String[] recipeSteps = mRecipesArray[position].steps;
for(int i=0;i<recipeSteps.length;i++){
mTextView_recipe.append(recipeSteps[i]+"\n");
}
}
}
File: main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#400000FF"
/>
<TextView
android:id="#+id/recipeTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#4000FF00"
/>
<TextView
android:id="#+id/recipeText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#40FF0000"
/>
</LinearLayout>
You will want to use tw.append() instead of setText, otherwise all you will ever see is the last string in the array.
Make it dynamic, I would use a fragments (using the Android Support Library if on older android versions) model, load each array, attaching the string array as a bundle to the fragment before launching.
Fragment tut:http://developer.android.com/guide/topics/fundamentals/fragments.html
Support library: http://developer.android.com/training/basics/fragments/support-lib.html

Categories

Resources