【发布时间】:2020-08-26 03:04:41
【问题描述】:
我有一个 ListView,它显示一个使用适配器动态创建的 ArrayList。但是,每个列表项视图的某些元素是根据以前的项值计算的。我正在使用 Intents 打开另一个活动,用户可以在其中编辑选定的列表项,并将更新传递回主活动。在主要活动中,我将 getIntent 和相关的设置器放置在 ArrayList 生成之后和适配器之前。首次创建主要活动时,适配器会正确计算所有列表视图项。但是当用户在编辑活动中接受更新并返回主活动时,只有选定的列表项被更新。让整个列表循环并更新会很好(它永远不会是一个很长的列表),但我有点惊讶的是只有选定的列表项得到更新。我希望适配器会像首次创建活动时那样运行并且所有项目都会更新,或者它根本不会运行并且不会更新。
public class MainActivity extends AppCompatActivity {
private final Context thisContext = MainActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView planListView = findViewById(R.id.plan_listview);
final ArrayList<ItemProfile> planSteps = BuildPlan();
if(getIntent().getExtras() != null)
{
int stepNumber = getIntent().getIntExtra("stepNumber", 0);
ItemProfile thisStep = (ItemProfile) getIntent().getSerializableExtra("itemProfile");
planSteps.get(stepNumber-1).setDepth(thisStep.getDepth());
planSteps.get(stepNumber-1).setTime(thisStep.getTime());
planSteps.get(stepNumber-1).setInterval(thisStep.getInterval());
}
ItemsListAdapter planAdapter = new ItemsListAdapter(this, planSteps);
planListView.setAdapter(planAdapter);
planListView.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l)
{
int index = pos-1;
Intent i = new Intent(thisContext, EditItemActivity.class);
i.putExtra("stepNumber", pos);
i.putExtra("stepProfile", planSteps.get(index));
if (index > 0)
{
i.putExtra("groupStart", planSteps.get(index-1).getGroupEnd());
}
startActivity(i);
}
}
);
}
}
更新...我添加了 mainActivity 代码。它创建一个 ArrayList 计划(我在开发时使用 BuildPlan 方法填充一个虚拟计划),然后检查返回更新计划步骤的意图。如果存在意图,则在计划中更新指定的步骤。然后创建并设置列表适配器。最后创建并设置了 clickListener。
【问题讨论】:
-
你能添加一些sn-p,可以帮助人们快速理解问题吗?