Android自訂配接器和ListView的點擊事件相結合的使用,androidlistview

來源:互聯網
上載者:User

Android自訂配接器和ListView的點擊事件相結合的使用,androidlistview

下邊示範一個使用ListView和自訂配接器的案例,點擊ListView中的條目會出現一個對話方塊,進行成績的修改,修改之後會立即通知適配器進行資料的重新載入,如下:

(1)、用於顯示listView的布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="20dp" >    <ListView        android:id="@+id/course_student_manage"        android:layout_width="match_parent"        android:layout_height="match_parent" >    </ListView></LinearLayout>

二、用於顯示每一天資訊的布局檔案,就是每一個item選項,需要在適配器中使用到

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="80dp"    android:padding="8dp" >    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="fill_parent"        android:layout_height="80dp"        android:orientation="vertical" >        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="40dp" >            <TextView                android:layout_width="40dp"                android:layout_height="40dp"                android:text="姓名"                android:textSize="20sp" />            <View                android:layout_width="wrap_content"                android:layout_height="1dp"                android:layout_weight="1" />            <TextView                android:layout_width="40dp"                android:layout_height="40dp"                android:text="課程"                android:textSize="20sp" />            <View                android:layout_width="wrap_content"                android:layout_height="1dp"                android:layout_weight="1" />            <TextView                android:layout_width="40dp"                android:layout_height="40dp"                android:text="分數"                android:textSize="20sp" />        </LinearLayout>        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="40dp" >            <TextView                android:id="@+id/tv_name"                android:layout_width="80dp"                android:layout_height="50dp"                android:text=""                android:textSize="20sp" />            <View                android:layout_width="wrap_content"                android:layout_height="1dp"                android:layout_weight="1" />            <TextView                android:id="@+id/tv_course"                android:layout_width="80dp"                android:layout_height="50dp"                android:text=""                android:textSize="20sp" />            <View                android:layout_width="wrap_content"                android:layout_height="1dp"                android:layout_weight="1" />            <TextView                android:id="@+id/tv_score"                android:layout_width="40dp"                android:layout_height="40dp"                android:text=""                android:textSize="20sp" />        </LinearLayout>    </LinearLayout></RelativeLayout>

效果如下:

(3)對listview的控制類:這裡邊有一些載入資料的方法,根據具體的情況修改之就可以

public class ManageStudentScoreActivity extends Activity implements        OnItemClickListener {    private List<Course> courseList;    private ListView course_student_manage;    private CourseAdapter adapter;    private EditText update_score;    private Button btn_ok;    private String studentName;    private String courseName;    private AlertDialog dialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.course_studen_manage);        course_student_manage = (ListView) this                .findViewById(R.id.course_student_manage);        // 載入資料        courseList = loadCourseData();        if (courseList != null) {            adapter = new CourseAdapter(this, courseList);        } else {            ToastUtil.showToast(this, "沒有學生選課資訊!");        }        course_student_manage.setAdapter(adapter);        course_student_manage.setOnItemClickListener(this);    }    /**     * 查詢所有學生資訊     *      * @return     */    protected List<Course> loadCourseData() {        CourseInter courseInter = new CourseImpl();        return courseInter.getCourseDetail();    }    /**     * listview的點擊事件     */    public void onItemClick(AdapterView<?> adapter, View view, int position,            long id) {        //這裡就是建立一個彈出的對話方塊        AlertDialog.Builder builder = new AlertDialog.Builder(this);        // 載入布局檔案        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View dialogView = inflater.inflate(R.layout.dialog_update_score, null);        update_score = (EditText) dialogView.findViewById(R.id.update_score);        btn_ok = (Button) dialogView.findViewById(R.id.btn_ok);        builder.setView(dialogView);        builder.create();        dialog = builder.show(); //一定要show        // 獲得每一個item的條目學生姓名 課程姓名 和分數        View adapterView = view;        TextView tv_score = (TextView) adapterView.findViewById(R.id.tv_score);        TextView tv_name = (TextView) adapterView.findViewById(R.id.tv_name);        TextView tv_course = (TextView) adapterView.findViewById(R.id.tv_course);        studentName = tv_name.getText().toString();        courseName = tv_course.getText().toString();        btn_ok.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                String scoreString = update_score.getText().toString();                float score = Float.parseFloat(scoreString);                // 更改成績                updateCourseDetail(studentName, courseName, score);            }        });        // dialog.dismiss();    }    /**     * 更新學產生績資訊的     *      * @param studentName     * @param courseName     * @param score     */    protected void updateCourseDetail(String studentName, String courseName,            float score) {        CourseInter courseInter = new CourseImpl();        if (courseInter.updateStudentCourse(studentName, courseName, score)) {            ToastUtil.showToast(ManageStudentScoreActivity.this, "修改成功!");            //取消彈出的對話方塊            dialog.dismiss();            // 通知適配器資料已經變化            adapter.notifyDataSetChanged();        } else {            ToastUtil.showToast(ManageStudentScoreActivity.this, "修改失敗!");        }    }}

(4)自訂配接器如下:為了可以使適配器可以自動重新整理,一定要繼承的方法

@Override    public void notifyDataSetChanged() {        super.notifyDataSetChanged();    }

具體的適配器代碼:

public class CourseAdapter extends BaseAdapter {    private Context context;    private List<Course> coursetList;    public CourseAdapter(Context context, List<Course> coursetList) {        this.context = context;        this.coursetList = coursetList;    }    public int getCount() {        return coursetList.size();    }    public Object getItem(int position) {        return coursetList.get(position);    }    public long getItemId(int position) {        return position;    }    /**     * 通知adapter更新資料     */    @Override    public void notifyDataSetChanged() {        super.notifyDataSetChanged();    }    public View getView(int position, View convertView, ViewGroup parent) {        if (convertView == null) {            //這裡載入的每一個item條目的布局檔案            convertView = LayoutInflater.from(context).inflate(                    R.layout.student_score_item, null);        }        TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);        TextView tv_course = (TextView) convertView                .findViewById(R.id.tv_course);        TextView tv_score = (TextView) convertView.findViewById(R.id.tv_score);        // 獲得某一個位置的student        Course course = coursetList.get(position);        tv_name.setText(course.getStudentName() + "");        tv_course.setText(course.getCourseName() + "");        tv_score.setText(course.getCourseSocre() + "");        return convertView;    }}

ok!到此完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.