我正在尝试在列表视图应用中简单显示App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $paginate = array(
'limit' => 4,
'order' => 'User.id DESC'
);
public function admin_userAdd() {
if ($this->request->is('post')) {
pr($this->request->data);
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('The user has been saved');
return $this->redirect(array('action' => 'userList'));
}
$this->Session->setFlash('The user could not be saved. Please, try again.', 'error');
}
}
public function admin_userList(){
$users = $this->User->find('all');
$this->set('users', $this->paginate());
}
public function admin_userEdit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
//pr($this->request->data); die();
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('action' => 'userList'));
}
$this->Session->setFlash(
__('The user could not be saved. Please, try again.')
);
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
public function admin_passChange($id = null) {
$this->User->id = $id;
if (!empty($this->request->data)) {
$password = $this->request->data['User']['pass1'];
if ($this->User->saveField('password',$password)) {
$this->Session->setFlash('Password has been changed.');
//return $this->redirect(array('action' => 'userEdit'));
} else {
$this->Session->setFlash('Password could not be changed.');
}
} else {
$this->request->data = $this->User->read(null, $id);
}
}
}
中的所有记录。编译器没有检测到任何错误,但是当我运行应用程序时,它会收到运行时错误。
main_Activity.java
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $validate = array(
'username' => array(
'nonEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required',
'allowEmpty' => false
),
'between' => array(
'rule' => array('between', 4, 15),
'required' => true,
'message' => 'Usernames must be between 5 to 15 characters'
),
'unique' => array(
'on' => 'create',
'rule' => array('isUniqueUsername'),
'message' => 'This username is already in use'
),
'alphaNumericDashUnderscore' => array(
'rule' => array('alphaNumericDashUnderscore'),
'message' => 'Username can only be letters, numbers and underscores'
),
),
'email' => array(
'required' => array(
'on' => 'create',
'rule' => array('email', true),
'message' => 'Please provide a valid email address.'
),
'unique' => array(
'on' => 'create',
'rule' => array('isUniqueEmail'),
'message' => 'This email is already in use',
),
'between' => array(
'rule' => array('between', 6, 60),
'message' => 'Usernames must be between 6 to 60 characters'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'editor', 'accountent')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
),
'password' => array(
'length' => array(
'rule' => array('between', 5, 40),
'message' => 'Your password must be between 5 and 40 characters.',
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password.'
)
),
'pass1' => array(
'length' => array(
'rule' => array('between', 8, 40),
'message' => 'Your password must be between 5 and 40 characters.',
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password.'
)
)
);
public function beforeSave($options = array()) {
// hash our password
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
// if we get a new password, hash it
if (isset($this->data[$this->alias]['pass1'])) {
$this->data[$this->alias]['pass1'] = AuthComponent::password($this->data[$this->alias]['pass1']);
}
return true;
// fallback to our parent
return parent::beforeSave($options);
}
}
Show_Record.java(这是显示listview中所有用户详细信息的主类)
<?php echo $this->Session->flash(); ?>
<div class="row" style="float:right">
Hi <?php echo $this->data['User']['username']; ?>
</div>
<div class="row">
<div class="col-lg-6">
<?php echo $this->Form->create('User'); ?>
<?php
echo $this->Form->input('pass1', array('label' =>'New Password', 'class' => 'form-control'));
echo $this->Form->input('pass2', array('label' =>'Confirm Password', 'class' => 'form-control'));
?>
<?php echo $this->Form->end(__('Submit', array('class' => 'btn btn-default'))); ?>
</div>
show_recordAdapter.java
这是自定义适配器类
SQLite database
Databaseclass(其中定义了表的所有文件结构以及定义的方法)
package com.example.login_userauthentication;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Register(View v)
{
startActivity(new Intent(getApplicationContext(),Reg.class));
}
public void Login(View v)
{
startActivity(new Intent(getApplicationContext(),Login.class));
}
public void Show_allrecord(View v)
{
startActivity(new Intent(getApplicationContext(),Show_record.class));
}
public void update_record(View v)
{
startActivity(new Intent(getApplicationContext(),Update.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Database_helper.java(其中定义了create table和create database)
package com.example.login_userauthentication;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
public class Show_record extends Activity
{
Show_recordAdapter showrecord_adapter;
ListView listview;
ArrayList<Student> studentlist;
Database helper=new Database();
protected void onCreate(Bundle savedInstanceState)
{
System.out.println("show sumit");
super.onCreate(savedInstanceState);
setContentView(R.layout.show_recordlist);
System.out.println("show_record activity");
System.out.println("show sumit");
helper=new Database();
listview = (ListView) findViewById(R.id.listView1);
studentlist=new ArrayList<Student>();
studentlist = helper.getAllrecord();
System.out.println(studentlist);
System.out.println("show sumit");
showrecord_adapter=new Show_recordAdapter(getApplicationContext(),studentlist);
listview.setAdapter(showrecord_adapter);
// listview.setAdapter(new Show_recordAdapter(this, studentlist,
// getLayoutInflater()));
}
}
package com.example.login_userauthentication;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
这是logcat
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class Show_recordAdapter extends BaseAdapter
{
Context context;
ArrayList<Student> empList;
private static LayoutInflater inflater = null;
public Show_recordAdapter(Context context,ArrayList<Student> empList)
{
context=this.context;
empList=this.empList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return empList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return empList.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//final ViewItem item;
if(convertView==null)
convertView = inflater.inflate(R.layout.listview_iteams, null);
//item=new ViewItem();
TextView UemailTextview=(TextView)convertView.findViewById(R.id.uemail);
TextView upassTextview=(TextView)convertView.findViewById(R.id.upass);
TextView UnameTextview=(TextView)convertView.findViewById(R.id.uname);
TextView UmobileTextview=(TextView)convertView.findViewById(R.id.umobile);
// convertView.setTag(item);
// }
// else
// {
// item=(ViewItem)convertView.getTag();
// }
Student student=new Student();
student=empList.get(position);
UnameTextview.setText("emailid: "+student.getEmailid());
upassTextview.setText("password: "+student.getPassword());
UnameTextview.setText("name: "+student.getName());
UmobileTextview.setText("mobile: "+student.getMobileno());
return convertView;
}
// private class ViewItem
// {
// TextView UemailTextview;
// TextView upassTextview;
// TextView UnameTextview;
// TextView UmobileTextview;
// }
}
答案 0 :(得分:2)
Inside Database类getAllrecord()context1传递的是 null 。
dbhelper = new Databse_helper(context1);//here context1 is null
您可以尝试在数据库类中将上下文传递给 getAllrecord(上下文context1) 然后可以打电话
studentlist = helper.getAllrecord(Show_record.this);
。希望这有助于解决您的问题。