选择ListView项目并在Android中显示来自DB其他活动的用户信息

时间:2016-02-16 07:02:41

标签: android sqlite listview android-database

我想在listview项目中显示一些用户信息,当点击项目时,将完整的用户信息显示到另一个活动中(例如将数据从数据库传递到其他活动)。我使用SQLiteDataBase来保存用户信息。但我不知道怎么办?!
例如:点击用户5时,在其他活动中显示用户的5个完整信息!

注意:请不要让我失望,不要给我负面评价。 我在网上搜索但找不到合适的方法,我在这里提出了问题。请指导我,因为我是业余爱好者。

主页代码:

public class MainPage extends AppCompatActivity {

    private ListView listView;
    private UserDbHelper userDbHelper;
    private SQLiteDatabase sqLiteDatabase;
    private Cursor cursor;
    private ListDataAdapter listDataAdapter;
    private DataProvider dataProvider;
    private TextView dataCount_tv;

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

        userDbHelper = new UserDbHelper(getApplicationContext());
        sqLiteDatabase = userDbHelper.getReadableDatabase();
        cursor = userDbHelper.getUserInfo(sqLiteDatabase);

        listView = (ListView) findViewById(R.id.list);
        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.list_row_layout);
        listView.setAdapter(listDataAdapter);

        if (cursor.moveToFirst()) {
            do {
                String name, family, password, phone, email;
                name = cursor.getString(0);
                family = cursor.getString(1);
                password = cursor.getString(2);
                phone = cursor.getString(3);
                email = cursor.getString(4);
                dataProvider = new DataProvider(name, family, password, phone, email);
                listDataAdapter.add(dataProvider);

            } while (cursor.moveToNext());
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                /// Code ... ?!!!
            }
        });

        dataCount_tv = (TextView) findViewById(R.id.main_dataCount_text);
        dataCount_tv.setText("Count of Data : " + listDataAdapter.getCount());
    }

    public void search_button(View view){

        startActivity(new Intent(getApplicationContext(), SearchPage.class));
    }
}

用户页码:

public class UserPage extends AppCompatActivity {

    private TextView user_name_tv, user_family_tv, user_phone_tv, user_email_tv;
    private FloatingActionButton user_delete_btn, user_edit_btn;

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

        user_name_tv = (TextView) findViewById(R.id.user_info_name);
        user_family_tv = (TextView) findViewById(R.id.user_info_family);
        user_phone_tv = (TextView) findViewById(R.id.user_phone_text);
        user_email_tv = (TextView) findViewById(R.id.user_email_text);


    }
}

ListView适配器代码:

public class ListDataAdapter extends ArrayAdapter {

    List list = new ArrayList();

    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class LayoutHandler {

        TextView NAME, FAMILY, PASSWORD, PHONE, EMAIL;
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        LayoutHandler layoutHandler;
        if (row == null) {

            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.list_row_layout, parent, false);
            layoutHandler = new LayoutHandler();
            layoutHandler.NAME = (TextView) row.findViewById(R.id.list_user_name);
            layoutHandler.FAMILY = (TextView) row.findViewById(R.id.list_user_family);
            layoutHandler.PASSWORD = (TextView) row.findViewById(R.id.list_user_password);
            layoutHandler.PHONE = (TextView) row.findViewById(R.id.list_user_phone);
            layoutHandler.EMAIL = (TextView) row.findViewById(R.id.list_user_email);
            row.setTag(layoutHandler);

        } else {

            layoutHandler = (LayoutHandler) row.getTag();
        }
        DataProvider dataProvider = (DataProvider) this.getItem(position);
        layoutHandler.NAME.setText(dataProvider.getName());
        layoutHandler.FAMILY.setText(dataProvider.getFamily());
        layoutHandler.PASSWORD.setText(dataProvider.getPassword());
        layoutHandler.PHONE.setText(dataProvider.getPhone());
        layoutHandler.EMAIL.setText(dataProvider.getEmail());

        return row;
    }
}

SQLiteOpenHelper代码:

