Add button in listview with externe layout - android

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

Related

How to access the widget id that is inserted into a layout that is load in a 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.

Collection does not display on ListView

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;
}

add subitems in listview in android

I have a listvew and I add subitem into the listview like this code
public class MyCustomListView extends Activity {
/** Called when the activity is first created. */
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
lv=(ListView) this.findViewById(R.id.lvvv);
SimpleAdapter adapter = new SimpleAdapter(this,list, R.layout.custom_row_view,new String[] {"pen","color"},
new int[] {R.id.text1, R.id.text3}
);
populateList();
lv.setAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("pen","MONT Blanc");
temp.put("color", "Black");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("pen","Gucci");
temp1.put("color", "Red");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("pen","Parker");
temp2.put("color", "Blue");
}
}
but in the color key, if I have a lot of colors for a pen, and I want to arrange the colors into a list and this color list is under the pen.
ex:
-pen: Parker.
+color: blue.
+color: red.
how should I do that? please help me!
thank alot.
You need to use an ExpanadableListView. Check out this -
ExpanadaleListView Tutorial

How to add Image to the Custom List View in Android?

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.

repeating ListView

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

Categories

Resources