当前位置: 首页 > 知识库问答 >
问题:

类java.util.ArrayList具有泛型类型参数,请改用GenericTypeIndicator

欧照
2023-03-14

导入java.util.ArrayList;导入java.util.List;

EditText editTextName,editTextAmount, editTextNumber;
Button buttonAddArtist;
ArrayList array;
ListView listViewArtists;

//a list to store all the artist from firebase database
List<Artist> artists;

//our database reference object
DatabaseReference databaseArtists;

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

    //getting the reference of artists node
    databaseArtists = FirebaseDatabase.getInstance().getReference("artists");

    //getting views
    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextAmount = (EditText) findViewById(R.id.editTextAmount);
    editTextNumber = (EditText) findViewById(R.id.editTextNumber);

    listViewArtists = (ListView) findViewById(R.id.listViewArtists);

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

    //list to store artists
    artists = new ArrayList<>();


    //adding an onclicklistener to button
    buttonAddArtist.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //calling the method addArtist()
            //the method is defined below
            //this method is actually performing the write operation
            addArtist();
        }
    });

    //attaching listener to listview
    listViewArtists.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //getting the selected artist
            Artist artist = artists.get(i);

            //creating an intent
            Intent intent = new Intent(getApplicationContext(), ArtistActivity.class);

            //putting artist name and id to intent
            intent.putExtra(ARTIST_ID, artist.getArtistId());
            intent.putExtra(ARTIST_NAME, artist.getArtistName());

            //starting the activity with intent
            startActivity(intent);
        }
    });

    listViewArtists.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            Artist artist = artists.get(i);
            showUpdateDeleteDialog(artist.getArtistId(), artist.getArtistName());
            return true;
        }
    });


}

private void showUpdateDeleteDialog(final String artistId, String artistName) {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.update_dialog, null);
    dialogBuilder.setView(dialogView);

    final EditText editTextName = (EditText) dialogView.findViewById(R.id.editTextName);
    final EditText editTextAmount = (EditText) dialogView.findViewById(R.id.editTextAmount);
    final EditText editTextNumber = (EditText) dialogView.findViewById(R.id.editTextNumber);
    final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateArtist);
    final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteArtist);

    dialogBuilder.setTitle(artistName);
    final AlertDialog b = dialogBuilder.create();
    b.show();


    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = editTextName.getText().toString().trim();
            String amount = editTextAmount.getText().toString().trim();
            String number = editTextNumber.getText().toString().trim();
            int fb = Integer.parseInt(amount);
            int rl = Integer.parseInt(number);
            int m = fb / rl;
            ArrayList array=new ArrayList();





            if (!TextUtils.isEmpty(name)) {

                for(int l=0; l<rl; l++){
                    array.add(l,m);
                }
                Toast.makeText(getBaseContext(), array+"", Toast.LENGTH_SHORT).show();
                updateArtist(artistId, name, amount, number, array);
                b.dismiss();
            }

        }
    });



    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            deleteArtist(artistId);
            b.dismiss();
        }
    });
}

private boolean updateArtist(String id, String name, String amount, String number, ArrayList array) {
    //getting the specified artist reference
    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("artists").child(id);

    //updating artist
    Artist artist = new Artist(id, name, amount, number, array);
    dR.setValue(artist);
    Toast.makeText(getApplicationContext(), "Artist Updated", Toast.LENGTH_LONG).show();
    return true;
}

private boolean deleteArtist(String id) {
    //getting the specified artist reference
    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("artists").child(id);

    //removing artist
    dR.removeValue();

    //getting the tracks reference for the specified artist
    DatabaseReference drTracks = FirebaseDatabase.getInstance().getReference("tracks").child(id);

    //removing all tracks
    drTracks.removeValue();
    Toast.makeText(getApplicationContext(), "Artist Deleted", Toast.LENGTH_LONG).show();

    return true;
}

@Override
protected void onStart() {
    super.onStart();
    //attaching value event listener
    databaseArtists.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //clearing the previous artist list
            artists.clear();

            //iterating through all the nodes
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                //getting artist


                Artist artist = postSnapshot.getValue(Artist.class);
                //adding artist to the list
                artists.add(artist);
            }

            //creating adapter
            ArtistList artistAdapter = new ArtistList(MainActivity.this, artists);
            //attaching adapter to the listview
            listViewArtists.setAdapter(artistAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}


