将动态章节编号添加到Android中详细信息视图的标题栏

时间:2014-11-04 02:45:30

标签: java android xml

我的应用程序的这一部分从书概述到章节

enter image description here

enter image description here

我需要它来显示“章节”旁边的章节编号

这是我目前的代码

ProverbContent

public class ProverbContent {

    public static List<ProverbItem> ITEMS = new ArrayList<ProverbItem>();

    public static Map<String, ProverbItem> ITEM_MAP = new HashMap<String, ProverbItem>();

    static {

        addItem(new ProverbItem("1", "Chapter 1",
            "1 The proverbs of Solomon the son of David, king of Israel;\n" +
            "\n" +
    ... res of the content goes here

        ));

    }

    private static void addItem(ProverbItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);
    }

    public static class ProverbItem {
        public String id;
        public String content;
        public String chapter;

        public ProverbItem(String id, String content, String chapter) {
            this.id = id;
            this.content = content;
            this.chapter = chapter;
        }

        @Override
        public String toString() {
            return content;
        }
    }
}

activity_proverb_detail.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/proverb_detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProverbDetailActivity"
    tools:ignore="MergeRootFrame" />

ProverbDetailActivity

public class ProverbDetailActivity extends ActionBarActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_proverb_detail);

          getSupportActionBar().setDisplayHomeAsUpEnabled(true);

          if (savedInstanceState == null) {

              Bundle arguments = new Bundle();
              arguments.putString(ProverbDetailFragment.ARG_ITEM_ID,
                      getIntent().getStringExtra(ProverbDetailFragment.ARG_ITEM_ID));
              ProverbDetailFragment fragment = new ProverbDetailFragment();
              fragment.setArguments(arguments);
              getSupportFragmentManager().beginTransaction()
                      .add(R.id.proverb_detail_container, fragment)
                      .commit();
          }
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == android.R.id.home) {

              NavUtils.navigateUpTo(this, new Intent(this, ProverbListActivity.class));
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }

的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Proverbs Reference</string>
    <string name="title_proverb_detail">Chapter</string>

</resources>

<小时/> 我是Java和Android开发的新手,但我想我必须在 strings.xml 中添加一个数组,如

<string-array name="chapter">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    ....
</string-array>

可以在“章节”之后呈现,但我不确定我的下一步应立即将这一切联系起来

2 个答案:

答案 0 :(得分:0)

嗯......这个基本方法可以用来理解这个过程...

关于您的第一项活动......
- 创建ListView并设置值..
- 为ListView设置监听器单击... - 当用户选择项目时,设置章节编号 - 那就是它!

关于您的详细活动......
- 获取章节数...
- 将此数字设置为标题栏...
- 并使用该数字加载所选章节的文本... - 而且......那就是它! 。

ListChapter.java

public class ListChapters extends ActionBarActivity {

    ListView myListView;
    String[] myList = { "Introducing JAVA", "Overview", "Guide: How to...?", "etc" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_chapters);

        myListView = (ListView)findViewById(R.id.myListView);

        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList ); 
        myListView.setAdapter(myAdapter);

        myListView.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){

                switch(position){
                case 0:
                    OpenChapter(ListChapters.this, ChapterDetail.class, 1);
                    break;
                case 1:
                    OpenChapter(ListChapters.this, ChapterDetail.class, 2);
                    break;
                case 2:
                    OpenChapter(ListChapters.this, ChapterDetail.class, 3);
                    break;
                case 3:
                    OpenChapter(ListChapters.this, ChapterDetail.class, 4);
                    break;
                }

            };
        });

    }

    public void OpenChapter(Activity activityIn, Class<?> activityOut, int chapter){
        Intent intent = new Intent(activityIn, activityOut);
        Bundle values = new Bundle();
        values.putInt("chapterNum", chapter);
        intent.putExtras(values);
        startActivity(intent);
    }

}



ChapterDetail.java

public class ChapterDetail extends ActionBarActivity {

    int chapterNumber;
    TextView chapterContent;
    String[] chapterText = {
            "This is the content of chapter one, this data comes from db or other source.",
            "This is the content of chapter Two, this data comes from db or other source.",
            "This is the content of chapter Three, this data comes from db or other source.",
            "This is the content of chapter Four, this data comes from db or other source.",
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chapter_detail);

        Bundle values = getIntent().getExtras();
        chapterNumber = values.getInt("chapterNum");

        getSupportActionBar().setTitle("Chapter " + chapterNumber);

        chapterContent = (TextView)findViewById(R.id.chapterContent);

        switch(chapterNumber){
        case 1:
            chapterContent.setText(chapterText[0]);
            break;
        case 2:
            chapterContent.setText(chapterText[1]);
            break;
        case 3:
            chapterContent.setText(chapterText[2]);
            break;
        case 4:
            chapterContent.setText(chapterText[3]);
            break;
        }
    }

}

答案 1 :(得分:0)