listview / adapterview在所有字段中仅显示1个值

时间:2018-09-22 11:25:54

标签: java android

我正在构建我的第一个应用程序,但是我遇到了一段代码的麻烦。 添加值(在本例中为配方)后,将保存名称和配方本身。 该名称在正在运行的列表视图中列出。 但是,当我单击要显示/编辑/删除的值时,它还会在配方字段中显示名称。 有人可以帮助我/为我指明正确的方向吗?

代码仍然混乱:

我的食谱列表视图     公共类食谱扩展了AppCompatActivity {

private static final String TAG = "Recipes";

DatabaseRecipes mDatabaseRecipes;
ArrayList<RecipeHelper> recipeList;
ListView mListView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipes);
    recipeList = new ArrayList<>();
    mListView = (ListView) findViewById(R.id.listView);
    mDatabaseRecipes = new DatabaseRecipes(this);
    Cursor data = mDatabaseRecipes.getData();

    populateListView();

    Button button_add = (Button) findViewById(R.id.button_add);

    button_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick: Continue to adding Recipes");
            toastMessage("Continue to adding Recipes");
            Intent intent = new Intent(Recipes.this, AddRecipe.class);
            startActivity(intent);
        }
    });
}

private void populateListView() {
    Log.d(TAG, "populateListView: Displaying data in the ListView.");

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

    //set an onItemClickListener to the ListView
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String name = adapterView.getItemAtPosition(i).toString();
            String recipe = adapterView.getItemAtPosition(i).toString();
            Log.d(TAG, "onItemClick: You Clicked on " + name);

            Cursor data = mDatabaseRecipes.getData(); //get the id associated with that name
            int itemID = -1;
            while(data.moveToNext()){
                itemID = data.getInt(0);
            }
            if(itemID > -1){
                Log.d(TAG, "onItemClick: The ID is: " + itemID);
                Intent editScreenIntent = new Intent(Recipes.this, EditRecipe.class);
                editScreenIntent.putExtra("id",itemID);
                editScreenIntent.putExtra("name",name);
                editScreenIntent.putExtra("recipe",recipe);
                startActivity(editScreenIntent);
            }
            else{
                toastMessage("No ID associated with that ingredient");
            }
        }
});
}

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

我的编辑配方:     公共类EditRecipe扩展了AppCompatActivity {

private static final String TAG = "EditRecipe";

private Button button_edit,button_delete,button_back;
public EditText editRecipe, editRecipeName;

DatabaseRecipes mDatabaseRecipe;

private String selectedName, selectedRecipe;
private int selectedID;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_recipe);
    button_edit = (Button) findViewById(R.id.button_edit);
    button_delete = (Button) findViewById(R.id.button_delete);
    button_back = (Button) findViewById(R.id.button_back);
    editRecipeName = (EditText) findViewById(R.id.editRecipeName);
    editRecipe = (EditText) findViewById(R.id.editRecipe);
    mDatabaseRecipe = new DatabaseRecipes(this);

    //get the intent extra from the ListDataActivity
    Intent receivedIntent = getIntent();

    //now get the itemID we passed as an extra
    selectedID = receivedIntent.getIntExtra("id",-1); //NOTE: -1 is just the default value

    //now get the name we passed as an extra
    selectedName = receivedIntent.getStringExtra("name");

    //now get the name we passed as an extra
    selectedRecipe = receivedIntent.getStringExtra("recipe");

    //set the text to show the current selected name
    editRecipeName.setText(selectedName);

    //set the text to show the current selected name
    editRecipe.setText(selectedRecipe);

    button_edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String item = editRecipeName.getText().toString();
            String item2 = editRecipe.getText().toString();
            if(!item.equals("") && !item2.equals("")){
                mDatabaseRecipe.updateName(item,selectedID,selectedName);
                mDatabaseRecipe.updateRecipe(item2,selectedID,selectedRecipe);
                Intent int1 = new Intent(EditRecipe.this, Recipes.class);
                startActivity(int1);
            }else{
                toastMessage("You must enter a name");
            }
        }
    });

    button_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabaseRecipe.deleteName(selectedID,selectedName);
            editRecipeName.setText("");
            toastMessage("removed from database");
            Intent int1 = new Intent(EditRecipe.this, Recipes.class);
            startActivity(int1);
        }
    });

    button_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent int1 = new Intent(EditRecipe.this, Recipes.class);
            startActivity(int1);
        }
    });

}

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

我的editrecipe.xml     

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/background"
    android:textAllCaps="false"
    android:textSize="36sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

<EditText
    android:id="@+id/editIngredient"
    android:layout_width="wrap_content"
    android:layout_height="43dp"
    android:layout_marginBottom="284dp"
    android:layout_marginEnd="68dp"
    android:layout_marginRight="68dp"
    android:layout_marginTop="256dp"
    android:background="@color/black_overlay"
    android:ems="10"

    android:inputType="text|textPersonName"
    android:text="@string/ingredient"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.96"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button-recipes" />

<Button
    android:id="@+id/button_edit"
    android:layout_width="182dp"
    android:layout_height="92dp"
    android:layout_marginBottom="192dp"
    android:background="@android:drawable/alert_dark_frame"
    android:text="@string/edit_the_ingredient"
    android:textColor="#ffffff"
    android:textSize="18sp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/button_delete"
    android:layout_width="182dp"
    android:layout_height="92dp"
    android:layout_marginBottom="192dp"
    android:background="@android:drawable/alert_dark_frame"
    android:text="@string/delete_the_ingredient"
    android:textColor="#ffffff"
    android:textSize="18sp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/button_back"
    android:layout_width="182dp"
    android:layout_height="92dp"
    android:background="@android:drawable/alert_dark_frame"
    android:text="@string/Back"
    android:textColor="#ffffff"
    android:textSize="18sp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

在onItemCLickListener中,您正在做

String name = adapterView.getItemAtPosition(i).toString();
String recipe = adapterView.getItemAtPosition(i).toString();

两个变量的功能完全相同,因此您可能会获得两倍的相同信息

我不知道您的Recipe类的外观,但是也许您想要更多类似的东西

String name = recipesList.get(i).getName(); 
String recipe = recipesList.get(i).getRecipe();

编辑: 仔细阅读后,我意识到您不是在使用配方列表创建列表视图,而是在使用列表数据列表创建列表视图。您用光标的结果填充它,但是每次只填充第一列,因此我假设它始终是名称。您不会将食谱存储在任何地方,如果要在单击某个项目时能够检索它,就必须这样做。

假设您的Recipe类包含名称和配方,并且数据库中有一列用于名称,一列用于配方,则可以执行以下操作:

Cursor data = mDatabaseRecipes.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()){
    //get the value from the database in column 1
    //then add it to the ArrayList
    listData.add(data.getString(1));
    recipesList.add(new Recipe(data.get(NAME_COLUMN), data.get(RECIPE_COLUMN));
}

然后它应该工作而无需对代码进行太多更改。但是理想情况下,如果可能,您应该只使用一个列表。