ListView in Android

First, the first step: create an Activity

package com.xw.firstapp.shake;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.xw.firstapp.R;
import com.xw.firstapp.shake.db.StudentTableOperator;

public class AllRecordActivity extends Activity {

	private StudentListViewAdapter adapter;

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

	@Override
	protected void onResume() {
		super.onResume();
		updateList();
	}

	private void initListView() {
		adapter = new StudentListViewAdapter(this, null);
		ListView l = (ListView) findViewById(R.id.aty_student_list);
		l.setAdapter(adapter); //Bind adapter
	}

	private void updateList() {
                //Query the list of all data set to the adapter from the database
		adapter.setData(StudentTableOperator.getInstance(this).getAllStudents());
	}
}

layout file:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.xw.firstapp.shake.AllRecordActivity" >

    <ListView
        android:id="@+id/aty_student_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
    </ListView>

</LinearLayout>

Second, the second step: create a data model item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/item_list_view_student_l"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item_list_student_numbers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:" />

        <TextView
            android:id="@+id/item_list_student_remarks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:maxLines="1"
            android:text="Remarks:" />
    </LinearLayout>

    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/item_list_student_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="time:" />

</LinearLayout>

Third, the third step: create an adapter adapter

package com.xw.firstapp.shake;

import java.util.List;

import com.xw.firstapp.R;
import com.xw.firstapp.shake.db.StudentTableOperator;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StudentListViewAdapter extends BaseAdapter implements
		View.OnClickListener, View.OnLongClickListener {//Implement click and long press event interface
	private List<Student> students;
	private Context context;

	public StudentListViewAdapter(Context context, List<Student> students) {
		this.context = context;
		this.students = students;
	}

	public void setData(List<Student> students) {
		this.students = students;
		notifyDataSetChanged();
	}

	public List<Student> getStudents() {
		return this.students;
	}

	public int getCount() {
		return students == null ? 0 : students.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_list_view_student, null, false);
			holder = new ViewHolder(convertView);
			// Set up long press event and click event listeners
			holder.l.setOnClickListener((OnClickListener) this);
			holder.l.setOnLongClickListener((OnLongClickListener) this);
			convertView.setTag (holder);
		}
		holder = (ViewHolder) convertView.getTag();
		Student student = students.get(position);
		holder.tv_numbers.setText("学号:31671102" + student.getNumbers());
		holder.tv_remarks.setText(student.getRemarks());
		holder.tv_time.setText(student.getTime());
		holder.l.setTag(position);
		return convertView;
	}

	public void onClick(View v) {
		int p = (Integer) v.getTag();
		Intent intent = new Intent(context, ParticularsActivity.class);
		intent.putExtra("id", students.get(p).get_Id());
		context.startActivity(intent);
	}

	public boolean onLongClick(final View v) {
		final int p = (Integer) v.getTag();
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Operation").setItems(new String[] { "Delete record" },
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:// Click to delete the record
							int id = getStudents().get(p).get_Id();
							long rows = StudentTableOperator.getInstance(
									context).deleteById(id);
							if (rows > 0) {
								getStudents().remove(p);
								notifyDataSetChanged();
							}
							break;
						}
					}
				});
		builder.create().show();
		return true;
	}

	private class ViewHolder {
		private TextView tv_numbers, tv_remarks, tv_time;
		private LinearLayout l;

		public ViewHolder(View v) {
			tv_numbers = (TextView) v
					.findViewById(R.id.item_list_student_numbers);
			tv_remarks = (TextView) v
					.findViewById(R.id.item_list_student_remarks);
			tv_time = (TextView) v.findViewById(R.id.item_list_student_time);
			l = (LinearLayout) v.findViewById(R.id.item_list_view_student_l);
		}
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812695&siteId=291194637