public class UserDbHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "UserInfoDB.db";
    private static final int DATABASE_VERSION = 1;

    private static final String CREATE_QUERY = "CREATE TABLE " + UserContract.NewUserInfo.TABLE_NAME + "(" +
            UserContract.NewUserInfo.USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            UserContract.NewUserInfo.USER_NAME + " TEXT," +
            UserContract.NewUserInfo.USER_FAMILY + " TEXT," +
            UserContract.NewUserInfo.USER_PASSWORD + " TEXT," +
            UserContract.NewUserInfo.USER_PHONE + " TEXT," +
            UserContract.NewUserInfo.USER_EMAIL + " TEXT);";

    public UserDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e("DATABASE OPERATION : ", "Database Create / Open...");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_QUERY);
        Log.e("DATABASE OPERATION : ", "Table Create...");
    }


    public void AddUserInfo(String name, String family, String password, String phone, String email, SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(UserContract.NewUserInfo.USER_NAME, name);
        contentValues.put(UserContract.NewUserInfo.USER_FAMILY, family);
        contentValues.put(UserContract.NewUserInfo.USER_PASSWORD, password);
        contentValues.put(UserContract.NewUserInfo.USER_PHONE, phone);
        contentValues.put(UserContract.NewUserInfo.USER_EMAIL, email);

        db.insert(UserContract.NewUserInfo.TABLE_NAME, null, contentValues);
        Log.e("DATABASE OPERATION : ", "One row inserted...");
    }

    public Cursor getUserInfo(SQLiteDatabase db) {

        Cursor cursor;
        String[] projections = {UserContract.NewUserInfo.USER_NAME, UserContract.NewUserInfo.USER_FAMILY,
                UserContract.NewUserInfo.USER_PASSWORD, UserContract.NewUserInfo.USER_PHONE,
                UserContract.NewUserInfo.USER_EMAIL};
        String list_sort = UserContract.NewUserInfo.USER_ID + " DESC";

        cursor = db.query(UserContract.NewUserInfo.TABLE_NAME, projections, null, null, null, null, list_sort);
        return cursor;
    }

    public Cursor getContacts(String user_name, SQLiteDatabase db) {

        String[] projections = {UserContract.NewUserInfo.USER_FAMILY, UserContract.NewUserInfo.USER_PASSWORD,
                UserContract.NewUserInfo.USER_PHONE, UserContract.NewUserInfo.USER_EMAIL};
        String selection = UserContract.NewUserInfo.USER_NAME + " LIKE ?";
        String[] selection_args = {user_name};
        Cursor cursor = db.query(UserContract.NewUserInfo.TABLE_NAME, projections, selection, selection_args, null, null, null);

        return cursor;
    }

    public Boolean login(String username, String password, SQLiteDatabase db) throws SQLException {

        Cursor cursor = db.rawQuery("SELECT * FROM " + UserContract.NewUserInfo.TABLE_NAME +
                " WHERE " + UserContract.NewUserInfo.USER_NAME + "=? AND " +
                UserContract.NewUserInfo.USER_PASSWORD + "=?", new String[]{username, password});

        if (cursor != null) {
            if (cursor.getCount() > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

DataProvider类代码:

public class DataProvider {

    private String name, family, password, phone, email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public DataProvider(String name, String family, String password, String phone, String email) {

        this.name = name;
        this.family = family;
        this.password = password;
        this.phone = phone;
        this.email = email;

    }

}

请帮帮我,因为我需要这个教程。 tnx适用于所有亲爱的&lt; 3

2 个答案:

答案 0 :(得分:0)

你的dataprovider对象类

public class DataProvider implements Serializable {

private String name, family, password, phone, email;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getFamily() {
    return family;
}

public void setFamily(String family) {
    this.family = family;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public DataProvider(String name, String family, String password, String phone, String email) {

    this.name = name;
    this.family = family;
    this.password = password;
    this.phone = phone;
    this.email = email;

}
}

和onItemclick方法

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           DataProvider dataprovider = (DataProvider)listDataadapter.getItem(position);
           Bundle bundle = new Bundle();
           bundle.putSerializable("anyname",dataprovider);
           Intent intent = new Intent(MainPage.this,yoursecondactivity.class); 
           intent.putExtras(bundle);
          startActivity(intent);

        }
    });

在第二个活动中,您需要使用

获取此参数

getIntent()getExtras();

并检查它是否为null,如果不为null,则将其存储在dataprovider对象

答案 1 :(得分:0)

基本上,您需要将ListActivity中的用户信息传递给ProfileACtivity。有两种方法可以实现这一目标:

  1. 使用Parceable模型
  2. 创建一个parceable模型。让我们称之为UserModel。 Android Studio有助于创建一个Parceable类。您只需要使UserModel实现Parceable接口。 (什么是Parceable接口 - &gt;它有助于将对象从一个组件传递到另一个组件。您可以在线查找更多信息)

    现在使用以下代码在Intent中传递它

    intent.putExtra("user", myUserModel);
    

    在您的个人资料活动中,您可以使用以下方式获取此对象:

    UserModel userModel = getIntent().getExtra("user");
    //Now populate your views using the model you just obtained from the intent
    
    1. 将id传递给activity并从ProfileACtivity中的数据库中获取完整的UserModel:

      intent.putExtra(&#34; id&#34;,id);

    2. ProfileActivity:

      int id = getIntent().getExtra("id");
      
      UserModel userModel = mMySQLiteHelper.getUserData(id);
      //Populate your views using this model.
      

      请注意,我还没有通过您的代码。但我认为您可以轻松地在代码中容纳这些方法中的任何一种来实现相同的目标。

      如果您需要更多帮助,请与我们联系。