2017-01-08 58 views
0

我已經搜遍了許多類似的問題,但我無法找到我正在尋找的答案。我很自學,所以我對這個問題表示歉意。基本上我試圖訪問我的數組中的字符串ID並將其轉換爲可讀的字符串,以便我可以將其發送到谷歌地圖,以便用戶可以在那裏導航。這是我得到的最接近的。如何將ArrayList中的int String id轉換爲可用的字符串?

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 

import java.util.ArrayList; 

public class FoodFragment extends Fragment { 

    public FoodFragment() { 

    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.word_list, container, false); 

     final ArrayList<Location> places = new ArrayList<>(); 
     places.add(new Location(R.string.food1, R.string.desc1, R.drawable.tapass)); 
     places.add(new Location(R.string.food2, R.string.desc2, R.drawable.baguette)); 
     places.add(new Location(R.string.food3, R.string.desc3, R.drawable.sushi)); 
     places.add(new Location(R.string.food4, R.string.desc4, R.drawable.burger)); 
     places.add(new Location(R.string.food5, R.string.desc5, R.drawable.meat)); 
     places.add(new Location(R.string.food6, R.string.desc6, R.drawable.pizza)); 

     final Location here = places.get(0); 
     final int address = here.getName(); 

     MyAdapter adapter = new MyAdapter(getActivity(), places); 

     final ListView listView = (ListView) rootView.findViewById(R.id.list); 

     listView.setAdapter(adapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 

       Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
         Uri.parse("google.navigation:q=" + address)); 
       startActivity(intent); 
      } 
     }); 
     return rootView; 
    } 
} 

getName來自我的Location類。我意識到它返回一個int(以便我可以將string.xml中的字符串id放入數組中),但是如何獲取它以返回實際的字符串值?

public class Location { 

    private int mName; 
    private int mDescription; 
    private int mImage; 


    public Location (int name, int description, int image){ 
     mName = name; 
     mDescription = description; 
     mImage = image; 
    } 

    public int getName(){ 
     return mName; 
    } 

    public int getDescription(){ 
     return mDescription; 
    } 

    public int getImage(){ 
     return mImage; 
    } 
} 

我再次道歉noob問題和任何幫助,非常感謝。

回答

0

你應該改變你的位置類看起來像這樣

public class Location { 

    private int mName; 
    private int mDescription; 
    private int mImage; 
    private Context mContext; 


    public Location (int name, int description, int image, Context context){ 
     mName = name; 
     mDescription = description; 
     mImage = image; 
     mContext = context; 
    } 

    public String getName() { 
     return mContext.getString(mName) 
    } 

    public int getDescription(){ 
     return mDescription; 
    } 

    public int getImage(){ 
     return mImage; 
    } 
} 

那麼你可以創建一個這樣

places.add(new Location(R.string.food1, R.string.desc1, R.drawable.tapass, getActivity())); 
0

這裏address位置實例的string.xml 資源id您可以訪問的address值ID使用

String value = getString(address); 
+0

這正是我所尋找的非常感謝你。我太新了+1,但當我有足夠的代表時,我會回來。 –

相關問題