为啥我的 Listview 没有在我的活动中显示任何内容?
Posted
技术标签:
【中文标题】为啥我的 Listview 没有在我的活动中显示任何内容?【英文标题】:Why is my Listview not showing anything on my activity?为什么我的 Listview 没有在我的活动中显示任何内容? 【发布时间】:2019-11-23 18:15:27 【问题描述】:我创建了一个自定义列表,然后将一些数据添加到我的列表视图中,但它没有显示。你能告诉我我的代码缺少什么吗?它没有在我的 logcat 上显示与此问题相关的任何错误。
我想要发生的是,当用户从 AddStudentActivity 输入所有详细信息时,所有数据都将显示在 MainActivity 中的另一个活动中。
我在这个上使用意图。感谢您的帮助。
MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener
ArrayList<Student> studentArrayList = new ArrayList<>();
CustomAdapter adapter;
private Uri imageUri;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.student_listview);
adapter = new CustomAdapter(this, studentArrayList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
//for menu
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if(id == android.R.id.home)
onBackPressed();
return true;
else if(id == R.id.action_add)
Intent add = new Intent(MainActivity.this, AddStudentActivity.class);
startActivity(add);
return super.onOptionsItemSelected(item);
//inflate the menu
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.addmenu, menu);
return super.onCreateOptionsMenu(menu);
//handles the onclick listener for the listview
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK)
Bundle b = data.getExtras();
imageUri = b.getParcelable("image");
String lastname = b.getString("lastname");
String firstname = b.getString("firstname");
String course = b.getString("course");
Student student = new Student(imageUri, lastname, firstname, course);
studentArrayList.add(student);
adapter.notifyDataSetChanged();
else
CustomAdapter.java
public class CustomAdapter extends BaseAdapter
Context context;
//data container
ArrayList<Student> list;
LayoutInflater inflater;
//contructor
public CustomAdapter(Context context, ArrayList<Student> list)
this.context = context;
this.list = list;
this.inflater = LayoutInflater.from(context);
@Override
public int getCount()
return list.size();
@Override
public Object getItem(int position)
return list.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder;
if(convertView == null)
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_layout, parent, false);
holder.iv = (ImageView) convertView.findViewById(R.id.imageView);
holder.lname = (TextView) convertView.findViewById(R.id.textLastname);
holder.fname= (TextView) convertView.findViewById(R.id.textFirstname);
holder.course = (TextView) convertView.findViewById(R.id.textCourse);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
//inflate
holder.iv.setImageURI(list.get(position).getUriImage());
holder.lname.setText(list.get(position).getStudlname());
holder.fname.setText(list.get(position).getStudfname());
holder.course.setText(list.get(position).getStudcourse());
return convertView;
//creating a static class
static class ViewHolder
ImageView iv;
TextView lname, fname,course;
AddStudentActivity.java
public class AddStudentActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener
ListView lv;
ImageView studImage;
Uri studImageUri;
EditText lastname, firstname;
String selectedCourse;
Spinner course;
Button btnsave, btncancel;
CustomAdapter adapter;
private static final int PICK_IMAGE = 100;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_student);
//
studImage = (ImageView) findViewById(R.id.addstudentimage);
lastname = (EditText) findViewById(R.id.editTextLastname);
firstname = (EditText) findViewById(R.id.editTextFirstname);
course = (Spinner) findViewById(R.id.spinnerCourse);
btnsave = (Button) findViewById(R.id.btn_save);
btncancel = (Button) findViewById(R.id.btn_cancel);
studImage.setOnClickListener(this);
btnsave.setOnClickListener(this);
btncancel.setOnClickListener(this);
course.setOnItemSelectedListener(this);
//on click listeners for the buttons and imageview
@Override
public void onClick(View v)
int id = v.getId();
switch (id)
case R.id.addstudentimage:
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
break;
case R.id.btn_save:
String lname = lastname.getText().toString();
String fname = firstname.getText().toString();
String newCourse = course.getSelectedItem().toString();
if(!studImage.equals(R.drawable.user) && !lastname.equals(" ") && !firstname.equals("") && !course.getSelectedItem().toString().trim().equals(0))
Intent intent = new Intent();
intent.putExtra("image", this.studImageUri);
intent.putExtra("lastname", lname);
intent.putExtra("firstname", fname);
intent.putExtra("course", newCourse);
this.setResult(Activity.RESULT_OK, intent);
Toast.makeText(getApplicationContext(), "New student successfully added!", Toast.LENGTH_SHORT).show();
finish();
else
Toast.makeText(getApplicationContext(), "Error in adding a new student!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_cancel:
studImage.setImageResource(R.drawable.user);
lastname.setText("");
firstname.setText("");
course.setSelection(0);
break;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode != 0)
if(data != null)
studImageUri = data.getData();
studImage.setImageURI(studImageUri);
else
//on click listeners for the spinners
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
int sid = parent.getId();
switch (sid)
case R.id.spinnerCourse:
selectedCourse = this.course.getItemAtPosition(position).toString();
break;
@Override
public void onNothingSelected(AdapterView<?> parent)
学生.java
public class Student
Uri uriImage;
String studlname, studfname, studcourse;
//constructor
public Student(Uri uriImage, String studlname, String studfname, String studcourse)
super();
this.uriImage = uriImage;
this.studlname = studlname;
this.studfname = studfname;
this.studcourse = studcourse;
//getters and setters
public Uri getUriImage()
return uriImage;
public void setUriImage(Uri uriImage)
this.uriImage = uriImage;
public String getStudlname()
return studlname;
public void setStudlname(String studlname)
this.studlname = studlname;
public String getStudfname()
return studfname;
public void setStudfname(String studfname)
this.studfname = studfname;
public String getStudcourse()
return studcourse;
public void setStudcourse(String studcourse)
this.studcourse = studcourse;
【问题讨论】:
【参考方案1】:你使用了错误的语法,替换 startActivity -> startActivityForResult
参考:https://developer.android.com/training/basics/intents/result
【讨论】:
以上是关于为啥我的 Listview 没有在我的活动中显示任何内容?的主要内容,如果未能解决你的问题,请参考以下文章
ListView onItemClickListener 不做任何事情