gpt4 book ai didi

java - 无法通过 Activity 更新 ListView

转载 作者:行者123 更新时间:2023-12-01 13:09:52 27 4
gpt4 key购买 nike

我创建了一个基于 listview 的应用程序,其中当我通过另一个 Activity 添加数据时,数据不会在 listview 中更新,而它应该在 ListView !!

当我在 Activity 中的保存按钮上创建 onSave 方法时,但它似乎没有在 ListView 中更新。

ma​​inactivity.java

package com.example.listviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {
TimeTrackerAdapter timeTrackerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)
findViewById(R.id.times_list);
timeTrackerAdapter = new TimeTrackerAdapter();
listView.setAdapter(timeTrackerAdapter);
}

public boolean onCreateOptionsMenu(Menu m) {
super.onCreateOptionsMenu(m);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.time_list_menu, m );
return true;
}

public static final int TIME_ENTRY_REQUEST_CODE = 1;
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.add_time_menu_item) {
Intent intent = new Intent(this, AddTimeActivity.class);
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected(item);
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TIME_ENTRY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String notes = data.getStringExtra("notes");
String time = data.getStringExtra("time");

timeTrackerAdapter.addTimeRecord( new TimeRecord(time, notes));
timeTrackerAdapter.notifyDataSetChanged();
}
}
}



}

addtimeactivity.java

package com.example.listviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class AddTimeActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_time);
}
public void onCancel(View view) {
finish();
}
public void onSave(View view) {
Intent intent = getIntent();

EditText timeView = (EditText)findViewById(R.id.time_view);
intent.putExtra("time", timeView.getText().toString());

EditText notesView = (EditText)findViewById(R.id.notes_view);
intent.putExtra("notes", notesView.getText().toString());

this.setResult(RESULT_OK, intent);
finish();
}
}

timetrackeradapter.java

    package com.example.listviewdemo;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TimeTrackerAdapter extends BaseAdapter {
private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
public TimeTrackerAdapter() {
times.add(new TimeRecord(
"38:23", "Feeling good!"));
times.add(new TimeRecord(
"49:01", "Tired. Needed more caffeine"));
times.add(new TimeRecord(
"26:21", "I’m rocking it!"));
times.add(new TimeRecord(
"29:42", "Lost some time on the hills, but pretty good."));
}

@Override
public int getCount() {
return times.size();
}

@Override
public Object getItem(int index) {
return getItem(index);
}

@Override
public long getItemId(int index) {
return index;
}

@Override
public View getView(int index, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater =
LayoutInflater.from(parent.getContext());
view = inflater.inflate(
R.layout.time_list_item, parent, false);
}
TimeRecord time = times.get(index);
TextView timeTextView = (TextView)
view.findViewById(R.id.time_view);
timeTextView.setText(time.getTime());
TextView notesTextView = (TextView)
view.findViewById(R.id.notes_view);
notesTextView.setText(time.getNotes());
return view;

}

public void addTimeRecord(TimeRecord timeRecord) {
// TODO Auto-generated method stub

}





}

最佳答案

你的问题是这一行:

public void addTimeRecord(TimeRecord timeRecord) {
// TODO Auto-generated method stub

}

你还没有真正实现这个功能,所以它不会做任何事情!我猜你想这样做:

public void addTimeRecord(TimeRecord timeRecord) {
times.add(timeRecord);
notifyDataSetChanged();
}

请注意,有了这个,您就不需要在第一个 Activity 中使用多余的 notifyDataSetChanged()

关于java - 无法通过 Activity 更新 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22972135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com