为什么我的Listview没有显示任何活动信息?

时间:2019-07-15 03:46:25

标签: android listview android-intent

我创建了一个自定义列表,然后放入一些数据并将其充气到我的列表视图中,但未显示。您能告诉我代码中缺少什么吗?它在我的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) {

    }

Student.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 个答案:

答案 0 :(得分:1)

您使用了错误的语法,请替换startActivity-> startActivityForResult

引用:https://developer.android.com/training/basics/intents/result