1

How can I remove any null values from an array.

public class RecipeMethodActivity extends ListActivity {

Intent myIntent;
String value;
TextView editvalue;
TextView buttonPressed;
Intent intent;
String result = null;
InputStream is = null;
StringBuilder sb=null;
String result2 = null;



final Recipe[] mRecipesArray = {    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++){
        if(!recipes[i].name.equals(value)){
            recipes[i].steps = null;
            Recipe[] tmp = new Recipe[recipes.length - 1];
            //int j = 0;
            //for (int k = 0; k < recipes.length; k++) {
            //    if (j != 1) {
            //        tmp[j++] = recipes[i];
            //    }
            //}
            recipes = tmp;
        } else {
            ret.add(recipes[i].name);
        }
    }
    return ret;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   

    value = getIntent().getStringExtra("searchValue");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FetchRecipesRawArray(mRecipesArray));
    setListAdapter(adapter); 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://10.0.2.2/index.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                }catch(Exception e){
                    Log.e("log_tag", "Error in http connection"+e.toString());
                }
                //convert response to string
                try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    sb = new StringBuilder();
                    sb.append(reader.readLine() + "\n");
                    String line="0";
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result=sb.toString();
                    //Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
                }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
                }
                //paring data
                String fd_name;
                try{
                    JSONArray jArray = new JSONArray(result);
                    JSONObject json_data=null;
                    for(int i=0;i<jArray.length();i++){
                        json_data = jArray.getJSONObject(i);
                        fd_name=json_data.getString("recipename");
                    }
                }catch(JSONException e1){
                    Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();
                }catch (ParseException e1){
                    e1.printStackTrace();
                }



}

protected void onListItemClick(ListView l, View v, int position, long id){
    Intent intent = new Intent(this, MethodActivity.class);
    intent.putExtra(MethodActivity.EXTRA_RECIPEARRAY, mRecipesArray[position].steps);
    startActivity(intent);

}
}

it returns a list view which when clicked should take you to the matching recipe but if i ask for Earth Destruction I get the portal cake recipe methods.

Thanks

2
  • 1
    No one is going to guess your code. Please provide proper code so that we can help. Commented May 2, 2012 at 4:20
  • Hope that helps, if other code is needed let me know, I'm trying to get the recipes to match up. Commented May 2, 2012 at 4:35

1 Answer 1

1

This line of code is a problem

 intent.putExtra(MethodActivity.EXTRA_RECIPEARRAY, mRecipesArray[position].steps);

Instead of using mRecipesArray[position] use ret.get(position).steps

and declare ArrayList<Recipe> ret = new ArrayList<Recipe>(); outside of the FetchRecipesRawArray() method so that it can be accessed inside onListItemClick() method.

also modify your code in FetchRecipesRawArray()

else {
        ret.add(recipes[i]);
     }

Hope this will help you..

Sign up to request clarification or add additional context in comments.

2 Comments

I does not let me have .steps when changing it, and also I cannot change to ret.add(recipes[i]; because my steps is a String array not plain string.
Then change the ArrayList to ArrayList<Recipe> ret = new ArrayList<Recipe>();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.