从ListView添加光标数据并存储在数组中

时间:2019-03-08 02:58:34

标签: java android listview android-listview android-cursor

不好意思。我有一个用名称填充的listView。我要做的是从listView中获取这些名称,并将其存储在Array中,以便我能够随机选择和显示这些名称。但是我希望能够从另一个活动访问此数组。我可以使用已设置的游标执行此操作吗?如果是这样,怎么办?我一直在搜索并获得一些信息,但是我仍然感到困惑。

listView.java

    public class ListView extends AppCompatActivity {
    private static final String TAG = "ListView";

    private EditText editText;
    private android.widget.ListView listView;

    DatabaseHelper mDatabaseHelper;
    Button btnAdd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        mDatabaseHelper = new DatabaseHelper(this);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        editText = (EditText) findViewById(R.id.editText);
        listView = (android.widget.ListView) findViewById(R.id.lv);


        ArrayList<String> list = getIntent().getStringArrayListExtra("myList");

        android.widget.ListView lv = (android.widget.ListView) findViewById(R.id.lv);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        lv.setAdapter(adapter);


        //Takes user back to the main activity after clicking on back arrow
        ImageView ivBack = (ImageView) findViewById(R.id.ivBackArrow);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: pressed back arrow");
                Intent intent = new Intent(ListView.this, MainActivity.class);
                startActivity(intent);
            }
        });

        //Adds new hashtag to list and prompts if nothing is entered
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newEntry = editText.getText().toString();

                if (editText.length() != 0) {
                    addData(newEntry);
                    editText.setText("");
                } else {
                    toastMessage("you must put something in the text field");
                }
            }
        });

        populateListView();
    }

    /**
     * Adds new data into the Database
     * @param newEntry
     */
    public void addData(String newEntry) {
        boolean insertData = mDatabaseHelper.addData(newEntry);

        if (insertData) {
            toastMessage("Successfully inserted");
            recreate();
        } else {
            toastMessage("Whoops, something went wrong");
        }
    }

    /**
     * Default toastMessage
     * @param message
     */
    private void toastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * Populate listView with data and create listener to navigate to editDeleteList
     */
    private void populateListView() {
        Log.d(TAG, "populateListView: displaying data in the listview");

        //get data and append to list
        Cursor data = mDatabaseHelper.getData();
        ArrayList<String> listData = new ArrayList<>();
        while(data.moveToNext()) {
            //get the value from the database in column 1
            //set it to the arraylist
            listData.add(data.getString(1));
        }
        //create arraylist and set it to the adapter
        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
        listView.setAdapter(adapter);

        //set onclick listen to edit activity
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String name = adapterView.getItemAtPosition(position).toString();
                Log.d(TAG, "onItemClick: you clicked on " + name);

                Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
                int itemID = -1;
                while (data.moveToNext()) {
                    itemID = data.getInt(0);
                }
                if (itemID > -1) {
                    Log.d(TAG, "onItemID: the ID is: " + itemID);
                    Intent editScreenIntent = new Intent(ListView.this, EditDeleteList.class);
                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                } else {
                    toastMessage("No ID found");
                }
            }
        });
    }

    /**
     * Updates the listView after navigating back from EditDeleteList activity
     */
    @Override
    protected void onResume() {
        super.onResume();
        populateListView();
    }
}

0 个答案:

没有答案