如何使列表中的项目可单击并导致具有特定格式的新页面在android studio中

时间:2017-03-15 20:39:23

标签: java android xml listview

我试图在android中创建一个包含列表视图的页面,然后当您单击列表中的某个项目时,它应该会导致具有特定格式的新活动。

基本上,它是一个成绩中心页面,其中包含您的课程列表,当您点击课程时,它应该打开课程页面。

我创建了一个Grades中心活动,一个课程页面活动及其各自的课程。

现在有2个页面存在,但它们没有相互链接,这实际上是一个包含许多其他页面等的大文件,但我刚刚发布了我需要帮助的部分但是如果有必要我可以发布其他人也是。我希望列表中的元素可以点击 - 然后进入用户点击的课程页面,就像其中一个课程是Biol 116那么列表应该带你到biol 116课程页面。在此先感谢您的帮助! :)

GradeCentre xml文件:

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_grades_centre"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gregmallan.yuboard.GradesCentre"><![CDATA[

android:clickable="true">

]]>

<TextView
    android:text="Current Overall Grade:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:textColor="@color/colorPrimaryDark"
    android:textSize="20sp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="21dp" />

<ListView
    android:id="@+id/CourseGradeList"
    android:layout_height="350dp"
    android:layout_width="230dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="25dp" />

<TextView
    android:text="85.67"
    android:layout_height="40dp"
    android:id="@+id/Overall"
    android:layout_width="80dp"
    android:textSize="25sp"
    android:textColor="@android:color/holo_red_light"
    android:layout_alignTop="@+id/textView3"
    android:layout_toEndOf="@+id/CourseGradeList"
    android:layout_marginStart="17dp" />

GradeCentre java文件:

public class GradesCentre extends AppCompatActivity {

   double OverallAvg;
    ArrayList<Course> AllCourses;
    ArrayAdapter<Course> AdapterCourse;

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

        AllCourses = new ArrayList<>();

        AllCourses.add(new Course("Cosc 111"));
        AllCourses.add(new Course("Biol 116"));
        AllCourses.add(new Course("Anth 100"));

        AllCourses.get(0).setCourseGrade(95);
        AllCourses.get(1).setCourseGrade(78);
        AllCourses.get(2).setCourseGrade(84);

        OverallAvg = calcOverallGrade(AllCourses);

        AdapterCourse = new ArrayAdapter<Course>(this ,   android.R.layout.simple_list_item_1 , AllCourses);
        ListView listView = (ListView) findViewById(R.id.CourseGradeList);
        listView.setAdapter(AdapterCourse);

    }

    public void onItemClick(AdapterView<?> a, View v, int i, long l) {

    }

    public double calcOverallGrade(ArrayList<Course> Anything){
        double sum = 0;
        for(int i = 0; i < Anything.size();i++ ){
            sum = Anything.get(i).getCourseGrade() + sum;
        }
        OverallAvg = sum/(Anything.size());
        return OverallAvg;
    }

}

单课程页面xml:           

<TextView
    android:text="_________________________________________________"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:id="@+id/textView10"
    android:textStyle="normal|bold"
    android:textColor="@color/colorPrimaryDark"
    android:layout_below="@+id/SingleCourseGrade"
    android:layout_alignParentStart="true" />

<TextView
    android:text="_________________________________________________"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:id="@+id/textView9"
    android:textStyle="normal|bold"
    android:textColor="@color/colorPrimaryDark"
    android:layout_below="@+id/SingleCourseGrade"
    android:layout_alignParentStart="true" />

<TextView
    android:text="ex. Course 1"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/CourseName"
    android:layout_height="30dp"
    android:layout_width="200dp"
    android:textSize="24sp"
    android:textColor="@color/colorPrimaryDark"
    android:textStyle="normal|bold" />

