如何将Arraylist中的SQLite db值获取到Listview [英] How to get SQLite db values in Arraylist to Listview

查看:77
本文介绍了如何将Arraylist中的SQLite db值获取到Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在列表视图中设置适配器值.在其中,我创建了一个ArrayList来从SQLite DB获取数据,我需要将其传递给具有3列的listView.在这里我提到了我的代码.

I need to set adapter values in a listview. In it, I created an ArrayList to get data from SQLite DB and I need to pass it to a listView with 3 columns. Here I mentioned my codes.

我需要在哪里调用存储在本地Db中的数据?

Where do I need to call my data Stored in local Db?

DBHelper.java

DBHelper.java

 public ArrayList<SQLPojo.UserDetails> getUserDetails() {

    ArrayList<SQLPojo.UserDetails> aUserDetailList = new ArrayList<SQLPojo.UserDetails>();

    try {

      String aQuery = "SELECT * FROM " + TABLE_NAME;

      Cursor aCursor = mySqliteDataBase.rawQuery(aQuery, null);

      aCursor.moveToFirst();
      if(aCursor.getCount() > 0) {
        SQLPojo.UserDetails aUserDetails = myReturnValues.getUserDetailInstance();
        aUserDetails.setId(aCursor.getString(aCursor
                .getColumnIndex("ID")));
        aUserDetails.setUsername(aCursor.getString(aCursor
                .getColumnIndex("USERNAME")));
        aUserDetails.setPlace(aCursor.getString(aCursor
                .getColumnIndex("PLACE")));
        aUserDetailList.add( aUserDetails );
      }
      aCursor.close();
    }
    catch(android.database.SQLException e)
    {
      e.printStackTrace();
    }
    return aUserDetailList;
  }

在本课程中,我将获取所有数据并将其列出到列表视图中.

In this class, I am going to get all data and list it to the listview.

GetAllValues.java

GetAllValues.java

public class SQLViewAllData extends Activity {

private SQLDbHelper myDbHelper;
private SQLCustomViewAllDataAdapter myAdapter;
private ListView myListView;
private ArrayList<SQLPojo> aList = new ArrayList<>();
private Activity myContext;

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

    widgetInitialization();
    viewAllData();
}
//Initializing Widgets
public void widgetInitialization() {
    myDbHelper = new SQLDbHelper( myContext );
    myListView = (ListView)findViewById( R.id.listview_view_data_LV );
}

public ArrayList<SQLPojo.UserDetails> getUserInfo() {
    return myDbHelper.getUserDetails();
}

//view all data stored in DB
public void viewAllData( ) {
    myAdapter = new SQLCustomViewAllDataAdapter( SQLViewAllData.this,aList);
    myListView.setAdapter( myAdapter );
}}

这是我的适配器类,用于获取所有值 CustomAdapter.java

This is my Adapter Class to get all values CustomAdapter.java

 public View getView( int aPosition, View aConvertView, ViewGroup aParent ) {

LayoutInflater inflater = myActivity.getLayoutInflater();
ViewHolder aHolder;

if( aConvertView == null ) {
  aConvertView = inflater.inflate( R.layout.layout_inflate_custom_listview_view_data, null );

  aHolder = new ViewHolder();

  aHolder.aTextViewID = ( TextView )aConvertView.findViewById( R.id.custom_listview_view_data_ID );
  aHolder.aTextViewUN = ( TextView )aConvertView.findViewById( R.id.custom_listview_view_data_UN );
  aHolder.aTextViewPLC = ( TextView )aConvertView.findViewById( R.id.custom_listview_view_data_Place );
  aConvertView.setTag( aHolder );
}
else {
  aHolder = (ViewHolder) aConvertView.getTag();
}

aHolder.aTextViewID.setText(myData.get(aPosition).toString());
aHolder.aTextViewUN.setText(myData.get(aPosition).toString());
aHolder.aTextViewPLC.setText(myData.get(aPosition).toString());

return aConvertView;

}

推荐答案

应该简单明了.只要做:

It should be straight forward. Just do:

public void viewAllData( ) {
  aList = getUserInfo(); //this is where you call the local database.
  myAdapter = new SQLCustomViewAllDataAdapter( SQLViewAllData.this,aList);
  myListView.setAdapter( myAdapter );
}

还要确保将aList从类型 SQLPojo 更改为 SQLPojo.UserDetails .

Also, make sure you change aList from type SQLPojo to SQLPojo.UserDetails.

这篇关于如何将Arraylist中的SQLite db值获取到Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