库存清单中“动物”项目的onClick无效。应该提示动物档案信息

时间:2019-05-17 22:46:36

标签: java android xml mobile

我只是想自己学习Android上的移动开发,因此才开始自己编写代码。为了学习,我提出了一些案例。目前,我遇到一个我似乎无法自行解决的问题,因为我还没有足够的经验:

在存货中,用户需要抓几个动物。如果用户单击动物档案,则应该有另一个页面提示您所选动物的档案信息。

  

当前,当我单击清单列表中的动物时。如果有人可以帮助指导我   寻求有效的解决方案?

这些是我编写的课程:

  • InventoryActivity.java
  • AnimalProfile.java
  • Activity_inventory.xml
  • Animal_profile.xml
  • activity_detail.xml

我怀疑InventoryActivity.java和AnimalProfile.java之间存在一些问题,其中在单击库存项目时,不会获取该项目的相关配置文件信息。

具体地说,我认为问题必须出在InventoryActivity.java 的以下代码段中:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent newActivity;
                switch (position){
                    case 0: newActivity = new Intent(InventoryActivity.this, AnimalProfile.class);
                    startActivity(newActivity);
                    break;
                }

在我所有的代码下方:


  • InventoryActivity.java

package com.example.animalcrash;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.animalcrash.controller.DierController;
import com.example.animalcrash.model.Dier;

import java.util.ArrayList;
import java.util.Arrays;

public class InventoryActivity extends AppCompatActivity {
    ListView listView;
    /*int id[] = {0,1,2,3};
    String mTitle[] = {"Robert", "Omer", "Mehmet", "Liza"};
    String mDescription[] = {"Leeuw", "Vos", "Paard", "Rode panda"};*/
    static int images[] = {R.drawable.grijsleeuw, R.drawable.grijsvos,
            R.drawable.grijspaard, R.drawable.greyredpanda, R.drawable.greyredpanda};


    Integer id[];
    String mTitle[];
    String mDescription[];

    ArrayList<Integer> dId = new ArrayList<>();
    ArrayList<String> dName = new ArrayList<>();
    ArrayList<String> dDesc = new ArrayList<>();
    ArrayList<String> dImg = new ArrayList<>();

    /**
     * Hieronder wordt foto's veranderd van de dieren als ze zijn gevangen
     * @param id van het dier
     * @param drawing het foto van het dier
     */
    public static void updateImages(int id, Bitmap drawing) {
        /*DierController dierController = new DierController();
        Dier dier = dierController.getDierById(id);
        String diernaam = dier.getNaam();
        Drawable d = new BitmapDrawable(getResources(), drawing);
        images[id] = drawing;*/
        System.out.println("TRYING TO UPDATE THE IMAGE IN INVENTORY");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory);

        listView = findViewById(R.id.myInventory);

        // Omer testing
        DierController dierController = new DierController();
        for (Dier d: dierController.getDieren()) {
            dId.add(d.getId());
            dName.add(d.getNaam());
            dDesc.add(d.getBeschrijving());
            dImg.add(d.getAfbeelding());
        }
        System.out.println("TESTING THE DIER ID ARRAY:" + dId);
        System.out.println("TESTING THE DIER NAME ARRAY:" + dName);
        System.out.println("TESTING THE DIER DESC ARRAY:" + dDesc);
        System.out.println("TESTING THE DIER IMG ARRAY:" + dImg);


        id = new Integer[dId.size()];
        id = dId.toArray(id);
        mTitle = new String[dName.size()];
        mTitle = dName.toArray(mTitle);
        mDescription = new String[dDesc.size()];
        mDescription = dDesc.toArray(mDescription);

        System.out.println(Arrays.toString(id));
        System.out.println(Arrays.toString(mTitle));
        System.out.println(Arrays.toString(mDescription));



