I want to set color for particular row in listview.That row will know at runtime. I ahve done list view like this :
ArrayList<SalesRoutes> routeList = getSalesRoute();
ArrayList<HashMap<String, String>> routhPath = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < routeList.size(); i++) {
if(Integer.parseInt(routeList.get(i).getOutlets()) >0){
HashMap<String, String> map = new HashMap<String, String>();
map.put("routeCode",((SalesRoutes) routeList.get(i)).getRouteCode());
map.put("routeName",((SalesRoutes) routeList.get(i)).getDescription());
map.put("outlets", ((SalesRoutes) routeList.get(i)).getOutlets());
routhPath.add(map);
}
}
ListView list = getListView();
sd = new SimpleAdapter(this, routhPath, R.layout.route_path,new String[] {"routeCode","routeName","outlets" },new int[] { R.id.routeCode,R.id.routeName,R.id.outlets});
row = getLayoutInflater().inflate(R.layout.route_path_row, null, false);
getListView().addHeaderView(row);
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
//list.setSelection(0);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setItemChecked(positions, true);
list.setSelectionAfterHeaderView();
Please tell me how can i do this...
Thanks in advance
One way is to use the index of the row you want to get like
getListView().getChildAt(index).setBackground(#ff0000);
Otherwise you would need to create a custom adapter and overwrite the getView method which is called before rendering each row. You can use that to check any conditions and set the background accordingly.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The above is a tutorial about that.
Related
I'm trying to populate a ListView using a HashMap and a SimpleAdapter from a nested array with 12 columns and 12 rows
So here is my code
try {
String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
cursor.moveToFirst();
for (int i =0; i<cursor.getCount();i++){
for(int j = 0; j < cursor.getColumnNames().length; j++) {
String uname = cursor.getString(j);
array2[i][j]=uname;
}
cursor.moveToNext();
}
} finally {
cursor.close();
}
and then the simple adapter with hashmap
int[] to = new int[]{R.id.item2, R.id.item3, R.id.item4, R.id.item5, R.id.item6, R.id.item7, R.id.item8 ,R.id.item9, R.id.item10, R.id.item11, R.id.item12};
List<HashMap<String,String>> fillMaps = (List<HashMap<String, String>>) new ArrayList<HashMap<String, String>>();
for(int i = 0; i<10;i++){
HashMap<String,ArrayList> map = new HashMap<String, ArrayList>();
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, array2, to);
when I try this I get an error
SimpleAdapter(android.content.Context, java.util.List<? extends java.util.Map<java.lang.String,?>>, int, java.lang.String[], int[])' in android.widget.SimpleAdapter cannot be applied to
(com.example.thevenom1215.prueba.MainActivity, java.util.List<java.util.HashMap<java.lang.String, java.lang.String>>, int, java.util.ArrayList<java.lang.String>, int[])
Or I thought that I could convert the nested array to a simple array is that possible? If so how can I accomplish that? I tried this
String from [] = new String from[252]:
from[0]= array[0][1]+"|"+array[0][2]+"|"+array[0][3]+......+array[0][12];
but it doesn't work.
In the array2, which is array2[12][21], every row has 12 columns, which are information of a person, for example (name, age)
Cesar Joel Gurrola, xx
The first row of the array is: [Cesar, Joel, Gurrola, xx]
I need that in an array because further in my code I need String by String and not a whole String "Cesar, Joel, Gurrola, xx"
Sql query
sql="select b.CVE_CONTR, r.NO_RECIBO , a.NOM_SOLICIT ,r.NO_PARCIAL ,r.SDO_TOTAL, r.STS_PAGO ,a.AP_PATSOLICIT,a.AP_MATSOLICIT, " +
"f.DESCRIPCION, b.NO_PARCIALID , b.PAGO_PARCIAL, b.TOT_APAG from MFRF_M_SOLICITANTES a, MFRF_M_CONTPREV_H b, MFRF_M_CONTPREV_D_RECGEN_2 r," +
"C_PAQUETE f , C_PARCIALIDADES g, MFRF_C_COLONIAS c where b.CVE_CONTR = '"+etnumcontrato.getText().toString() + "' and r.STS_PAGO in ('1','10','11','12')" +
"and c.ID_COLONIA = a.ID_COLONIA and f.ID_PAQUETE = b.ID_PAQUETE and g.ID_PARCIALIDAD = b.ID_PARCIAL AND a.ID_SOLICIT = b.ID_SOLICITANTE ";
Overall, I wouldn't recommend this.
String [][] array2 = new String[cursor.getCount()][cursor.getColumnCount()];
An adapter displays one row-item at a time, not the whole table.
I would instead recommend a CursorAdapter since you do have a Cursor object, but I'll continue with a SimpleAdapter.
First, you need to create an XML layout, let it be named item_layout.xml. It contains all the Views that you want to bind the row from the database to. I believe these must all be TextViews for a SimpleAdapter.
String[] from = new String[] {"rowid", "fname", "mname", "lname"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
Then, loop over the rows of the cursor, and pull out the necessary information into a List of HashMaps. Note: this is one row of data each time that we are looping over the cursor results. And, as described earlier, you only have TextViews, so you can only store String values.
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
final int rows = cursor.getCount();
for (int i = 0; i < rows; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("fname", cursor.getString(cursor.getColumnIndexOrThrow("fname"));
map.put("mname", cursor.getString(cursor.getColumnIndexOrThrow("mname"));
map.put("lname", cursor.getString(cursor.getColumnIndexOrThrow("lname"));
fillMaps.add(map);
cursor.moveToNext();
}
Now that you have looped over all the rows of the data and are satisfied with that data, you just set the adapter.
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
R.layout.item_layout,
from, to);
listView.setAdapter(adapter);
I created a custom ListView and in java filled that ListView with a SimpleAdapter. Here's my code:
setContentView(R.layout.activity_my);
list = (ListView) findViewById(R.id.lvList);
itemList = new ArrayList<HashMap<String, String>>();
itemMap = new HashMap<String, String>();
for (int i = 0; i < songs.length; i++) {
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
SimpleAdapter adapter = new SimpleAdapter(this, itemList,
android.R.layout.simple_list_item_2, new String[] {"song",
"artist"}, new int[] { android.R.id.text1,
android.R.id.text2 });
list.setAdapter(adapter);
Now I created two String Arrays Song and Artist respectively and put them in a HashMap and then put the HashMap in a ArrayList called itemList. After that i set up the adapter with the default list item id's in the parameter.
The problem I am facing is that when i run the application, the list view shows only the last item and subItem of the String Arrays repeatedly. Here's the image for reference.
My ListView
I'd like to know why this is happening. I tried putting the itemList.add(itemMap); outside the for() loop but it still didn't work. Help would really be appreciated with a proper explanation, cause i'm kinda new to this. Thank You!
You can achieve this by recreating your itemMap object at every iteration. Try the following way.
itemMap = new HashMap<String, String>();
for (int i = 0; i < songs.length; i++) {
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
Change this to
for (int i = 0; i < songs.length; i++) {
itemMap = new HashMap<String, String>();
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
Explaination:
Refer here. Note HashMap is a key value pair which means it will store some value for a key. So HashMap will not allow duplicate key. From your code already youe have added the value for song and artist. So you can not add the value for these keys again. But the itemlist add the itemMap at every iteration. That is why the list repeating the same record. To avoid this problem simply re instantiate the itemMap object.
I hope this will help you.
bro, you won't believe but you are doing wrong.
make a new object of HashMap on each iteration.
I have a listview with multicolumns, where I have several textviews in each row. Including two textview in each row with orientation="vertical".
In the xml file I can set the tag of each textview. However this tag of each textview is equal in each row.
How I can set the tag of each textview? The same problem happens with the id. In the first row it's ok. The problem is in following rows.
I put an image with an example.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container,false);
lv = (ListView)rootView.findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();
String[] columnTags = new String[] {"hour","col1", "col2", "col3","col4", "col5", "col6","col7", "col8", "col9","col10", "col11", "col12","col13","col14"};
int[] columnIds = new int[] {R.id.tv_list_item1,R.id.tv_listRow_item1, R.id.tv_listRow_item2, R.id.tv_listRow_item3,R.id.tv_listRow_item4, R.id.tv_listRow_item5, R.id.tv_listRow_item6,R.id.tv_listRow_item7, R.id.tv_listRow_item8, R.id.tv_listRow_item9,R.id.tv_listRow_item10, R.id.tv_listRow_item11, R.id.tv_listRow_item12,R.id.tv_listRow_item13, R.id.tv_listRow_item14};
for(int i=0; i<24; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
for(int j=0; j<15; j++)
{
if(j==0){
map.put("hour", "0"+i+":00");
if(i<10){
map.put("hour", "0"+i+":00");
}
else
map.put("hour", i+":00");
}
else if(j>0){
map.put(columnTags[j], "row”+i+”col"+j);
}
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter =
new SimpleAdapter(getActivity(), mylistData, R.layout.sechedule_list_row,
columnTags , columnIds);
lv.setAdapter(arrayAdapter);
You have to create your own custom adapter and then in getView set your tags and ids for each text view.
Here is an example how to use getView
android - custom ListView, getView method
This one example help you lot i think http://www.technotalkative.com/android-multi-column-listview/
I'm creating a game and here's the code I'm using to show a list of games with the user names, score, date etc. But how do I get the values of the TextViews tv_playerScore and tv_opponentScore so I can compare them and change the textColors of them? Because what I want is to parseInt and see which has the highest value and set its textcolor to green, and the others textcolor to red.
private void showGames(JSONArray games) throws JSONException {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < games.length(); i++) {
map.put("challenger", games.getJSONObject(i).getString("challengerName"));
map.put("active", games.getJSONObject(i).getString("active"));
map.put("opponent", games.getJSONObject(i).getString("opponentName"));
map.put("date", games.getJSONObject(i).getString("date"));
map.put("gameID", games.getJSONObject(i).getString("gameID"));
map.put("amount", games.getJSONObject(i).getString("amount"));
map.put("playerScore", games.getJSONObject(i).getString("challengerScore"));
map.put("opponentScore", games.getJSONObject(i).getString("opponentScore"));
if (Integer.parseInt(games.getJSONObject(i).getString("active")) == 2) {
mylist.add(map);
}
map = new HashMap<String, String>();
}
SimpleAdapter sadapter = new SimpleAdapter(this, mylist, R.layout.list, new String[]
{"amount", "active", "gameID", "challenger", "opponent", "date", "playerScore", "opponentScore"},
new int[] {R.id.tv_amount, R.id.tv_activte, R.id.tv_gameID, R.id.tv_player, R.id.tv_opponent, R.id.tv_date, R.id.tv_playerScore, R.id.tv_opponentScore});
listView.setAdapter(sadapter);
}
If you want to get the values of a textview must use findViewById function from Activity.
TextView tv_playerScore = (TextView) findViewById (R.id.tv_playerScore);
If the showGames() method is not a class that inherits from Activity (or similiar), you should make a setter injection of the elements of sight to those who want to access.
To compare:
tv_playerScore.getText().toString().compareTo(tv_opponentScore.getText().toString());
Finally, to change the color:
tv_playerScore.setTextColor(Color.CYAN);
Regards.
I think you should have a closer look in how ListViews work ( http://developer.android.com/resources/tutorials/views/hello-listview.html )
In short I guess you'll have to write your own Adpater class (e.g. extend SimpleAdapater) and write over its getView method. Here you can set the color of the textviews depending on the according value. (I think it would make sense to have them sorted before instead of checking them every time a list element is drawn...
I want to set imagese in listview but I don't do it properly. What's wrong with images I dont know .plz help me my code is here.
final ArrayList<HashMap<String,String>> dataListfeture = new ArrayList<HashMap<String,String>>();
ArrayList<HashMap<String,String>> dataList = new ArrayList<HashMap<String,String>>();
for(int j=0; j<nodesfeture.getLength(); j++)
{
Element e1 =(Element)nodesfeture.item(j);
Node node = nodesfeture.item(j);
HashMap<String, String> mapfeautre = new HashMap<String, String>();
mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
String rename = mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
rename.replaceAll("", "%20");
mapnamelist= mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
mapdistancelist =mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
mapvenuelist=mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
mapfeautre.put("imageupload",node.getAttributes().getNamedItem("imageupload").getNodeValue());
String strimageurl=mapfeautre.put("imageupload",node.getAttributes().getNamedItem("imageupload").getNodeValue());
mapimagelist="http://www.afterdarknightlife.com/"+strimageurl;
System.out.println("Image URL "+j+" :"+mapimagelist);
mylistfeature.add(mapfeautre);
}
System.out.println("data list = "+dataListfeture);
ListAdapter adapterfeaturetype = new SimpleAdapter(MainScreen.this, mylistfeature, R.layout.demophoto, new String[]
{
"imageupload","name", "distance","venuetype
},
new int[]
{
R.id.item_featureimage,
R.id.item_featurename,
R.id.item_featuredistance,
R.id.item_featurevenuetype,
});
may be this will help you...
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
Please give GreedDroid a try. It has very advanced set of list view items (including an item with an image) which you can use from code and from xml as well.