我有三个String数组,其中两个在一个名为Homepage.java的类中,其中一个在Chatss.java中,如下所示 -
Homepage.java代码为 -
package com.md.viyo;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.CountCallback;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class Homepage extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, Homepage.TabInfo>();
private PagerAdapter mPagerAdapter;
int j=0,i=0;
String[] fetch_name,fetch_num;
SharedPreferences prefs1, prefs, prefs2;
int tottal,ij=0, snumcount=0, snamecount=0, scloudcount=0;
Context mcontext;
/**
*
* @author mwho
* Maintains extrinsic info of a tab's construct
*/
private class TabInfo {
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
/**
* A simple factory that returns dummy views to the Tabhost
* @author mwho
*/
class TabFactory implements TabContentFactory {
private final Context mContext;
/**
* @param context
*/
public TabFactory(Context context) {
mContext = context;
}
/** (non-Javadoc)
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
/** (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout
setContentView(R.layout.activity_home);
Parse.initialize(getApplicationContext(), "TUkcv3AsYpCAeZB1R6d5o3HvujUICdv9sqF4nMg5", "vBC4pyACIlwWxRmtqHriAJG2Jk3WFuF0X9bWpGyI");
// Initialise the TabHost
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff9911")));
bar.setIcon(new ColorDrawable(Color.parseColor("#ff9911")));
//mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF"));
// mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
// Intialise ViewPager
this.intialiseViewPager();
Cursor phones1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null );
while(phones1.moveToNext())
{
j=j+1;
}
fetch_name = new String[j];
fetch_num = new String[j];
phones1.moveToFirst();
while(phones1.moveToNext())
{
fetch_name[i]= phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
fetch_num[i] = phones1.getString(phones1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
i =i + 1;
}
phones1.close();
Toast.makeText(getApplicationContext(), ""+ fetch_num.length, Toast.LENGTH_LONG).show();
}
/** (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
*/
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
super.onSaveInstanceState(outState);
}
/**
* Initialise ViewPager
*/
private void intialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Callss.class.getName()));
fragments.add(Fragment.instantiate(this, Chatss.class.getName()));
fragments.add(Fragment.instantiate(this, Contactss.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
//
this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}
/**
* Initialise the Tab Host
*/
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
Homepage.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Calls").setIndicator("Calls"), ( tabInfo = new TabInfo("Calls", Callss.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
Homepage.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Chats").setIndicator("Chats"), ( tabInfo = new TabInfo("Chats", Chatss.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
Homepage.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Contacts").setIndicator("Contacts"), ( tabInfo = new TabInfo("Contacts", Contactss.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
// Default to first tab
//this.onTabChanged("Tab1");
//
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
//mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#ffffff"));
}
mTabHost.setOnTabChangedListener(this);
}
/**
* Add Tab content to the Tabhost
* @param homepage
* @param tabHost
* @param tabSpec
* @param clss
* @param args
*/
private static void AddTab(Homepage homepage, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(homepage.new TabFactory(homepage));
tabHost.addTab(tabSpec);
}
/** (non-Javadoc)
* @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
*/
public void onTabChanged(String tag) {
//TabInfo newTab = this.mapTabInfo.get(tag);
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
*/
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
*/
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
this.mTabHost.setCurrentTab(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
*/
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.items, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.profile:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
在上面的代码中,两个String Arrays是&#34; fetch_name&#34; &安培; &#34; fetch_num&#34; 现在还有一个字符串数组作为&#34; cloud_num&#34; 其代码如下 -
package com.md.viyo;
import java.util.List;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.parse.CountCallback;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class Chatss extends android.support.v4.app.Fragment{
String[] cloud_num;
String sss;
SharedPreferences spref1;
aint ij=0;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_chatss, container, false);
spref1 = getActivity().getSharedPreferences("con", Context.MODE_APPEND);
sss = spref1.getString("conno", "nai");
final ParseObject po = new ParseObject("Register");
loadone(sss);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
loadtwo(sss);
return v;
}
private void loadone(String sss1) {
// TODO Auto-generated method stub
ParseQuery<ParseObject> hue = ParseQuery.getQuery("Register");
hue.whereNotEqualTo("contact",sss1);
hue.countInBackground(new CountCallback() {
@Override
public void done(int tottal, ParseException arg1) {
// TODO Auto-generated method stub
if (arg1!=null){
Toast.makeText(getActivity(), "Please try after some time..." , Toast.LENGTH_LONG).show();
}
else
{
cloud_num = new String[tottal];
}
Toast.makeText(getActivity(), "" + tottal, Toast.LENGTH_LONG).show();
}
});
hue.cancel();
}
private void loadtwo(String sss2) {
// TODO Auto-generated method stub
ParseQuery<ParseObject> quee = ParseQuery.getQuery("Register");
quee.whereNotEqualTo("contact",sss2);
quee.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> oobid, ParseException arg1) {
// TODO Auto-generated method stub
if (arg1!=null)
{
Toast.makeText(getActivity(), ""+arg1, Toast.LENGTH_LONG).show();
}
else{
for(ParseObject register : oobid){
cloud_num[ij] = register.getString("contact").toString();
ij=ij+1;
}
}
Toast.makeText(getActivity(), "i" + cloud_num.length, Toast.LENGTH_LONG).show();
}
});
quee.cancel();
Toast.makeText(getActivity(), "ho gya", Toast.LENGTH_LONG).show();
}
public String[] getcnum()
{
return(cloud_num);
}
} 我想在另一个名为Contactss.java的类中使用这三个String Arrays值 -
Contactss.java如下 -
package com.md.viyo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class Contactss extends android.support.v4.app.Fragment
{
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_contactss, container, false);
return v;
}
}
我已经尝试了很多传递String Arrays的方法,但没有任何帮助,请通过提供完整代码来传递String Arrays来帮助我。
答案 0 :(得分:0)
您可以使用public Static
关键字string arrays
并在所需的任何课程中调用
或
<强> SharedPrefernces 强>
将其转换为带有分隔符的字符串并在共享偏好中进行stroing
StringBuilder sb = new StringBuilder();
for (int i = 0; i < playlists.length; i++) {
sb.append(playlists[i]).append(",");
}
prefsEditor.putString(PLAYLISTS, sb.toString());
在审核结束时
String[] playlists = playlist.split(",");
答案 1 :(得分:0)
在第一类声明这两个方法:
public static String[] fun1(){return fetch_name;}
public static String[] fun2(){return fetch_num;}
同样的方法在第二个类中使用相同的方法签名声明另一个方法。仅按照将要更改函数名称和您返回的String数组变量名称。
public static String() fun3{return cloud_name);
在第三节课中,这样做:
String [] names= Homepage.fun1();
String [] num= Homepage.fun2();
String [] cname= Chatss.fun3();
以上三条线都需要在函数中写入。您可以在类本身中声明String []变量。我希望我足够清楚。如果您有任何疑问,请发表评论。