        MyAdapter adapter = new MyAdapter(this, mTitle, mDescription, images, id);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent newActivity;
                switch (position){
                    case 0: newActivity = new Intent(InventoryActivity.this, AnimalProfile.class);
                    startActivity(newActivity);
                    break;
                }
            }
        });
    }

    class MyAdapter extends ArrayAdapter<String> {

        Context context;
        String rTitle[];
        String rDescription[];
        int rImgs[];
        Integer rId[];

        MyAdapter(Context c, String title[], String description[], int imgs[], Integer id[] ) {
            super(c, R.layout.activity_detail, R.id.textView2, title);
            this.context = c;
            this.rTitle = title;
            this.rDescription = description;
            this.rImgs = imgs;
            this.rId = id;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.activity_detail, parent, false);
            ImageView images = row.findViewById(R.id.girafGrijs);
            TextView myTitle = row.findViewById(R.id.textView1);
            TextView myDescrip = row.findViewById(R.id.textView2);

            images.setImageResource(rImgs[position]);
            myTitle.setText(rTitle[position] + " (id: " +rId[position] + ")");
            myDescrip.setText(rDescription[position]);

            return row;
        }
    }
}

  • AnimalProfile.java

package com.example.animalcrash;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

public class AnimalProfile extends AppCompatActivity {

    TextView profileName;
    TextView levelDieren;
    TextView descrip;
    ImageView dierFoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animal_profile);

        Bundle bundle = getIntent().getExtras();
        profileName = findViewById(R.id.profileName);
        levelDieren = findViewById(R.id.levelDieren);
        descrip = findViewById(R.id.descrip);
        dierFoto = findViewById(R.id.animalProfilePhoto);

        if (bundle != null) {
            profileName.setText(bundle.getString("STRING_I_NEED"));
            if (profileName.getText().toString().equalsIgnoreCase("Leeuw (id: 1)")){
                dierFoto.setImageDrawable(ContextCompat.getDrawable(AnimalProfile.this,
                        R.drawable.leeuw));
            } else if (profileName.getText().toString().equalsIgnoreCase("Vos (id: 2)")){
                dierFoto.setImageDrawable(ContextCompat.getDrawable(AnimalProfile.this,
                        R.drawable.vos));
            } else if (profileName.getText().toString().equalsIgnoreCase("Paard (id: 3)")){
                dierFoto.setImageDrawable(ContextCompat.getDrawable(AnimalProfile.this,
                        R.drawable.paard));
            } else if (profileName.getText().toString().equalsIgnoreCase("Rode Panda (id: 4)")){
                dierFoto.setImageDrawable(ContextCompat.getDrawable(AnimalProfile.this,
                        R.drawable.redpanda));
            } else if (profileName.getText().toString().equalsIgnoreCase("Neushoorn (id: 5)")){
                dierFoto.setImageDrawable(ContextCompat.getDrawable(AnimalProfile.this,
                        R.drawable.neushoorn));
            }
        }


    }
}

  • AnimalProfile.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/dierProfiel"
    android:padding="16dp"
    tools:context=".AnimalProfile">

    <ImageView
        android:id="@+id/animalProfilePhoto"
        android:layout_width="550dp"
        android:layout_height="550dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:contentDescription="@string/animalProfilePhoto"
        android:src="@drawable/standaardanimal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="494dp"
        android:layout_height="216dp"
        android:layout_marginStart="36dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/animalProfilePhoto"
        app:layout_constraintVertical_bias="1.0"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/profileName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical"
            android:text="@string/naam"
            android:textColor="#000"
            android:textSize="30sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/levelDieren"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical"
            android:text="@string/level"
            android:textColor="#000"
            android:textSize="24sp"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/descrip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/description"
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="italic"
        />

    </LinearLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="27dp" />



</android.support.constraint.ConstraintLayout>

  • Activity_Detail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layOutPerDier"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp"
    tools:context=".InventoryActivity">

    <ImageView
        android:id="@+id/girafGrijs"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/grijsgiraf"
        android:src="@drawable/girafgrijs"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="494dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:orientation="vertical"
        app:layout_constraintStart_toEndOf="@+id/girafGrijs"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteY="7dp">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/naam"
            android:textStyle="bold"
            android:textColor="#000"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/description"
            android:textColor="#181F46"
            android:textStyle="italic"
            android:textSize="20sp" />
    </LinearLayout>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="27dp" />


</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案