我尝试了很多我在网上找到的解决方案,但是其中任何一个都有效,我有这个代码:
public class Cambiar_ciudad extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_cambiar_ciudad, container,
false);
String[] values = new String[] {"Barcelona", "Madrid", "Sevilla", "Valencia" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return rootView;
}
}
该代码向我展示了List
("Barcelona", "Madrid", "Sevilla", "Valencia")
我希望当我推送其中一个时,他们会将我发送到新的布局。
我是在一个非片段中完成的,它适用于此代码:
Ciudades = new String[]{"Badajoz","Barcelona", "Madrid", "Sevilla", "Valencia"};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Ciudades);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
}
});
但是在ListFragment中,我不知道如何将选项与Menu_Principal.class
我希望你们中的任何人都能解决它,非常感谢!
答案 0 :(得分:0)
将此代码放入
@Override
public void onListItemClick(ListView l, View v, int position, long id){
Context context = getActivity();
// When clicked, show a toast with the TextView text
if (position == 0) {
Intent myIntent = new Intent(context, Menu_Principal.class);
context.startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(context, Menu_Principal.class);
context.startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(context, Menu_Principal.class);
context.startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(context, Menu_Principal.class);
context.startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(context, Menu_Principal.class);
context.startActivityForResult(myIntent, 0);
}
}
答案 1 :(得分:0)
现在我有了这段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_cambiar_ciudad, container,
false);
String[] values = new String[] { "Badajoz","Barcelona", "Madrid", "Sevilla", "Valencia" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return rootView;
@Override
public void onListItemClick(ListView l, View v, int position, long id){
Context context = getActivity();
// When clicked, show a toast with the TextView text
if (position == 0) {
Intent myIntent = new Intent(context, Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(context, Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(context, Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(context, Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(context, Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
}
}
}
但它在这里给我一些错误:
(ListView l, View v, int position, long id){
ListView红色
v on red
还有一些&#34 ;;&#34;预期
错误:(35,36)错误:&#39 ;;&#39;预期
错误:(35,53)错误:&#39;;&#39;预期
错误:(35,54)错误:不是声明
错误:(35,55)错误:&#39;;&#39;预期
错误:(35,70)错误:预期
错误:(35,76)错误:不是声明
错误:(35,78)错误:&#39 ;;&#39;预期