<TextView
    android:text="Current Overall Grade: "
    android:layout_height="30dp"
    android:layout_below="@+id/CourseName"
    android:layout_alignParentStart="true"
    android:layout_marginTop="15dp"
    android:id="@+id/textView5"
    android:textSize="20sp"
    android:textColor="@color/colorPrimaryDark"
    android:layout_width="200dp" />

<Button
    android:text="Add/Manage Components"
    android:layout_height="50dp"
    android:id="@+id/AddManage"
    android:layout_width="350dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" />

<ListView
    android:id="@+id/SingleCourseGradeList"
    android:layout_above="@+id/AddManage"
    android:layout_alignStart="@+id/AddManage"
    android:layout_height="315dp"
    android:layout_width="280dp" />

<TextView
    android:text="%%%"
    android:layout_marginEnd="14dp"
    android:id="@+id/SingleCourseGrade"
    android:textSize="20sp"
    android:textColor="@android:color/holo_red_light"
    android:layout_alignParentEnd="true"
    android:layout_width="60dp"
    android:layout_height="25dp"
    android:layout_alignTop="@+id/textView5" />

<TextView
    android:text="_________________________________________________"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:id="@+id/textView8"
    android:textStyle="normal|bold"
    android:textColor="@color/colorPrimaryDark"
    android:layout_above="@+id/textView5"
    android:layout_alignParentStart="true" />

<TextView
    android:text="Individual Components and Grades:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView7"
    android:textSize="16sp"
    android:textColor="@color/colorPrimaryDark"
    android:layout_above="@+id/SingleCourseGradeList"
    android:layout_alignStart="@+id/SingleCourseGradeList" />

</RelativeLayout>

单学期页面java文件:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.gregmallan.yuboard.SchoolRelatedClasses.CourseComponent;

import java.util.ArrayList;

public class SingleCoursePage extends AppCompatActivity{

ArrayAdapter<CourseComponent> adapter;

Button Components;
Button CompDetail;

double CourseGrade;

ArrayList<CourseComponent> AllComponents;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_course_page);
//this is just added until we get real data
    AllComponents = new ArrayList<CourseComponent>();

    CourseComponent x = new CourseComponent("Dummy", 20);
    AllComponents.add(x);

    CourseComponent x2 = new CourseComponent("Dummy2", 40);
    AllComponents.add(x2);

    adapter = new ArrayAdapter<CourseComponent>(this ,       android.R.layout.simple_list_item_1 , AllComponents);
    ListView listView = (ListView) findViewById(R.id.SingleCourseGradeList);
    listView.setAdapter(adapter);

    Components = (Button) findViewById(R.id.AddManage);
    Components.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Inform the user the button has been clicked
            openCourseComponents(v);
        }
    });

}

public void openCourseComponents(View view){
    Intent intent1 = new Intent(this, IndividualCourseCentre.class);
    startActivity(intent1);
}

//public void openComponentBreakdown(View view){
  //  Intent intent2 = new Intent(this, IndividualCourseCentre.class);
   // startActivity(intent2);
//}


public double calcCourseGrade(ArrayList<CourseComponent> x){

    double sum = 0;
    for(int i = 0; i < x.size();i++){
        sum = x.get(i).getTotalComponentGrade();
    }
    CourseGrade = sum/(x.size());
    return CourseGrade;

    //TO-DO
    //when this method is called changed the text to the CourseGrade
}
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                String selectedFromList =(String) (listView.getItemAtPosition(position));
                Intent intent = new Intent(GradesCentre.this, Cosc111Activity.class);
                if(selectedFromList.equals("Cosc 111")){
                    //Set object here
                    intent.putExtra("Course_Details", obj);
                    startActivity(intent);
                }

               else if(selectedFromList.equals("Biol 116")){
                    //Set object here
                    intent.putExtra("Course_Details", obj);
                    startActivity(intent);
                }

               //Do as above for rest of the list items
            }
        });

在SingleCourseActivity中获取对象:

getIntent().getSerializableExtra("Course_Details");

希望这有帮助。