1. package com.example.saulo.apptest;Android StudioのArrayListが最後の位置のみを上書きして他のものを上書きするのはなぜですか?
import android.nfc.Tag;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
public class Movie {
private String disciplina;
private int rating ;
private String professor ;
List<String> disciplinas;
List<String> professores;
public Movie newInstance() {
disciplinas = new ArrayList<>();
professores = new ArrayList<>();
Movie movie = new Movie();
disciplinas.add(0, "Comportamento organizacinal");
disciplinas.add(1, "graducao");
professores.add(0, "Ana Maria");
professores.add(1, "Luciana");
//Random random = new Random();
// ArrayList<Movie> list = new ArrayList<>();
for(String d : disciplinas){
movie.setDisciplina(d);
}
for(String p : professores) {
movie.setProfessor(p);
}
// list.add(movie);
// movie.setDisciplina (disciplinas [random.nextInt (disciplinas.length)]);
// movie.setRating (random.nextInt (5) + 1);
// movie.setYear (1980 + random.nextInt(37));
return movie;
}
public String getDisciplina() {
return disciplina;
}
public void setDisciplina(String disciplina) {
this.disciplina = disciplina;
}
public String getProfessor() {
return professor;
}
public void setProfessor(String professor) {
this.professor = professor;
} }
2.パッケージcom.example.saulo.apptest。
import android.content.Context; import android.view.LayoutInflater;
import android.view.View; import android.view.ViewGroup; import
android.widget.ArrayAdapter; import android.widget.TextView;
import java.util.List;
public class MovieAdapter extends ArrayAdapter<Movie> {
private int resource ;
public MovieAdapter (Context context , int resource , List<Movie> objects) {
super(context, resource, objects);
this.resource = resource ;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View row = convertView ;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(this.resource, parent, false);
}
Movie movie = getItem (position);
TextView name = (TextView) row.findViewById(R.id.disciplina);
name.setText(movie.getDisciplina());
TextView professor = (TextView) row.findViewById(R.id.professor);
professor.setText(" Professor : " + movie.getProfessor());
// TextView rating = (TextView) row.findViewById(R.id.rating);
// rating.setText(String.valueOf(movie.getRating()));
return row;
} }
3. package com.example.saulo.apptest;私は
for(String d : disciplinas){
movie.setDisciplina(d);
}
for(String p : professores) {
movie.setProfessor(p);
}
:あなたが達成したいどのようなコードがないと何の説明は素敵だっただろうが、
import android.nfc.Tag; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.util.Log; import android.view.View; import
android.widget.AdapterView; import android.widget.ArrayAdapter;
import android.widget.GridView; import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced_grid_view);
initAdvancedGridView();
initGridView();
}
private ArrayList<String> createItemsList() {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i <= 5; i++) {
list.add("Item" + i);
}
return list ;
}
private ArrayList <Movie> createMoviesList() {
ArrayList <Movie> list = new ArrayList <Movie>();
for (int i = 0; i <= 1; i++) {
Movie movie = new Movie();
list.add(movie.newInstance());
}
return list ;
}
private void initGridView() {
GridView gridView = (GridView) findViewById(R.id.grid_view);
if (gridView == null) {
return;
}
ArrayList<String > list = createItemsList();
ArrayAdapter<String > adapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1,
list);
gridView.setAdapter(adapter);
}
private void initAdvancedGridView(){
GridView gridView = (GridView) findViewById(R.id.advanced_grid_view);
if (gridView == null) {
return ;
}
ArrayList<Movie> list = createMoviesList();
MovieAdapter adapter = new MovieAdapter (this, R.layout.grid_view_item, list);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
Movie movie = (Movie) parent.getAdapter().getItem(position);
Toast toast = Toast.makeText(MainActivity.this, movie.getDisciplina() , Toast.LENGTH_LONG);
toast.show();
}
});
}
}
ようこそ!私は、このコードの95%があなたの質問に関連していないと思います。あなたの問題を示す[**最小**、完全で実証可能な例](http://stackoverflow.com/help/mcve)を作成してください。 –