当前位置:编程学习 > wap >>

安卓的问题 求高手指导 急

本来是多个activity来显示多个xml界面的
 但现在的要求是 只用一个activity来接收  
 
我想用参数来区分不同的xml界面  每次activity根据参数来显示你要显示的界面
 这样怎么弄呢?求指导  急!!  --------------------编程问答-------------------- listView1.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{
Intent intent = new Intent(MenuActivity.this,MessageActivity.class);

switch(position){

case 0:
setContentView(R.layout.submenu);
startActivityForResult(intent,1);
break;
case 1:

setContentView(R.layout.subenu);
startActivityForResult(intent,2);
break;
case 3:

setContentView(R.layout.submail);
startActivityForResult(intent,4);
break;
}

}
});
}



能看看这段代码有什么问题吗? --------------------编程问答-------------------- 用Fragment来做,不同的参数加载不同的Fragment。
Fragment的用法你搜一下,网上很多例子的。 --------------------编程问答-------------------- case 0:
setContentView(R.layout.submenu);
startActivityForResult(intent,1);
break;

----->

case 0:
setContentView(R.layout.submenu);
intent.putExtra(XX,1)
startActivityForResult(intent,1);
break;
类似这样的吧
--------------------编程问答--------------------
引用 3 楼 guoyoulei520 的回复:
case 0:
setContentView(R.layout.submenu);
startActivityForResult(intent,1);
break;

----->

case 0:
setContentView(R.layout.submenu);
intent.putExtra(XX,1)
startActivityForResult(intent,1);
break;
类似这样的吧



怎么做啊? --------------------编程问答--------------------
引用 4 楼 u011828076 的回复:
Quote: 引用 3 楼 guoyoulei520 的回复:

case 0:
setContentView(R.layout.submenu);
startActivityForResult(intent,1);
break;

----->

case 0:
setContentView(R.layout.submenu);
intent.putExtra(XX,1)
startActivityForResult(intent,1);
break;
类似这样的吧



怎么做啊?

启动MessageActivity前在intent里面塞相应的值.然后再在MessageActivity里面取值判断啊 --------------------编程问答-------------------- 如果数据量大的话,利用Intent传递数据可能造成内存泄露,建议使用Fragment做,Tabhost也行,不过如果是大屏幕的设备,Android推荐使用Fragment进行界面切换;小设备的话,ActionBar结合Fragment一样可以实现导航功能,进行不同界面间的切换,Fragment很简单:
定义一个Fragment类,在onCreateView方法里写View组件的操作,然后在Activity中new该Fragment对象,就可以将数据传入Fragment中,更新界面组件了 --------------------编程问答-------------------- Activity里的部分方法
package com.example.myddandroidclient;

import java.util.ArrayList;

import org.json.JSONArray;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

import com.example.myddandroid_fragment.ShoppingCartListFragment;
import com.example.myddandroid_fragment.ShoppingCartListFragment.Callbacks;

/**
 * 这是一个ActionBar结合Fragment实现Tab导航的Activity,你可以使用老师以前提供的Tabhost进行实现该功能
 */
public class GoodsListActivity extends Activity implements
ActionBar.TabListener, Callbacks {

static final String GOODS_LIST_ACTIVITY_ACTION = "com.example.my_ddandroidclient.action";
private static final String SELECTED_ITEM = "selected_item";

private ActionBar actionBar;
ShoppingCartListFragment fragment;
private Bundle listJsonArray = new Bundle();
private JSONArray jsonArray;
private ArrayList<Bundle> listItem = new ArrayList<Bundle>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_goods_list_activity);

actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("商品列表")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("查找商品")
.setTabListener(this));
actionBar
.addTab(actionBar.newTab().setText("购物车").setTabListener(this));

}

/**
 * ActionBar.TabListener接口的方法
 */
@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
fragment = new ShoppingCartListFragment();
listJsonArray.putInt(ShoppingCartListFragment.SELECTED_ITEM, tab.getPosition());

fragment.setArguments(listJsonArray);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.goods_detail, fragment);
ft.commit();
}

