簡體   English   中英

如何將一個ArrayList中的int字符串id轉換為可用的字符串?

[英]How to convert an int String id from within an ArrayList to a usable string?

我已經搜尋了許多類似的問題,但找不到所需的答案。 我幾乎是自學成才的,因此我很抱歉這個問題。 基本上,我試圖訪問數組中的字符串ID並將其轉換為可讀的字符串,以便可以將其發送到Google Maps,以便用戶可以導航到該位置。 這是我最近得到的。

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

我再次為菜鳥的問題表示歉意,我們將不勝感激。

您應該將Location類更改為如下形式

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

然后您可以像這樣創建Location實例

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

這里address是資源idstring.xml您可以訪問的值address使用ID

 String value = getString(address);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM