尝试在Android中更新我的listView时,我的适配器出现问题。我在堆栈上遇到了类似的问题,但仍然没有弄清楚。
我正在开发一种Bucket List应用程序,我开始一项新活动,以便将愿望添加到愿望列表中。它只是一个用于学习目的的原型,我尝试在没有异步解决方案的情况下,只需使用adapter.add(“用户插入的愿望”),但应用程序只是崩溃,或者甚至没有启动。我提到我已经检查了发送消息的活动的功能(我只是显示它,你可以在 onActivityResult 上看到)。此外,下面提供的示例代码也可以使用,因为我已经注释了问题代码,但没有向listView添加任何内容。
那么为什么我认为它不起作用(如果我使用注释代码):如果我在 onCreate 中声明我的适配器,它在 onActivityResult中不可见我无法使用添加的方法,也不能使用适配器,如果我在外面声明我的适配器,它只会崩溃/无法启动应用程序。
package com.example.packagename.reforgedbucketlist;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import static com.example.beatriceghetel.reforgedbucketlist.R.styleable.View;
public class MainActivity extends AppCompatActivity {
public String logMsg = "State";
String[] wishesArray = {"Preset wish: use this app!","Preset wish: enjoy this app!","Preset wish: rate this app!","Preset wish: recommend this app!"};
/*ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, wishesArray);
ListView listView = (ListView) findViewById(R.id.wishes_list);*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, wishesArray);
ListView listView = (ListView) findViewById(R.id.wishes_list);
listView.setAdapter(adapter);
Log.d(logMsg, "The onCreate() event");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_items, menu);
MenuItem item1 = (MenuItem) findViewById(R.id.about_id);
MenuItem item2 = (MenuItem) findViewById(R.id.settings_id);
MenuItem item3 = (MenuItem) findViewById(R.id.add_wish_id);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onStart() {
super.onStart();
Log.d(logMsg, "The onStart() event");
}
@Override
protected void onResume() {
super.onResume();
Log.d(logMsg, "The onResume() event");
}
@Override
protected void onPause() {
super.onPause();
Log.d(logMsg, "The onPause() event");
}
@Override
protected void onStop() {
super.onStop();
Log.d(logMsg, "The onStop() event");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(logMsg, "The onDestroy() event");
}
public void onAddWishClick(MenuItem item) {
Intent getNewWishIntent = new Intent(this, AddWishActivity.class);
final int result = 1;
getNewWishIntent.putExtra("CallingActivity", "Wish");
startActivityForResult(getNewWishIntent, result);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
TextView userWishView = (TextView) findViewById(R.id.message_sent_back);
String wishSentBack = data.getStringExtra("UserWish");
userWishView.append(" " + wishSentBack);
//adapter.add(wishSentBack);
//adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:1)
问题是您使用的是具有固定大小的数组。
尝试将声明更改为:
public class MainActivity extends AppCompatActivity {
public String logMsg = "State";
List<String> list = new ArrayList<>();
String[] wishesArray = {"Preset wish: use this app!","Preset wish: enjoy this app!","Preset wish: rate this app!","Preset wish: recommend this app!"};
// On create ....{
list.addAll(Arrays.asList(wishesArray ));
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, list);
//.... do the rest normally
}
当您需要更新数据时:
list.add(wishSentBack);
adapter.notifyDataSetChanged();