单击按钮后,我无法向每个id和名称进行Toast。每次单击不同按钮后显示最后的ID和名称。我想在点击特定按钮
后显示Toast中的每个数据列表
活动
listOfFileToLoad['name'].loaded;
listOfFileToLoad['name'].loaded = 2;
适配器
public class ListViewActivity extends AppCompatActivity {
ListView studentListView;
ArrayList<Student> studentList = new ArrayList<>();
StudentAdapter studentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
studentListView = (ListView) findViewById(R.id.studentListView);
studentList.add(new Student(01, "Monir"));
studentList.add(new Student(02, "Jibon"));
studentList.add(new Student(03, "Mahmud"));
studentList.add(new Student(04, "Saiful"));
studentList.add(new Student(05, "Arif"));
studentAdapter = new StudentAdapter(this, studentList);
studentListView.setAdapter(studentAdapter);
}
public void viewStudentClick(View view) {
Toast.makeText(getApplicationContext(),studentAdapter.nameText.getText(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),studentAdapter.idText.getText(), Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
studentListView = (ListView) findViewById(R.id.studentListView);
studentList.add(new Student(01, "Monir"));
studentList.add(new Student(02, "Jibon"));
studentList.add(new Student(03, "Mahmud"));
studentList.add(new Student(04, "Saiful"));
studentList.add(new Student(05, "Arif"));
studentAdapter = new StudentAdapter(this, studentList);
studentListView.setAdapter(studentAdapter);
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ListViewActivity.this,studentList.get(position).getId() +
studentList.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
}
答案 1 :(得分:1)
使用:
studentListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// selected item
String name = (String) parent.getItemAtPosition(position);
Toast toast = Toast.makeText(getApplicationContext(),"Position:"+position+", Name: "+name, Toast.LENGTH_SHORT).show();
}
});