异步和跳过帧



所以我遇到了一个问题,我在列表视图中单击了一个项目,它做了一些额外的操作,然后继续我的下一个活动。我遇到一个问题,我的应用程序进入黑屏,控制台告诉我

I/Choreographer:跳过了630帧!应用程序可能在其主线程上做了太多的工作。

我试过阅读Async,但我不知道如何在代码中使用它。所以我发布了我的主要活动,因为这是在没有与第二个活动交互的情况下发生的。

public class MainActivity extends AppCompatActivity {
    ListView ListofList;
    ArrayList<String> Lists;
    ArrayAdapter<String> ListAdapter;
    Button button;
    ArrayList<String> Subject = new ArrayList<>();
    int Size = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListofList = findViewById(R.id.LofL);
        Lists = new ArrayList<String>();
        ListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Lists);
        ListofList.setAdapter(ListAdapter);
        button = findViewById(R.id.AddList);
        Lists.add("Test");
        ListofList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getBaseContext(), ItemList.class);
                String s = (String) ListofList.getItemAtPosition(position);
                i.putExtra("Title", s);
                i.putExtra("size", Size);
                int a = 0;
                for (String t : Subject) {
                    i.putExtra("Item" + a, t);
                    a++;
                }
                startActivityForResult(i, 2);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getBaseContext(), CreateItem.class);
                String message = "Please enter the name of the list below.";
                i.putExtra("mess", message);
                startActivityForResult(i, 1);
            }
        });
    }
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        Bundle extras = getIntent().getExtras();
        if (data == null) {
        }
        if (data != null) {
            if (requestCode == 1) {
                String item = (data.getStringExtra("item"));
                Lists.add(item);
                ListAdapter.notifyDataSetChanged();
            }
        }
        if (requestCode == 2) {
            ArrayList<String> subject = new ArrayList<String>();
            int i = 0;
            assert data != null;
            int size = (data.getIntExtra("ListSize", 0));
            Size = size;
            while (i <= size) {
                String item = (data.getStringExtra("item" + i));
                subject.add(item);
                i++;
            }
            Subject = subject;
        }
    }
}

应Ben p.的要求

public class ItemList extends AppCompatActivity {
    static ArrayList<String> customItems;
    ArrayAdapter<String> adapter;
    ListView lvItems;
    Button button;
    Button button2;
    ArrayAdapter<String> trashAdapter;
    ArrayList<String> trash;
    ListView itemtrash;
    static TextView Title;
    String Tit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        Title = findViewById(R.id.Title);
        //set up Item List
        customItems = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, customItems);
        lvItems = (ListView)  findViewById(R.id.lvItems);
        //Set up trash List
        trash = new ArrayList<String>();
        trashAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, trash);
        itemtrash = findViewById(R.id.itemtrash);
        //Initialize Buttons
        button = findViewById(R.id.AddItem);
        button2 = findViewById(R.id.ClearTrash);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            ((TextView)findViewById(R.id.Title)).setText(extras.getString("Title"));
            Tit = extras.getString("Title");
            int i = 0;
            int size = (extras.getInt("size", 0));
            while (i <= size) {
                String item = (extras.getString("Item" + i));
                if (item == null) {
                    continue;
                }
                customItems.add(item);
                i++;
            }
        }
        //CharSequence Titlefilter = Title.getText();
        //lvItems.setTextFilterEnabled(true);
        //ItemList.this.adapter.getFilter().filter(Titlefilter);
        //adapter.notifyDataSetChanged();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getBaseContext(), CreateItem.class);
                String message = "Please enter new item below.";
                i.putExtra("mess", message);
                startActivityForResult(i, 1);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                trashAdapter.clear();
                trashAdapter.notifyDataSetChanged();
            }
        });
        lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.getItem(position);
                trashAdapter.add(s);
                adapter.remove(s);
                assert s != null;
                adapter.notifyDataSetChanged();
                trashAdapter.notifyDataSetChanged();
            }
        });
        itemtrash.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = trashAdapter.getItem(position);
                customItems.add(s);
                trash.remove(s);
                adapter.notifyDataSetChanged();
                trashAdapter.notifyDataSetChanged();
            }
        });
        lvItems.setAdapter(adapter);
        itemtrash.setAdapter(trashAdapter);
    }
    @Override
    public void onBackPressed() {
        Intent i = new Intent();
        int a = 0;
            for (String s : customItems){
                String t = adapter.getItem(a);
                i.putExtra("item" + a, t);
                a++;
            }
        int size = customItems.size();
        i.putExtra("ListSize", size);
        setResult(2, i);
        finish();
    }
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        Bundle extras = getIntent().getExtras();
        if (data == null) {
        }
        if (data != null) {
            String item = (data.getStringExtra("item"));
            String title = Tit;
            customItems.add(title + "nn" + item);
            adapter.notifyDataSetChanged();
        }
    }
}

我想明白了。在ItemList活动中,我意外地用进行了一个无限循环。

相关内容

  • 没有找到相关文章

最新更新