--------------------编程问答-------------------- 自定义的Fragment的部分方法:
package com.example.myddandroid_fragment;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.myddandroid_adapter.BookDetailListAdapter;
import com.example.myddandroid_adapter.CartDetailListAdapter;
import com.example.myddandroid_adapter.SearchDetailListAdapter;
import com.example.myddandroidclient.GoodsListActivity;
import com.example.myddandroidclient.R;

public class ShoppingCartListFragment extends Fragment implements
SearchView.OnQueryTextListener {

View view;
ListView bookDetailList;
ListView bookCartList;
ListView bookSearchList;
Button accept;
Button delete;
Button search;
EditText searchEdit;

public static final String SELECTED_ITEM = "selected_item";

private HttpClient httpClient;
protected HttpGet get;
protected HttpResponse response;

private Callbacks callbacks;

public inte易做图ce Callbacks {
public void onItemSelected(Bundle item);

public void onCartItemSelected(int position);

public void onCartDeleteButtonClick();

public void getJSONArray(JSONArray jsonArray);
}

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

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"ShoppingCartListFragment所在的Activity必须实现Callbacks接口!");
}
callbacks = (Callbacks) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int position = args.getInt(ShoppingCartListFragment.SELECTED_ITEM);

if (position == 0) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.goods__list, null);
final ListView bookDetailList = (ListView) view
.findViewById(R.id.goods_list);
httpClient = new DefaultHttpClient();
JSONArray jsonArray = null;
if (args.getBundle("1") == null) {
jsonArray = getJSONArray();
callbacks.getJSONArray(jsonArray);
GoodsListActivity goods = (GoodsListActivity) getActivity();
goods.setJSONArray(jsonArray);
} else {
jsonArray = getJSONArrayFromBundle(args);
try {
System.out.println("JSONArray中的数据有——————》"
+ jsonArray.getJSONObject(1).getString(
"bookDetailHead"));
} catch (JSONException e) {
e.printStackTrace();
}
}

BookDetailListAdapter adapter = new BookDetailListAdapter(
jsonArray, getActivity());
bookDetailList.setAdapter(adapter);

bookDetailList
.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
Bundle item = (Bundle) bookDetailList
.getItemAtPosition(position);
Toast.makeText(getActivity(), "加入购物车成功!",
Toast.LENGTH_SHORT).show();
callbacks.onItemSelected(item);
return false;
}
});
return view;
}

if (position == 1) {
View view1 = inflater.inflate(R.layout.search_list, null);
bookSearchList = (ListView) view1.findViewById(R.id.search_list);

// bookSearchList.setTextFilterEnabled(true);不知道有什么用处
SearchView sv = (SearchView) view1.findViewById(R.id.searchView);
sv.setIconifiedByDefault(false);
sv.setOnQueryTextListener(this);
sv.setSubmitButtonEnabled(true);
sv.setQueryHint("搜索书籍");

return view1;
}

if (position == 2) {
View view2 = inflater.inflate(R.layout.cart__list, null);
bookCartList = (ListView) view2.findViewById(R.id.cart_list);
accept = (Button) view2.findViewById(R.id.accept);
delete = (Button) view2.findViewById(R.id.delete);

GoodsListActivity goods = (GoodsListActivity) getActivity();
ArrayList<Bundle> listItem = goods.getListItem();

CartDetailListAdapter adapter = new CartDetailListAdapter(listItem,
getActivity());
bookCartList.setAdapter(adapter);

bookCartList
.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
callbacks.onCartItemSelected(position);
return false;
}
});

accept.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
double price = 0.0;

AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("确认支付")
.setMessage("本次购物共需支付" + price + "元,点击确定转入支付页面……")
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).create().show();
}
});
delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
callbacks.onCartDeleteButtonClick();
}
});
return view2;
}
return null;
}
--------------------编程问答--------------------
引用 8 楼 Kuai_jia_long 的回复:
自定义的Fragment的部分方法:
package com.example.myddandroid_fragment;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.myddandroid_adapter.BookDetailListAdapter;
import com.example.myddandroid_adapter.CartDetailListAdapter;
import com.example.myddandroid_adapter.SearchDetailListAdapter;
import com.example.myddandroidclient.GoodsListActivity;
import com.example.myddandroidclient.R;

public class ShoppingCartListFragment extends Fragment implements
SearchView.OnQueryTextListener {

View view;
ListView bookDetailList;
ListView bookCartList;
ListView bookSearchList;
Button accept;
Button delete;
Button search;
EditText searchEdit;

public static final String SELECTED_ITEM = "selected_item";

private HttpClient httpClient;
protected HttpGet get;
protected HttpResponse response;

private Callbacks callbacks;

public inte易做图ce Callbacks {
public void onItemSelected(Bundle item);

public void onCartItemSelected(int position);

public void onCartDeleteButtonClick();

public void getJSONArray(JSONArray jsonArray);
}

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

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"ShoppingCartListFragment所在的Activity必须实现Callbacks接口!");
}
callbacks = (Callbacks) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int position = args.getInt(ShoppingCartListFragment.SELECTED_ITEM);

if (position == 0) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.goods__list, null);
final ListView bookDetailList = (ListView) view
.findViewById(R.id.goods_list);
httpClient = new DefaultHttpClient();
JSONArray jsonArray = null;
if (args.getBundle("1") == null) {
jsonArray = getJSONArray();
callbacks.getJSONArray(jsonArray);
GoodsListActivity goods = (GoodsListActivity) getActivity();
goods.setJSONArray(jsonArray);
} else {
jsonArray = getJSONArrayFromBundle(args);
try {
System.out.println("JSONArray中的数据有——————》"
+ jsonArray.getJSONObject(1).getString(
"bookDetailHead"));
} catch (JSONException e) {
e.printStackTrace();
}
}

BookDetailListAdapter adapter = new BookDetailListAdapter(
jsonArray, getActivity());
bookDetailList.setAdapter(adapter);

bookDetailList
.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
Bundle item = (Bundle) bookDetailList
.getItemAtPosition(position);
Toast.makeText(getActivity(), "加入购物车成功!",
Toast.LENGTH_SHORT).show();
callbacks.onItemSelected(item);
return false;
}
});
return view;
}

if (position == 1) {
View view1 = inflater.inflate(R.layout.search_list, null);
bookSearchList = (ListView) view1.findViewById(R.id.search_list);

// bookSearchList.setTextFilterEnabled(true);不知道有什么用处
SearchView sv = (SearchView) view1.findViewById(R.id.searchView);
sv.setIconifiedByDefault(false);
sv.setOnQueryTextListener(this);
sv.setSubmitButtonEnabled(true);
sv.setQueryHint("搜索书籍");

return view1;
}

if (position == 2) {
View view2 = inflater.inflate(R.layout.cart__list, null);
bookCartList = (ListView) view2.findViewById(R.id.cart_list);
accept = (Button) view2.findViewById(R.id.accept);
delete = (Button) view2.findViewById(R.id.delete);

GoodsListActivity goods = (GoodsListActivity) getActivity();
ArrayList<Bundle> listItem = goods.getListItem();

CartDetailListAdapter adapter = new CartDetailListAdapter(listItem,
getActivity());
bookCartList.setAdapter(adapter);

bookCartList
.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
callbacks.onCartItemSelected(position);
return false;
}
});

accept.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
double price = 0.0;

AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("确认支付")
.setMessage("本次购物共需支付" + price + "元,点击确定转入支付页面……")
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).create().show();
}
});
delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
callbacks.onCartDeleteButtonClick();
}
});
return view2;
}
return null;
}





谢谢! --------------------编程问答-------------------- 直接在一个xml中把所有的xml包含进来不就可以了

你可以搜下 xml <include />的demo, 然后用view的gone和visiable属性来设置就可以了.

如果还不明白的话,下载一个micode的开源代码 看下就明白了,不过这个有点代码量有点大,呵呵
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,