简体   繁体   English

Android,如何动态更新 ListView 项和所有后续项?

[英]Android, how do I dynamically update a ListView item and all subsequent items?

I have a ListView that displays an ArrayList that is dynamically created using an adapter.我有一个 ListView,它显示一个使用适配器动态创建的 ArrayList。 However, certain elements of each list item view are calculated based on previous item values.但是,每个列表项视图的某些元素是根据以前的项值计算的。 I am using Intents to open another activity where the user can edit a selected list item, and the updates are passed back to the main activity.我正在使用 Intents 打开另一个活动,用户可以在其中编辑选定的列表项,并将更新传递回主活动。 In the main activity I've placed the getIntent, and the associated setters, after the ArrayList is generated and before the adapter.在主要活动中,我将 getIntent 和相关的 setter 放在生成 ArrayList 之后和适配器之前。 When the main activity is first created the adapter correctly calculates all list view items.首次创建主要活动时,适配器会正确计算所有列表视图项。 But when the user accepts updates in the edit activity and returns to the main activity, only the selected list item is updated.但是当用户在编辑活动中接受更新并返回主活动时,只会更新选定的列表项。 Having the entire list cycle through and update would be fine (it will never be a very long list), but I'm a little surprised that only the selected list item is getting updated.让整个列表循环并更新会很好(它永远不会是一个很长的列表),但我有点惊讶只有选定的列表项会得到更新。 I expected that either the adapter would run as it does when the activity is first created and all items would get updated, or that it wouldn't run at all and none would get updated.我希望适配器会像第一次创建活动时那样运行并且所有项目都将得到更新,或者它根本不会运行并且没有任何项目会得到更新。

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);
                }
            }
        );
    }
}

Update... I've added the mainActivity code.更新...我已经添加了 mainActivity 代码。 It creates an ArrayList plan (I'm using a BuildPlan method to populate a dummy plan while I'm developing) then checks for an intent that is returning an updated plan step.它创建一个 ArrayList 计划(我在开发时使用 BuildPlan 方法来填充虚拟计划)然后检查返回更新计划步骤的意图。 If an intent exists the specified step is updated in the plan.如果存在意图,则在计划中更新指定的步骤。 The list adapter is then created and set.然后创建并设置列表适配器。 Finally the clickListener is created and set.最后创建并设置 clickListener。

I've done something relatively similar but I used dynamic spinners and listviews from a database.我做了一些相对相似的事情,但我使用了数据库中的动态微调器和列表视图。

Here is the code.这是代码。 Basically you invalidate the list view, reset the data and call notifyDataSetChanged() on the adapter.基本上,您使列表视图无效,重置数据并在适配器上调用 notifyDataSetChanged()。

public  ListView lv;
Spinner suburbSpinner;
ArrayAdapter<String> suburbAdapter;
ArrayList<String> suburbs = new ArrayList<>();
ArrayList<Resource> resources = new ArrayList<>();
ArrayAdapter<Resource> arrayAdapter;

public void updateList(String type, String suburb, String businessType, int suburbPos) {
    DatabaseHelper db = new DatabaseHelper(this, "fairCanberraDB", null, 1);
    lv = findViewById(R.id.list);

    // Reset suburb spinner, get new list view resources.

    lv.invalidateViews();
    resources = db.resourceQuery(type, suburb, businessType);
    System.out.println("resource: " + resources);
    ArrayList<Resource> suburbQuery = db.resourceQuery(type, "All", businessType);
    Spinner suburbSpinner = findViewById(R.id.suburbSpinner);
    suburbs.clear();
    suburbs.add("All");

    for (int x = 0; x < suburbQuery.size(); x++) {
        if (suburbs.contains(suburbQuery.get(x).getSuburb())) {
            continue;
        } else {
            suburbs.add(suburbQuery.get(x).getSuburb());
        }
    }
    db.close();
    suburbAdapter.notifyDataSetChanged();
   arrayAdapter.notifyDataSetChanged();
    ArrayAdapter<Resource> arrayAdapter = new ArrayAdapter<Resource>(
            this,
            android.R.layout.simple_list_item_1,
            resources);

    lv.setAdapter(arrayAdapter);

    if(suburbPos < suburbSpinner.getCount())
    { suburbSpinner.setSelection(suburbPos);}
    setSpinnerListener(suburbSpinner);


}

// Register listener for a spinner
public void setSpinnerListener(Spinner spinner)
{
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            Spinner typeSpinner = (Spinner) findViewById(R.id.typeSpinner);
            Spinner businessTypeSpinner = (Spinner) findViewById(R.id.businessTypeSpinner);
            Spinner suburbSpinner = (Spinner) findViewById(R.id.suburbSpinner);
            String businessType = businessTypeSpinner.getSelectedItem().toString();
            if(businessType.contains("Private"))
            {
                businessType = "private user";
            }

            updateList(typeSpinner.getSelectedItem().toString(), suburbSpinner.getSelectedItem().toString(),
                    businessType, suburbSpinner.getSelectedItemPosition());


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM