gpt4 book ai didi

java - Android,如何从外部 Activity 更新ListView内容?

转载 作者:行者123 更新时间:2023-12-01 17:51:50 25 4
gpt4 key购买 nike

我有一个包含 4 个类的项目:2 个 Activity 、1 个适配器和 1 个简单对象类。类的名称为:MainActivityAddingItemsActivityItemAdapterSimpleItem。在与MainActivity对应的布局中,有一个名为SimpleListView的 ListView 。 SimpleListView 应包含 SimpleItem 对象。 ItemAdapter 用于处理SimpleListView。从 MainActivity 更新、添加项目到 SimpleListView 非常容易。我想要达到的是从 AddingItemsActivity 更新、添加出现在 SimpleListView 上的项目(当用户返回到 MainActivity 时出现) )。你能告诉我我应该做什么才能达到这个目标吗?

PS:我想问:“如何从AddingItemsActivity更新SimpleListView?”但我读到这不是正确的问题,因为 SimpleListView 不存在于 AddingItemsActivity 中。

主要 Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button toAddingActivity = findViewById(R.id.toAddingActivitybutton);
final ListView simpleItemsListView = findViewById(R.id.SimpleListView);
final ItemAdapter mAdapter = new ItemAdapter(this, R.layout.simple_item_adapter);


toAddingActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), AddingItemsActivity.class);
startActivity(intent);
}
});
}
}

添加项目 Activity

public class AddingItemsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adding_items);
Button addItem = findViewById(R.id.AddItembutton);
// final ListView simpleItemsListView = findViewById(R.id.SimpleListView);
// final ItemAdapter mAdapter = new ItemAdapter(this, R.layout.simple_item_adapter);



addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// first reflex try, which does not work:

// SimpleItem item = new SimpleItem("String number 1", "String number 2");
// mAdapter.add(item);
// simpleItemsListView.setAdapter(mAdapter);
}
});
}
}

项目适配器

public class ItemAdapter extends ArrayAdapter<SimpleItem>{
public ItemAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ItemAdapter(Context context, int resource, List<SimpleItem> items) {
super(context, resource, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.simple_item_adapter, null);
}

SimpleItem item = getItem(position);

if (item != null) {
TextView tv1 = v.findViewById(R.id.adapterTextView1);
TextView tv2 = v.findViewById(R.id.adapterTextView2);

tv1.setText(item.getStr1());
tv2.setText(item.getStr2());
}
return v;
}
}

简单项目

public class SimpleItem {
private String str1;
private String str2;


public SimpleItem(String s1, String s2)
{
str1 = s1;
str2 = s2;
}

public String getStr1()
{
return str1;
}


public String getStr2()
{
return str2;
}


}

最佳答案

  1. 使用startActivityForResultAddingItemsActivity获取SimpleItem结果。

    Intentintent = new Intent(getBaseContext(),AddingItemsActivity.class);
    startActivityForResult(intent,1);

  2. AddingItemsActivity中创建一个SimpleItem,向其添加值并使用setResult将简单项目实例返回给MainActivity

    // inside on click
    SimpleItem item = new SimpleItem("String number 1", "String number 2");
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", item);
    setResult(Activity.RESULT_OK,returnIntent);
    finish();`
  3. 在主 Activity 中

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
    SimpleItem result = (SimpleItem)data.getSerializableExtra("result");
    // add result to the list, used by adapter
    // notify adapter using notifyDataSetChanged
    }
    }
    }//onActivityResult

注意:添加public class SimpleItem Implements Serialized并且似乎您忘记创建列表并将其传递给适配器实例,因此只需创建它即可

关于java - Android,如何从外部 Activity 更新ListView内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49286646/

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