我在android中的字符串数组有点麻烦。 onClick工作正常,但它不显示新活动中单击项目的标题。当我说标题时,我的意思是在工具栏中。
<string-array name="states">
<item>Alabama</item>
<item>Alaska</item>
<item>Arizona</item>
<item>California</item>
<item>Colorado</item>
<item>Connecticut</item>
</string-array>
这是我在
中的片段的代码public class FragmentState extends Fragment {
private ListView catagoryList;
private View root;
private TreeAdapter adapter;
public FragmentState() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_scholar, container, false);
catagoryList = (ListView) root.findViewById(R.id.list);
adapter = new TreeAdapter(getActivity(), R.layout.item_catagory, getResources().getStringArray(R.array.categories));
catagoryList.setAdapter(adapter);
catagoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(FragmentState.this.getActivity(), StateView.class);
startActivity(intent);
}
});
return root;
}
}
在我的活动中
public class StateView extends AppCompatActivity {
RecyclerView reship;
BulletinAdapter ship;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scholar_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.ptoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
listTitle();
}
private void listTitle() {
String[] list = getResources().getStringArray(R.array.categories);
int p = 0;
setTitle(list[p]);
}
}
上述方法有效,但标题仅显示点击的第一个项目。我尝试使用Intents但它也不会显示标题。任何帮助将非常感激。
答案 0 :(得分:2)
在您的代码中,您只使用第一个索引值:
int p = 0;
setTitle(list[p]);
答案 1 :(得分:0)
您可以从FragmentState
传递位置。
Intent intent = new Intent(FragmentState.this.getActivity(), StateView.class);
intent.putExtra("STATE_POSITION", position);
startActivity(intent);
并在State
活动中检索并将其传递给listTitle()
public class StateView extends AppCompatActivity {
RecyclerView reship;
BulletinAdapter ship;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scholar_view);
Intent intent = getIntent();
int statePosition = intent.getIntExtra("STATE_POSITION", 0); // Here 0 is default value in case `STATE_POSITION` was not sent via intent
Toolbar toolbar = (Toolbar) findViewById(R.id.ptoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
listTitle(statePosition);
}
private void listTitle(int statePosition) {
String[] list = getResources().getStringArray(R.array.categories);
setTitle(list[statePosition]);
}
}
答案 2 :(得分:0)
首先更改适配器的字符串资源数组名称
adapter = new TreeAdapter(getActivity(), R.layout.item_catagory, getResources().getStringArray(R.array.states));
因为你有数组名称作为状态而不是类别
在开始第二项活动之前,第二次通过捆绑中的点击位置
Intent intent = new Intent(FragmentState.this.getActivity(), StateView.class);
intent.putExtras("position",position);
startActivity(intent);
第二个活动中的第三个读取此捆绑值并将其分配给变量
int selectedPosition=getIntent().getExtras().getInt("position",0);
修改列表标题方法
private void listTitle() {
String[] list = getResources().getStringArray(R.array.states);
setTitle(list[selectedPosition]);
}