I am creating an app in android that takes a data collection and displays the collection to a list view. I completed the code but nothing is displayed in the view. Here is the code:
public class Activity extends ListActivity {
protected ArrayList<HashMap<String,String>> gameCollection = new ArrayList<HashMap<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Populate view with data
gameCollection.add(displayVideoGame("PlayStation","FIFA 14"));
gameCollection.add(displayVideoGame("PlayStation","Thief"));
gameCollection.add(displayVideoGame("PlayStation","Watch Dogs"));
gameCollection.add(displayVideoGame("PlayStation","Battlefield 4"));
gameCollection.add(displayVideoGame("PlayStation","Second Sun"));
gameCollection.add(displayVideoGame("PlayStation","Mario Brothers"));
gameCollection.add(displayVideoGame("PlayStation","Don't Starve"));
gameCollection.add(displayVideoGame("PlayStation","Elder Scrolls Online Beta"));
//Calling the Simple Adapter calling gameCollection List and setting the predefined layout
SimpleAdapter adapter = new SimpleAdapter(this,this.gameCollection,android.R.layout.simple_list_item_1, new String[] {"Playstation"},new int[] {android.R.id.text1});
//set adapter to collection
setListAdapter(adapter);
}
//Create Private Method - Returns HashMap with key-value pairs
private HashMap<String, String> displayVideoGame(String key, String value)
{
HashMap<String, String> videoGameHashMap = new HashMap<String, String>();
videoGameHashMap.put(key, value);
return videoGameHashMap;
}
Related
I have created a listview with externe layout like tweet and I want to set button clickable into all of the adapter like the X in photo:
Example when I click in button if I'm admin I delete the post
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
final ArrayList<HashMap<String, String>> listItem = new ArrayList<>();
FirebaseFirestore db = FirebaseFirestore.getInstance();
HashMap<String, String> map = new HashMap<>();
map.put("module", "Example");
map.put("years", "Year");
map.put("sections", "Section");
map.put("groupe", "Groupe");
map.put("urlImage",("Image"));
map.put("date","example");
listItem.add(map);
SimpleAdapter mSchedule = new SimpleAdapter
(getBaseContext(), listItem, R.layout.affichage,
new String[]{"avatar", "module", "years", "sections", "groupe"},
new int[]{R.id.avatar, R.id.module, R.id.years, R.id.sections, R.id.groupe});
ListView.setAdapter(mSchedule);
}
You have to implement CustomAdapter with BaseAdapter as base class.
You can check this link https://guides.codepath.com/android/Using-a-BaseAdapter-with-ListView for implementation details
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
How to access the widget id that is inserted into a layout that is loada in a listview by adapter?
Class:
public class PedidosListActivity extends Activity implements AdapterView.OnItemClickListener, SimpleAdapter.ViewBinder {
private static final List<Map<String, Object>> produtos = new ArrayList<Map<String, Object>>();
ListView listView;
NumberPicker _picker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_pedidos_confirmados);
Map<String, Object> mapProduto = (Map<String, Object>) getIntent().getSerializableExtra("produto");
Map<String, Object> item = new HashMap<String, Object>();
item.put("imagem_produto", R.drawable.batatafritas);
produtos.add(item);
String[] de = {"imagem_produto", "nome_produto", "peso", "preco"};
int[] para = {R.id.imagem_produto, R.id.nome_produto,
R.id.peso, R.id.preco};
listView = (ListView) findViewById(R.id.lv_id)
_picker = (NumberPicker) listView.findViewById(R.id.npQuantidade);
SimpleAdapter adapter = new SimpleAdapter(this, produtos, R.layout.listview_test, de, para);
adapter.setViewBinder(this);
listView.setAdapter(adapter);
In your custom adapter you can put a string extra so that when the user clicks the item in the ListView you pass the information you want.
I have a custom list view with image view and text view.I am loading text view data from SQLite Database and I want to add image from my drawable folder to the image view contain in list based on some condition
I am using cursor to load data to the list view.Please any one guide me how to do this
Very simple solution for that,using a simple adapter.
public class TestList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list);
ArrayList<Map<String, String>> arrlist = new ArrayList<Map<String, String>>();
Map<String, String> m = null;
for (int i = 0; i <=50; i++) {
m = new HashMap<String, String>();
m.put("key", String.valueOf(R.drawable.ic_launcher));
m.put("title", "Title-" + i);
m.put("subtitle", "SubTitle-" + i);
arrlist.add(m);
}
SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
R.layout.sample, new String[] { "key", "title", "subtitle" },
new int[] { R.id.imageView1, R.id.title, R.id.subtitle });
list.setAdapter(adapter);
}
}
Xml layouts: main xml layout has a listview with id-list,and sample.xml is your inflating layout,it has one image,two textviews.Modify the code as your requirement.
Hey guys I am a working on displaying a list of items on listview, the code I am using is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
ListView lv= (ListView)findViewById(R.id.listview);
rtrnList = new ArrayList<HashMap<String,String>>();
getmyLocation();
listclass = new listClass(offersobj);
listclass.populate();
rtrnList = listclass.getListArray();
adapter = new SimpleAdapter(
this,
rtrnList,
R.layout.custom_row_view,
new String[] {"Name","Msg","time"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
lv.setAdapter(adapter);
}
problem is say I am displaying three names Avinash, Arun, Rajesh. When application starts these three names are displayed on list. When I close and again start the application the values are repeating Avinash, Arun, Rajesh,Avinash, Arun, Rajesh. I am not able to figure out how to solve this.
The code you show seems fine. My guess is that listclass.populate() modifies offersobj and that offersobj is reused over several creations of your activity. So, whenever the activity is created, additional data is populated.
public class ListViewA extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"rowid", "col_1", "col_2", "col_3"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
}
for more example see this linkthis alsolistview about adapter, for create a hashmap, why bitmap type imported cannot show image in listview?What adapter shall I use to use HashMap in a ListView