2012-05-02 78 views
1

如何從數組中刪除任何空值。Android:從數組中刪除空值:

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); 

} 
} 

返回點擊它應該帶你到匹配配方時列表視圖,但如果我要毀滅地球,我得到了門戶網站蛋糕食譜的方法。

由於

+1

沒有人會猜到你的代碼。請提供適當的代碼,以便我們提供幫助。 – Nishant

+0

希望有所幫助,如果需要其他代碼,請告訴我,我試圖讓食譜匹配。 –

回答

1

這行代碼是一個問題

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

代替使用mRecipesArray[position]使用ret.get(position).steps

並聲明ArrayList<Recipe> ret = new ArrayList<Recipe>();FetchRecipesRawArray()方法的,以便它可以內部onListItemClick()方法訪問。

還修改FetchRecipesRawArray()

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

希望這將幫助您在代碼..

+0

我不讓我有.steps當改變它,並且我不能改變'ret.add(recipes [i];'因爲我的步驟是一個字符串數組而不是純字符串 –

+0

然後將'ArrayList'改爲' ArrayList ret = new ArrayList ();' – Nishant

+0

現在就開始工作:)非常感謝您的幫助。 –