/*
 * This method is saving a new artist to the
 * Firebase Realtime Database
 * */
private void addArtist() {
    //getting the values to save
    String name = editTextName.getText().toString().trim();
    String amount = editTextAmount.getText().toString().trim();
    String number = editTextNumber.getText().toString().trim();
    int fb = Integer.parseInt(amount);
    int rl = Integer.parseInt(number);
    int m = fb / rl;

    ArrayList array = new ArrayList();

    for(int l=0; l<rl; l++){
        array.add(l,m);
    }

    Toast.makeText(getBaseContext(), array+"", Toast.LENGTH_SHORT).show();
    //checking if the value is provided
    if (!TextUtils.isEmpty(name)) {

        //getting a unique id using push().getKey() method
        //it will create a unique id and we will use it as the Primary Key for our Artist
        String id = databaseArtists.push().getKey();

        //creating an Artist Object
        Artist artist = new Artist(id, name, amount, number, array);

        //Saving the Artist
        databaseArtists.child(id).setValue(artist);

        //setting edittext to blank again
        editTextName.setText("");
        editTextAmount.setText("");
        editTextNumber.setText("");


        //displaying a success toast
        Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
    } else {
        //if the value is not given displaying a toast
        Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
    }
} }

另外,artist.java配备了空构造函数

@IgnoreExtraProperties公共类Artist{private String ArtistId;private String ArtistName;private String ArtistAmount;private String ArtistAry;

public Artist(){
    //this constructor is required
}

public Artist(String artistId, String artistName, String artistAmount, String artistNumber, ArrayList artistArray) {
    this.artistId = artistId;
    this.artistName = artistName;
    this.artistAmount = artistAmount;
    this.artistNumber = artistNumber;
    this.artistArray = artistArray;


}

public String getArtistId() {
    return artistId;
}

public String getArtistName() {
    return artistName;
}

public String getArtistAmount() {
    return artistAmount;
}

public String getArtistNumber() { return artistNumber; }

public ArrayList getArtistArray() { return artistArray; } }

这就是我的数据库的样子:-

共有1个答案

司空和悌
2023-03-14

您需要指定类型

artists = new ArrayList<Artist>();

更简单的方法是在声明空间中初始化艺术家

所以不是

List<Artist> artists;
List<Artist> artists = new ArrayList<>();
 类似资料:
  • 问题是: 创建一个名为Duo的泛型类,它有两个参数a和B。声明一个名为a类型的第一个变量,以及一个名为B类型的第二个变量。创建一个接受这两个参数的构造函数。在构造函数中,将这些参数分别分配给声明的变量。 这是我能想到的解决方案: 然后,当我继续问下一个问题时,我被绊住了。问题是: 使用问题4中的Duo类声明并创建两个对象,如下所示: a) 第一个名为sideShape的对象分别由字符串类型和整数类

  • 问题内容: 我有一个方法以a 作为参数。 在中,我如何知道a 是还是a 是? 问题答案: 根据用户omain的回答“如果使用<?>,则意味着您将不会在任何地方使用参数化类型。要么转到特定类型(在您的情况下,似乎是),要么转到非常通用的“ 另外,我相信如果您使用问号,编译器将在运行时(类型;有效Java的第119页)消除类型不匹配的情况,绕过擦除,并有效地消除了使用泛型类型所带来的好处? 要回答发问

  • 我有一个问题,从抽象类中重写泛型方法。 这是我的抽象类: 当我创建类(B)来实现类(a)时,如下所示: 显示了(getData)方法中的以下编译错误: ”“B。getData“(“字符串函数(字符串)”不是“a”的有效重写。getData'('字符串函数(类型)‘)。dart(无效覆盖) 以及返回语句中的此错误: 类型为“String”的值不能从方法'getData'返回,因为它的返回类型为'St

  • 我想使用泛型类作为另一个泛型类的类型参数。 起初,我对类的定义是这样的: 然后我的需求发生了变化,我不得不为我的R类型使用包装器/持有者类 到目前为止,我的尝试:(给出编译时错误:

  • 我已经声明了这样一个活动 谢谢您的关注

  • 我在我的一个实用程序类中有一个方法,它接受一个集合和一个类对象,并返回一个Iterable实例,该实例可以遍历作为指定类实例的集合的所有成员。其签名为: 这对于大多数用例都非常有效,但现在我需要将其与泛型类