我有两个活动。首先,您可以将工作名称和工作地点输入到 inputtext 视图中。第一个活动还包含导航菜单(从屏幕左侧出现)。单击“搜索”按钮后,结果将显示在第二个活动的列表视图中。您可以长按任何列表视图项目,然后它将在第一个活动中添加到导航抽屉中。您可以通过单击“返回”箭头返回第一个活动。问题是,当我要返回第一个活动时,再次按“搜索”按钮,然后再次返回第一个活动,导航抽屉菜单再次为空(第一次尝试添加的所有项目都被删除)。如何解决?
这是我的活动图片(抱歉质量差):
1-第一个活动;2-第一个活动中的导航抽屉,其中添加了第二个活动的列表视图中的项目;3 秒活动(带有搜索结果的列表视图)
编辑:我的代码来自两个活动(没有导入以缩短一点):第一(主要):
public class MainActivity extends ActionBarActivity {
ListviewActivity lv = new ListviewActivity();
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
public ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
ArrayList<String> arrayFav = new ArrayList<String>();
ArrayList<String> arrayLin = new ArrayList<String>();
private ImageView mImageViewLogo;
private Button mButtonSzukaj;
private EditText mEditTextPraca;
private EditText mEditTextMiejsce;
public static String nazwaStanowiska;
public static String nazwaMiejscowosci;
private Settings mSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_layout);
mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mImageViewLogo = (ImageView)findViewById(R.id.imageViewLogo);
mButtonSzukaj = (Button) findViewById(R.id.buttonSzukaj);
mEditTextPraca = (EditText)findViewById(R.id.editTextPraca);
mEditTextMiejsce = (EditText)findViewById(R.id.editTextMiejsce);
final String mPrBefore = mEditTextPraca.getText().toString();
final String mPrAfter = mPrBefore.trim();
final String mMiBefore = mEditTextMiejsce.getText().toString();
final String mMiAfter = mMiBefore.trim();
mEditTextPraca.setText(mPrAfter);
mEditTextMiejsce.setText(mMiAfter);
mSettings = new Settings(this);
mAdapter.notifyDataSetChanged();
if(mAdapter.isEmpty()){
arrayFav.add("Brak ofert");
mDrawerList.setOnItemClickListener(null);
mDrawerList.setOnItemLongClickListener(null);
}
mAdapter.notifyDataSetChanged();
mButtonSzukaj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(mEditTextPraca.getText().toString()) && (TextUtils.isEmpty(mEditTextMiejsce.getText().toString()))) {
mEditTextPraca.setError("Pole obowiązkowe!");
mEditTextMiejsce.setError("Pole obowiązkowe!");
return;
} else if (TextUtils.isEmpty(mEditTextPraca.getText().toString())) {
mEditTextPraca.setError("Pole obowiązkowe!");
return;
} else if (TextUtils.isEmpty(mEditTextMiejsce.getText().toString())) {
mEditTextMiejsce.setError("Pole obowiązkowe!");
return;
} else {
nazwaStanowiska = mEditTextPraca.getText().toString();
nazwaMiejscowosci = mEditTextMiejsce.getText().toString();
Intent myIntent = new Intent(MainActivity.this, ListviewActivity.class);
MainActivity.this.startActivityForResult(myIntent, 1);
mSettings.setmText1(mEditTextPraca.getText().toString());
mSettings.setmText2(mEditTextMiejsce.getText().toString());
mSettings.save();
}
}
});
readPreferences();
}
protected void readPreferences(){
mEditTextPraca.setText(mSettings.getmText1());
mEditTextMiejsce.setText(mSettings.getmText2());
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
ArrayList<String> passedText = data.getStringArrayListExtra("text");
ArrayList<String> passedLink = data.getStringArrayListExtra("link");
//arrayFav.clear();
//arrayLin.clear();
arrayFav.addAll(passedText);
arrayLin.addAll(passedLink);
addDrawerItems();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.podziel_sie_opinia) {
Intent mIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:661249888"));
mIntent.putExtra("sms_body", "Uważam, że aplikacja...");
startActivity(mIntent);
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void addDrawerItems() {
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayFav);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Tu pojawi się kliknięta oferta", Toast.LENGTH_LONG).show();
/*Intent myBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onet.pl"));
startActivity(myBrowserIntent);*/
Intent myBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(arrayLin.get(position)));
myBrowserIntent.putExtra("paramPosition", position);
startActivity(myBrowserIntent);
}
});
mDrawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Usunięto z ulubionych!", Toast.LENGTH_SHORT).show();
mAdapter.remove(mAdapter.getItem(position));
mAdapter.notifyDataSetChanged();
if(mAdapter.isEmpty()){
arrayFav.add("Brak ofert");
}
mAdapter.notifyDataSetChanged();
return false;
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Twoje oferty");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
protected void onResume() {
super.onResume();
mAdapter.notifyDataSetChanged();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
第二个(带有结果的列表视图):
public class ListviewActivity extends ActionBarActivity {
int global_position =0;
boolean longClick = false;
static String wybranaOferta = "";
ArrayList<String> choosedOffer = new ArrayList<String>();
ArrayList<String> choosedLink = new ArrayList<String>();
MainActivity mainActiv;
static List<String> mLista = new ArrayList<>();
static ArrayList<String> positionArr = new ArrayList<String>();
static List<String> mListaTest1 = new ArrayList<>();
static List<String> mListaTest2 = new ArrayList<>();
static List<String> mListaLinki = new ArrayList<>();
static List<String> mListaNazwy = new ArrayList<>();
static List<String> mListaFirmy = new ArrayList<>();
private JobListAdapter mAdapter;
public Elements jobName, jobName2, jobNameComp, jobName2Comp;
private ProgressBar mProgress;
private Context context;
public ArrayList<String> workList = new ArrayList<String>();
public ArrayList<String> companyList = new ArrayList<String>();
public ArrayList<String> jobList = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private JazzyListView mListView;
public String doURLpraca = MainActivity.nazwaStanowiska;
public String doURLmiejsce = MainActivity.nazwaMiejscowosci;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mListView = (JazzyListView) findViewById(R.id.list);
mListView.setTransitionEffect(new FanEffect());
mListView.setItemsCanFocus(true);
//Progress bar
mListView.setEmptyView(findViewById(R.id.progressBarLoading));
Toast.makeText(getApplicationContext(), "Wyszukiwanie ofert...", Toast.LENGTH_LONG).show();
new NewThread().execute();
mAdapter = new JobListAdapter(this, jobList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mListaLinki.get(position)));
myBrowserIntent.putExtra("paramPosition", position);
startActivity(myBrowserIntent);
}
});
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
choosedOffer.add(mListaTest1.get(position).toString());
choosedLink.add(mListaLinki.get(position).toString());
Toast.makeText(getApplicationContext(), "Dodano do ulubionych!", Toast.LENGTH_SHORT).show();
return false;
}
});
}
protected void onPreExecute() {
}
public void onBackPressed() {
Intent intent = new Intent(ListviewActivity.this, MainActivity.class);
intent.putStringArrayListExtra("text", choosedOffer);
intent.putStringArrayListExtra("link", choosedLink);
setResult(RESULT_OK, intent);
finish();
}
public class NewThread extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg) {
String doURLwork = mainActiv.nazwaStanowiska;
String doURLplace = mainActiv.nazwaMiejscowosci;
Document doc, doc2;
Elements classs, lins;
String uerele;
try {
doc = (Document) Jsoup.connect("http://www.infopraca.pl/praca?q=" + doURLwork + "&lc=" + doURLplace)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
doc2 = (Document) Jsoup.connect("http://www.pracuj.pl/praca/" + doURLwork + ";kw/" + doURLplace + ";wp")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
//Oferty
jobName = doc.select("h2.p-job-title a[href]"); //Infopraca
jobName2 = doc2.select("h2.offer__list_item_link a[href]"); //pracuj.pl
//Firmy
jobNameComp = doc.select("h3.p-name.company a[href]"); //Infopraca
jobName2Comp = doc2.select("h3.offer__list_item_link a[href]"); //pracuj.pl
//Oferty pracy
//Infopraca
mListaTest1.clear();
for (Element jobNames : jobName) {
mListaTest1.add(jobNames.text() + "\n");
}
//Pracuj.pl
for (Element jobNames2 : jobName2) {
mListaTest1.add(jobNames2.text() + "\n");
}
if(mListaTest1.size()==0){
Toast.makeText(getApplicationContext(), "Zmień parametry wyszukiwania!", Toast.LENGTH_LONG).show();
}
//--------------------------------------------------
//Firmy
//Infopraca
mListaTest2.clear();
for (Element jobNames : jobNameComp) {
mListaTest2.add(jobNames.text() + "\n");
}
//Pracuj.pl
for (Element jobNames2 : jobName2Comp) {
mListaTest2.add(jobNames2.text() + "\n");
}
//Linki do ofert
//Infopraca
for (Element link : jobName) {
mListaLinki.add(link.attr("abs:href"));
}
//Pracuj.pl
for (Element link : jobName2) {
mListaLinki.add(link.attr("abs:href"));
}
//Łączenie wyników - oferta + nazwa firmy
jobList.clear();
for(int i=0; i<mListaTest1.size(); i++){
jobList.add(mListaTest1.get(i)+"\n"+mListaTest2.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPreExecute(String result) {
}
@Override
protected void onPostExecute(String result) {
mAdapter.notifyDataSetChanged();
}
}
}