我需要在listview中设置适配器值。在其中,我创建了一个ArrayList来从SQLite DB获取数据,我需要将它传递给一个包含3列的listView。我在这里提到了我的代码。
我需要在哪里调用我的数据存储在本地Db中?
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;
}
在本课程中,我将获取所有数据并将其列入列表视图。
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
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;
}
答案 0 :(得分:0)
应该是直截了当的。只是做:
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 。