我目前遇到问题,找不到太多帮助。
我的意图是编写用于调用两个类的onbackpressed
方法。这里的问题是这是一个ListView
,所以我在ListView中调用后退按钮,效果很好。
当用户按下引用日历名称的任何按钮时,将读取CSV文件,并显示格式。从那里,当用户单击“后退”按钮时,将显示一个或多个List_Event,这是完全合适的。
但是,从ListView
本身开始,我需要回到我的HomePage
,并且我对如何进行此操作一无所知。 Ť
在List_Event中添加onpressed的真正原因是有一个ItemArrayAdapter可以读取文件,因此在该逻辑中定义onpressedback按钮没有用。
这是班级:List_Event.java
package com.example.dell.display_event;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import java.io.InputStream;
import java.util.List;
public class List_Event extends AppCompatActivity implements View.OnClickListener {
// private List<Button> buttons;
private ListView listView;
private ItemArrayAdapter itemArrayAdapter;
private static String file_name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_event);
Button january = (Button) findViewById(R.id.January);
january.setOnClickListener(this); // calling onClick() method
Button february = (Button) findViewById(R.id.February);
february.setOnClickListener(this); // calling onClick() method
Button march = (Button) findViewById(R.id.March);
march.setOnClickListener(this); // calling onClick() method
Button april = (Button) findViewById(R.id.April);
april.setOnClickListener(this);
Button may=(Button) findViewById(R.id.May);
may.setOnClickListener(this);
Button june=(Button) findViewById(R.id.June);
june.setOnClickListener(this);
Button july=(Button) findViewById(R.id.July);
july.setOnClickListener(this);
Button august=(Button) findViewById(R.id.August);
august.setOnClickListener(this);
Button september=(Button) findViewById(R.id.September);
september.setOnClickListener(this);
Button october=(Button) findViewById(R.id.October);
october.setOnClickListener(this);
Button november=(Button) findViewById(R.id.November);
november.setOnClickListener(this);
Button december=(Button) findViewById(R.id.December);
december.setOnClickListener(this);
listView=(ListView) findViewById(R.id.listView);
itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.item_layout);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(itemArrayAdapter);
listView.onRestoreInstanceState(state);
}
@Override
public void onClick(View v) {
Button january = (Button) findViewById(R.id.January);
january.setOnClickListener(this); // calling onClick() method
Button february = (Button) findViewById(R.id.February);
february.setOnClickListener(this); // calling onClick() method
Button march = (Button) findViewById(R.id.March);
march.setOnClickListener(this); // calling onClick() method
Button april = (Button) findViewById(R.id.April);
april.setOnClickListener(this);
Button may=(Button) findViewById(R.id.May);
may.setOnClickListener(this);
Button june=(Button) findViewById(R.id.June);
june.setOnClickListener(this);
Button july=(Button) findViewById(R.id.July);
july.setOnClickListener(this);
Button august=(Button) findViewById(R.id.August);
august.setOnClickListener(this);
Button september=(Button) findViewById(R.id.September);
september.setOnClickListener(this);
Button october=(Button) findViewById(R.id.October);
october.setOnClickListener(this);
Button november=(Button) findViewById(R.id.November);
november.setOnClickListener(this);
Button december=(Button) findViewById(R.id.December);
december.setOnClickListener(this);
switch (v.getId()) {
case R.id.January:
InputStream inputStream_january = getResources().openRawResource(R.raw.january);
CSVFile csvFile_january = new CSVFile(inputStream_january);
List<String[]> Event_List_january = csvFile_january.read();
for (String[] Event_Data_january : Event_List_january) {
itemArrayAdapter.add(Event_Data_january);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.February:
InputStream inputStream_february = getResources().openRawResource(R.raw.february);
CSVFile csvFile_february = new CSVFile(inputStream_february);
List<String[]> Event_List_february = csvFile_february.read();
for (String[] Event_Data_february : Event_List_february) {
itemArrayAdapter.add(Event_Data_february);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.March:
InputStream inputStream_march = getResources().openRawResource(R.raw.march);
CSVFile csvFile_march = new CSVFile(inputStream_march);
List<String[]> Event_List_march = csvFile_march.read();
for (String[] Event_Data_march : Event_List_march) {
itemArrayAdapter.add(Event_Data_march);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.April:
InputStream inputStream_april = getResources().openRawResource(R.raw.april);
CSVFile csvFile_april = new CSVFile(inputStream_april);
List<String[]> Event_List_april = csvFile_april.read();
for (String[] Event_Data_april : Event_List_april) {
itemArrayAdapter.add(Event_Data_april);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.May:
InputStream inputStream_may = getResources().openRawResource(R.raw.may);
CSVFile csvFile_may = new CSVFile(inputStream_may);
List<String[]> Event_List_may = csvFile_may.read();
for (String[] Event_Data_may : Event_List_may) {
itemArrayAdapter.add(Event_Data_may);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.June:
InputStream inputStream_june = getResources().openRawResource(R.raw.june);
CSVFile csvFile_june = new CSVFile(inputStream_june);
List<String[]> Event_List_june = csvFile_june.read();
for (String[] Event_Data_june : Event_List_june) {
itemArrayAdapter.add(Event_Data_june);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.July:
InputStream inputStream_july = getResources().openRawResource(R.raw.july);
CSVFile csvFile_july = new CSVFile(inputStream_july);
List<String[]> Event_List_july = csvFile_july.read();
for (String[] Event_Data_july : Event_List_july) {
itemArrayAdapter.add(Event_Data_july);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.August:
InputStream inputStream_august = getResources().openRawResource(R.raw.august);
CSVFile csvFile_august = new CSVFile(inputStream_august);
List<String[]> Event_List_august = csvFile_august.read();
for (String[] Event_Data_august : Event_List_august) {
itemArrayAdapter.add(Event_Data_august);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.September:
InputStream inputStream_september = getResources().openRawResource(R.raw.september);
CSVFile csvFile_september = new CSVFile(inputStream_september);
List<String[]> Event_List_september = csvFile_september.read();
for (String[] Event_Data_september : Event_List_september) {
itemArrayAdapter.add(Event_Data_september);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.October:
InputStream inputStream_october = getResources().openRawResource(R.raw.october);
CSVFile csvFile_october = new CSVFile(inputStream_october);
List<String[]> Event_List_october = csvFile_october.read();
for (String[] Event_Data_october : Event_List_october) {
itemArrayAdapter.add(Event_Data_october);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.November:
InputStream inputStream_november = getResources().openRawResource(R.raw.november);
CSVFile csvFile_november = new CSVFile(inputStream_november);
List<String[]> Event_List_november = csvFile_november.read();
for (String[] Event_Data_november : Event_List_november) {
itemArrayAdapter.add(Event_Data_november);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
case R.id.December:
InputStream inputStream_december = getResources().openRawResource(R.raw.december);
CSVFile csvFile_december = new CSVFile(inputStream_december);
List<String[]> Event_List_december = csvFile_december.read();
for (String[] Event_Data_december : Event_List_december) {
itemArrayAdapter.add(Event_Data_december);
}
january.setVisibility(View.GONE);
february.setVisibility(View.GONE);
march.setVisibility(View.GONE);
april.setVisibility(View.GONE);
may.setVisibility(View.GONE);
june.setVisibility(View.GONE);
july.setVisibility(View.GONE);
august.setVisibility(View.GONE);
september.setVisibility(View.GONE);
october.setVisibility(View.GONE);
november.setVisibility(View.GONE);
december.setVisibility(View.GONE);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search, menu);
return true;
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(List_Event.this, List_Event.class));
finish();
}
}
ItemArrayAdapter.java
package com.example.dell.display_event;
import android.content.Context;
import android.content.Intent;
import android.icu.text.StringSearch;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.widget.Button;
public class ItemArrayAdapter extends ArrayAdapter<String[]> {
private List<String[]> Event_List = new ArrayList<String[]>();
static class ItemViewHolder {
TextView Days;
TextView Events;
}
public ItemArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public void add(String[] object) {
Event_List.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.Event_List.size();
}
@Override
public String[] getItem(int index) {
return this.Event_List.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.item_layout, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.Days = (TextView) row.findViewById(R.id.Days);
viewHolder.Events = (TextView) row.findViewById(R.id.Events);
row.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder)row.getTag();
}
String[] day = getItem(position);
viewHolder.Days.setText(day[0]);
viewHolder.Events.setText(day[1]);
return row;
}
}
点击日历按钮后,效果很好
在这种情况下,后退按钮可以正常工作。