i want to add some info from my database to a list view, i have been able to pass that information to a textView just don't know how to get it to a list view this is my code.
public class ViewClass extends Activity{
ListView myList;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
Initialize();
Database viewAgenda = new Database(this);
viewAgenda.open();
String data = viewAgenda.getAgendaData();
viewAgenda.close();
test.Add(data);
}
private void Initialize() {
// TODO Auto-generated method stub
myList = (ListView) findViewById(R.id.panel_bottom);
}
}
You can add the data you get from the Database in ArrayList and then add this arraylist in List
ArrayList<String> list_data = new ArrayList<>();
list_data.add("item"); // likewise add list items here
ArrayAdapter<String> adapter= new ArrayAdapter<>(context, android.R.layout.activity_list_item, list_data);
myList.setAdapter(adapter);
here is Example
http://wiki.remobjects.com/wiki/Displaying_Data_with_ListView_and_BaseAdapter_(Android)
http://androidsolution4u.blogspot.in/2013/09/android-populate-listview-from-sqlite.html
may be helps you.
You must use the adapter class to link a database with a ListView. Please see the below code.
ListView lv = (ListView) findViewById(The Id of your ListView);
//fetch your data to a Cursor
Cursor c = "The result of a select statement";
SimpleCursorAdapter adap = new SimpleCursorAdapter(this,
R.layout.item2,//Your layout for the list view
c,
new String[]{"fullname", "balance"}, //field names
new int[]{R.id.rowData, R.id.rowMoney});//The ids of elements from your list view layout. The field "fullname" will be loaded into the "R.id.rowData" element.
lv.setAdapter(adap);
Related
The following code consist of the listview function for displaying data from sqlite.
The issue is that no matter which data i selected on, it will only delete the last data and not the data being selected. How can i go about solving the issue? please help.
Thank you.
Arealist.java
public class Arealist extends AppCompatActivity {
public static ListView listView;
public static ArrayList<Area> list;
AreaListAdapter adapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.listView);
list = new ArrayList<>();
adapter = new AreaListAdapter(this, R.layout.area_items, list);
listView.setAdapter(adapter);
//get all data
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM AREA");
list.clear();
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
double area = cursor.getDouble(1);
String date = cursor.getString(2);
list.add(new Area(id, area, date));
}
adapter.notifyDataSetChanged();
}
It seems your problem is in this line MainActivity.sqLiteHelper.deleteData(Area.getId());
Try with this
MainActivity.sqLiteHelper.deleteData(area.getId());
Make Area Object local.
set Area Id in delete buttons as a Tag and get it in onClick().
Add In getView()
holder.btnDelete.setTag(area.getId());
Add In OnClick()
final int areaId = view.getTag();
Add in AlertDialog:
MainActivity.sqLiteHelper.deleteData(areaId);
I'm following the following tutorial http://www.devexchanges.info/2015/03/combining-gridview-and-listview-in-one.html
I'm attempting to change the list view to use an array of strings, and display text rather than images for the bottom part of this application.
I'm not entirely sure what I should be using as my ListView ID here. It seems like I can only use a findViewbyID, but I'd rather just give it an array of strings to populate that list view. Am I missing something simple here?
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
ListView lv = (ListView)findViewById(????);
lv.setAdapter(adapter);
Here is the code
public class ListViewActivity extends Activity {
private ExpandableHeightGridView gridView;
private ListView listView;
//drawables array for listview
private static final int[] corpImage = { R.drawable.apple, R.drawable.blackberry_logo,
R.drawable.google, R.drawable.microsoft, R.drawable.mozilla, R.drawable.oracle };
static final String[] CORPS = new String[] { "Microsoft", "Google", "Apple" };
//drawables array for gridview
private static final int[] osImage = { R.drawable.bbos, R.drawable.firefox_os,
R.drawable.ic_launcher, R.drawable.ios, R.drawable.tizen, R.drawable.ubuntu_touch,
R.drawable.wpos, R.drawable.symbian };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
addGridViewAsListViewHeader();
setAdapters();
}
/**
* set header for listview (it's a gridview)
*/
#SuppressLint("InflateParams")
private void addGridViewAsListViewHeader() {
View header = (View) getLayoutInflater().inflate(R.layout.layout_gridview, null);
listView.addHeaderView(header);
gridView = (ExpandableHeightGridView) header.findViewById(R.id.gridview);
// set expand to show all gridview rows
gridView.setExpanded(true);
}
/**
* set adapters for list view and gridview
*/
private void setAdapters() {
// convert int array to Integer array by apache common lang library
Integer[] osImageList = ArrayUtils.toObject(osImage);
Integer[] corpImageList = ArrayUtils.toObject(corpImage);
//Added by me but not working correctly
//ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.item_grid, CORPS);
//ListView lv = (ListView)findViewById(R.id.image);
// lv.setAdapter(adapter);
// set listview and gridview adapters
CustomAdapter gridViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, osImageList);
CustomAdapter listViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, corpImageList);
gridView.setAdapter(gridViewAdapter);
listView.setAdapter(listViewAdapter);
}
}
If your goal is to only use one string in each line, and display it as a list of items you can simply do this by using array adapters as follows.
ArrayList<String> companies = new ArrayList<>();
companies.add("Google");
companies.add("Microsoft");
// Find a reference to the {#link ListView} in the layout
ListView companiesListView = (ListView) findViewById(R.id.list);
// Create a new {#link ArrayAdapter} of earthquakes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, earthquakes);
// Set the adapter on the {#link ListView}
// so the list can be populated in the user interface
companiesListView.setAdapter(adapter);
In your layout file you should declare a list view where you want to display this list of items. In this case id of that list view would be "list"
I am working on health related app. In ListView I want to assign value to each item. For example milk contains 21 calories so I want to assign 21 to ListView item milk.
Here is my activity code containing ListView.
public class FoodEntry extends AppCompatActivity {
// Array of strings...
String[] food = {"Naan","Pav bhaji","chole tikiya", "rice", "soybean", "milk", "curd"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_entry);
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.activity_item, food);
ListView listViewFoodItems = (ListView)findViewById(R.id.listViewFood);
listViewFoodItems.setAdapter(adapter);
}
}
Instead of using a String[] to store your data like you're doing here:
// Array of strings...
String[] food = {"Naan","Pav bhaji","chole tikiya", "rice", "soybean", "milk", "curd"};
Use a new object, such as CalorieCount.
// Array of CalorieCount
CalorieCount[] food = { new CalorieCount("Naan", 20) ... };
You can use SimpleAdapter with Hashmap
foodItems= new ArrayList<HashMap<String, String>>();
//create first item object
HashMap<String, String> item1= new HashMap<String, String>();
item1.put("name", "Naan");
item1.put("calories", "21");
foodItems.add(item1)//add multiple items like this
String[] from = { "name"};
// view id's to which data to be binded
int[] to = { R.id.name};
//Creating Adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, foodItems,
R.layout.activity_item, from, to);
//Setting Adapter to ListView
listViewFoodItems.setAdapter(adapter);
To know more visit How SimpleAdapter Binds Hashmap Data to items of ListView
I have a dummy listview that I have setup in my app that uses the following code:
public class Roster extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roster);
// ListView resource.
mainListView = (ListView) findViewById( R.id.rosterListView );
// List of names.
String[] players = new String[] { "Dude1", "Guy2"};
ArrayList<String> playerlist = new ArrayList<String>();
playerlist.addAll( Arrays.asList(players) );
//ArrayAdapter using the list.
listAdapter = new ArrayAdapter<String>(this, R.layout.rosterrow, playerlist);
// Add more Players, If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Person8" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
getActionBar().setDisplayHomeAsUpEnabled(true);
}
which gives me a listview of three players.
I now want to use the json object to populate the list instead of using a string. How can I populate the list using my json object?
Try to get the value of Json by key and put it in to string array.
JsonObject myjsondata = new JsonObject(jsondata.toString());
String[] players = new String[] {myjsondata.getString("key") , myjsondata.getString("key1")};
The problem i am facing is that i have my code is executing "else" block even when the "if " condition is true.
public class MsgNewPackage extends Activity {
private DatabaseHandler dbhandler;
private SimpleCursorAdapter dataAdapter;
private ListView listView;
String PKG_NAME,PKG_DUR,PKG_PRICE,PKG_ITEMS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_msg_new_package);
dbhandler = new DatabaseHandler(this);
displayListView();
}
private void displayListView()
{
String check= "Mobilink";
Intent intent = getIntent();
String conn = intent.getStringExtra("NET_CONN");
// Toast.makeText(getApplicationContext(), conn, Toast.LENGTH_SHORT).show();
if(conn == check)
{
Cursor cursor = dbhandler.fetchAllMOBSMSPackages();
String[] columns = new String[]{DatabaseHandler.KEY_PKG_NAME,DatabaseHandler.KEY_NO_OF_FREE_ITEMS,DatabaseHandler.KEY_DURATION,DatabaseHandler.KEY_CHARGES};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.title,R.id.no_of_item,R.id.duration,R.id.pkgcharges};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row2, cursor,columns,to);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
else
{
Cursor cursor = dbhandler.fetchAllSMSPackages();
String[] columns = new String[]{DatabaseHandler.KEY_PKG_NAME,DatabaseHandler.KEY_NO_OF_FREE_ITEMS,DatabaseHandler.KEY_DURATION,DatabaseHandler.KEY_CHARGES};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.title,R.id.no_of_item,R.id.duration,R.id.pkgcharges};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row2, cursor,columns,to);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
The toast displays that conn has "Mobilink" but the listview gets populated with results of fetchAllSMSPackages() (i.e function called in else block ) which should not be the case. Pleas help.
Try using conn.equals(check) instead of conn == check.
Strings in Java are objects, and not primitives, so you cannot compare their value using ==. This will compare the object as a whole, and not the text.
You should use .equals() when comparing String values. Not ==.
Try using:
conn.equals(check);