【问题标题】:Change Textview based on String基于字符串更改Textview
【发布时间】:2015-05-21 20:31:07
【问题描述】:

我正在尝试使用 Listview 根据上一个视图的字符串更改 TextView 中的文本。

由于某种原因,它不断返回错误。这是我的代码:

public class LyricsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        /** Inflating the layout country_details_fragment_layout to the view object v */
        View v = inflater.inflate(R.layout.learnlyrics, null);

        /** Getting the textview object of the layout to set the details */
        TextView tv = (TextView) v.findViewById(R.id.learnsong);

        Bundle b = getArguments();

        tv.setText("Details of " + Country.name[b.getInt("position")]);

        int position = b.getInt("position");


        String s = b.getInt("position");
        if (s.startsWith("Pretty Hurts")) {

            tv.setText("[Intro]\\nHarvey Keitel: Ms. Third Ward, your first question -- what is your aspiration in life?\\n" +
                    "Beyoncé: Oh, my aspiration in life would be to be happy\\n\\n[Verse 1]\\nMama said, you're a pretty girl\\n " +
                    "What's in your head it doesn't matter\\nBrush your hair, fix your teeth\\nWhat you wear is all that matters\\n\\n[Pre-Hook]\\nJust another stage\\nPageant the pain away\\nThis time I'm gonna take the crown\\nWithout falling down, down\\n\\n[Hook]\\nPretty hurts\\nWe shine the light on whatever's worse\\nPerfection is the disease of a nation\\nPretty hurts, pretty hurts\\nWe shine the light on whatever's worse\\nTryna fix something\\nBut you can't fix what you can't see\\nIt's my soul that needs the surgery\\n\\n[Verse 2]\\nBlonder hair, flat chest\\nTV says bigger is better\\nSouth beach, sugar free\\nVogue says thinner is better\\n\\n[Pre-Hook + Hook]\\n\\n[Bridge]\\nAin’t got no doctor or pill that can take the pain away\\nThe pain's inside\\nAnd nobody frees you from your body\\nIt's the soul, its the that needs surgery\\nIt's the soul that needs surgery\\nPlastic smiles and denial can only take you so far\\nThen you break when the fake facade leaves you in the dark\\nYou left a shattered mirror\\nAnd the shards of a beautiful girl\\n\\n[Hook]\\n\\n[Outro]\\nWhen you're alone all by yourself\\nAnd you're lying in your bed\\nReflection stares right into you\\nAre you happy with yourself?\\nIt's just a way to masquerade\\nThe illusion has been shed\\nAre you happy with yourself?\\nAre you happy with yourself?\\nYes\\n");

        }

        return v;
    }
}

错误是:

String s = b.getInt("position");

作为一个新的安卓开发者,我不知道为什么?请帮忙。

【问题讨论】:

  • 试试String s = String.valueOf(b.getInt("position"));
  • 感谢您这么快回复。错误不再存在,但文本仍然没有改变:(
  • 您在 LogCat 中看到了什么吗?你Bundle b = getArguments();中的数据是从哪里来的?
  • 来自一个活动! setContentView(R.layout.country_details_activity_layout); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransacton = fragmentManager.beginTransaction(); LyricsFragment detailsFragment = new LyricsFragment();捆绑 b = 新捆绑(); b.putInt("位置", getIntent().getIntExtra("位置", 0)); detailsFragment.setArguments(b); fragmentTransacton.add(R.id.country_details_fragment_container, detailsFragment); fragmentTransacton.commit();

标签: java android xml string


【解决方案1】:

@JonasCz 指出错误

您需要从 int 转换为 String,因为您不能将 int 存储到 String 变量中!

String s = String.valueOf(b.getInt("position"));

【讨论】:

  • 到目前为止一切顺利。文本并没有改变:(我在想 String s = String.valueOf(b.getInt("position")); if (s.startsWith("Pretty Hurts")) { tv.setText("What chu say"); } 是问题。请帮忙。
  • this: if ("Pretty Hurts".equals(s)){ tv.setText("What chu say"); } 也不起作用:/
【解决方案2】:

想通了。将意图更改为 Activity 并且 Activity 具有以下代码:

public class SongLyrics extends Activity {

Context context;
String[] rank;
int position;

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

    /** Setting the layout for this activity */
    setContentView(R.layout.learnlyrics);


    String title = getIntent().getExtras().getString("rank");

    Intent i = getIntent();
    position = i.getExtras().getInt("position");
    rank = i.getStringArrayExtra("rank");


    TextView titleTextView = (TextView) findViewById(R.id.learnsong);

  //  titleTextView.setText(title);

    titleTextView.setText(rank[position]);



}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多