java - Listview 在 Activity 上不可见
问题描述
在单击浮动操作按钮时动态添加列表项后,我在 Activity 上看不到我的列表视图。当我单击floatingActionButton 时,会打开一个接受我输入的新活动,单击保存按钮后ReminderActivity 会打开,但未显示在列表中添加的输入。
我的动机是listView
每次从ReminderInputDataActivity获取数据后添加一个新项目,当我单击浮动操作按钮时会打开。
public class ReminderActivity extends AppCompatActivity {
private ListView lvReminder;
private FloatingActionButton floatingActionButton;
private ArrayList<Reminder> reminders;
public static final String REMINDER_INPUT = "reminder_input";
private ReminderCustomAdapter reminderListAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
reminders = new ArrayList<>();
lvReminder = (ListView) findViewById(R.id.lvReminder);
Intent getIntent = getIntent();
String[] reminderInput = getIntent.getStringArrayExtra(REMINDER_INPUT);
if (reminderInput != null) {
reminders.add(new Reminder(reminderInput[0], reminderInput[1], reminderInput[2]));
}
reminderListAdapter = new ReminderCustomAdapter(this, R.layout.listview_reminder, reminders);
lvReminder.setAdapter(reminderListAdapter);
floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ReminderActivity.this, ReminderInputDataActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
if (reminderListAdapter != null) {
reminderListAdapter.notifyDataSetChanged();
}
}
}
public class Reminder {
private String title;
private String date;
private String time;
public Reminder() {
}
public Reminder(String title, String date, String time) {
this.title = title;
this.date = date;
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
public class ReminderInputDataActivity extends AppCompatActivity {
private EditText etReminderTitle;
private Button btDate, btTime;
public static final String REMINDER_INPUT = "reminder_input";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input_reminder);
etReminderTitle = (EditText) findViewById(R.id.etReminderTitle);
btDate = (Button) findViewById(R.id.btReminderDate);
btTime = (Button) findViewById(R.id.btReminderTime);
btDate.setText(new SimpleDateFormat("E, dd MMM yyyy", Locale.getDefault()).format(new Date()));
btTime.setText(new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date()));
Button btRemSave = (Button) findViewById(R.id.btRemSave);
Button btRemCancel = (Button) findViewById(R.id.btRemCancel);
final Intent intent = new Intent(ReminderInputDataActivity.this, ReminderActivity.class);
btRemSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] userInput = new String[3];
userInput[0] = etReminderTitle.getText() != null ? etReminderTitle.getText().toString() : "";
userInput[1] = btDate.getText().toString();
userInput[2] = btTime.getText().toString();
Bundle bundle = new Bundle();
bundle.putStringArray(REMINDER_INPUT, userInput);
intent.putExtras(bundle);
startActivity(intent);
}
});
btRemCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
}
public class ReminderCustomAdapter extends BaseAdapter {
private final Context mContext;
private int resource;
private ArrayList<Reminder> reminderList;
public ReminderCustomAdapter(Context mContext, int resource, ArrayList<Reminder> reminderList) {
this.mContext = mContext;
this.resource = resource;
this.reminderList = reminderList;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(resource, null);
TextView tvInputRemTitle = (TextView) view.findViewById(R.id.tvInputRemTitle);
TextView tvInputRemDate = (TextView) view.findViewById(R.id.tvInputRemDate);
TextView tvInputRemTime = (TextView) view.findViewById(R.id.tvInputRemTime);
Reminder reminder = reminderList.get(position);
tvInputRemTitle.setText(reminder.getTitle());
tvInputRemDate.setText(reminder.getDate());
tvInputRemTime.setText(reminder.getTime());
return view;
}
}
活动提醒.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lvReminder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:backgroundTint="@color/colorPrimaryDark"
android:contentDescription="@string/reminder"
android:src="@android:drawable/ic_input_add"
app:shapeAppearanceOverlay="@style/fab_3_rounded" />
</RelativeLayout>
listview_reminder.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tvInputRemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Title"
android:textSize="15sp" />
<TextView
android:id="@+id/tvInputRemDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvInputRemTitle"
android:hint="Date" />
<TextView
android:id="@+id/tvInputRemTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvInputRemTitle"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tvInputRemDate"
android:hint="Time" />
</RelativeLayout>
我是 android 新手并开始创建一个基本项目,对于上述问题,我观看了一些视频并浏览了文章,但无法在这里找到问题。
请帮忙。提前致谢!
解决方案
问题是由于此代码
final Intent intent = new Intent(ReminderInputDataActivity.this, ReminderActivity.class);
btRemSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] userInput = new String[3];
userInput[0] = etReminderTitle.getText() != null ? etReminderTitle.getText().toString() : "";
userInput[1] = btDate.getText().toString();
userInput[2] = btTime.getText().toString();
Bundle bundle = new Bundle();
bundle.putStringArray(REMINDER_INPUT, userInput);
intent.putExtras(bundle);
startActivity(intent);
}
});
在这里,您ReminderActivity
再次开始调用onCreate
此活动的方法,您在其中添加reminders = new ArrayList<>();
了重新创建没有数据的数组列表。为避免这种情况,您可以实施两种解决方案。
- 实施
interface
。 StartActivityForResult
.
最简单的解决方案是StartActivityForResult
. 例如,
Intent intent = new Intent(ReminderActivity.this,ReminderInputDataActivity.class);
startActivityForResult(intent, 2);
and in the same activity implement the callback.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
从 ReminderInputDataActivity 像这样将数据发送回此类。
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
推荐阅读
- nvidia-deepstream - deepstream sdk 可以在 GTX Geforce nvidia 显卡上工作吗?
- xml - 两个无界源变量到单个无界目标变量的映射
- django - 表单未保存对话框模式(弹出)Django中的更改
- swift - Swift macOS 无法关闭打印对话框
- ruby-on-rails - 如何使用 Ruby On Rails 连接到 Microsoft Azure Key Vault 获取令牌并从 Vault 中读取值
- c - linux内核socket源宏container_of
- c# - 是否可以在使用本机视图的 Xamarin 应用程序中拥有一致的 NavBar 和 Footer?
- python - 安装 Pillow 后无法导入 PIL - venv 下的 Django 项目
- java - 如何将 botbuilder-java 导入现有的 Spring Boot 应用程序?
- asp.net - 使用没有角色的用户呈现 SelectList