When I use following code in an Activity class, it works fine but when moved into a separate class, the Table is drawn, but no content is shown.
String text[] = getResources().getStringArray(R.array.main_table);
I checked all variables (text, fillMaps, from to), and the correct data is available. Also if I use the standard SimpleAdapter, the result is the same.
If anybody has an idea, I would be grateful.
public class MainStatisticGridView extends GridView {
public MainStatisticGridView(Context context) {
super(context);
GridView statistic = (GridView) ((Activity)context).findViewById(R.id.statistic_grid);
String[] from = new String[]{"text"};
int[] to = new int[]{R.id.statistic_cell_text};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String text[] = getResources().getStringArray(R.array.main_table);
for (int i = 0; i < text.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("text", text[i]);
fillMaps.add(map);
}
SimpleAdapter adapter = new MyCursorAdapter(context, fillMaps, R.layout.calendar_title_cell, from, to);
statistic.setAdapter(adapter);
}
private class MyCursorAdapter extends SimpleAdapter {
//private variables
Context context;
LayoutInflater inflater;
public MyCursorAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.statistic_cell, parent, false);
}
Set<Integer> VALUES = new HashSet<Integer>(Arrays.asList(
new Integer[]{0, 1, 2, 3, 4, 5, 10, 15, 20}
));
if (VALUES.contains(Integer.valueOf(position))) {
convertView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
TextView tv = (TextView) convertView.findViewById(R.id.statistic_cell_text);
tv.setTextColor(getResources().getColor(R.color.statistic_title));
convertView.setPadding(0, 0, 0, 0);
} else {
convertView.setBackground(getResources().getDrawable(R.drawable.border_intern));
}
return convertView;
}
}
}
Related
i have created a custom array adapter for my list view but for some reason the data is not getting displayed in listview item. even the log cat doesnt show anything when i put log statement in getView method of the custom adapter which extends ArrayAdapter.
This is the activity that holds listview
public class booktable extends AppCompatActivity {
ListView mylv;
int[] array = new int[5];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booktable);
mylv = findViewById(R.id.mylv);
array = new int[]{1, 0, 0, 0, 1};
ListAdapter la = new customadapter(getApplicationContext(),R.layout.activity_booktable,array);
mylv.setAdapter(la);
}
}
this is my custom adapter
public class customadapter extends ArrayAdapter {
LayoutInflater li;
int[] table = new int[5];
public customadapter(#NonNull Context context,int resource, int [] array) {
super(context,resource);
li = LayoutInflater.from(context);
table = array;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View v = li.inflate(R.layout.custom_row,parent);
ImageView img = v.findViewById(R.id.img);
TextView tv = v.findViewById(R.id.tv);
if(table[position]==0)
{
tv.setText("FULL");
tv.setTextColor(Color.RED);
}
if(table[position]==1)
{
tv.setText("AVAILABLE");
tv.setTextColor(Color.GREEN);
}
return v;
}
}
try to pass the parameter array or collection to ArrayAdapter constructor, Looks like this:
public customadapter(#NonNull Context context,int resource, int [] array) {
super(context, resource, array);
li = LayoutInflater.from(context);
table = array;
}
I am using Tab Layout. I am showing data in ListView inside ListFragment. Data is showing in ListView but problem is when we press tabb again, same data will add in Listview. So every time continue same data add in listView.
Code here-
public class HomeFragments extends ListFragment {
int[] ust_icon = {R.drawable.title,R.drawable.t2,R.drawable.t2};
String[] live_on = {"NA","NA","NA"};
String[] total_subtopics = {"3","3","3"};
String[] title = {"semiconductor devices ##","Equations of state ##","pn junction ##"};
String[] pos = {"1","5","6"};
String[] completed_subtopics = {"3","3","3"};
String[] live = {"false","false","false"};
String[] id = {"13","14","15"};
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
public HomeFragments() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_fragments, container, false);
HashMap<String, String> map = new HashMap<String, String>();
for (int i=0;i<3;i++)
{
map = new HashMap<String, String>();
map.put("ust_icon", Integer.toString(ust_icon[i]));
map.put("live_on",live_on[i]);
map.put("total_subtopics",total_subtopics[i]);
map.put("title",title[i]);
map.put("pos",pos[i]);
map.put("completed_subtopics",completed_subtopics[i]);
map.put("live",live[i]);
map.put("id",id[i]);
data.add(map);
}
String[] from = {"ust_icon","live_on","total_subtopics","title","pos","completed_subtopics","live","id"};
int[] to = {R.id.img1,R.id.txt1,R.id.txt2,R.id.txt3,R.id.txt4,R.id.txt5,R.id.txt6,R.id.txt7};
adapter = new SimpleAdapter(getActivity(),data, R.layout.list1, from, to);
setListAdapter(adapter);
return view;
}
#Override
public void onStart() {
super.onStart();
}
}
how to resolve this issue Please help....
I think I have your answer. Just wait for me a couple minute when I got home and then I’ll answer you ok?
Edit:
Try to construct your data and set your adapter in onActivityCreated()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_fragments, container, false);
return view;
}
and
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
HashMap<String, String> map = new HashMap<String, String>();
for (int i=0;i<3;i++)
{
map = new HashMap<String, String>();
map.put("ust_icon", Integer.toString(ust_icon[i]));
map.put("live_on",live_on[i]);
map.put("total_subtopics",total_subtopics[i]);
map.put("title",title[i]);
map.put("pos",pos[i]);
map.put("completed_subtopics",completed_subtopics[i]);
map.put("live",live[i]);
map.put("id",id[i]);
data.add(map);
}
String[] from = {"ust_icon","live_on","total_subtopics","title","pos","completed_subtopics","live","id"};
int[] to = {R.id.img1,R.id.txt1,R.id.txt2,R.id.txt3,R.id.txt4,R.id.txt5,R.id.txt6,R.id.txt7};
adapter = new SimpleAdapter(getActivity(),data, R.layout.list1, from, to);
setListAdapter(adapter);
}
if the code above not working, try to replace this:
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
by this: ArrayList<HashMap<String,String>> data;
and then in your onCreateView : data = new ArrayList<HashMap<String, String>>();
Try adding condition to your code like i did below.i just created boolean variable called check.if tab opens first time it will set listview as well as set it's value to true.now condition wont run and you will not get listview element add again and again
public class HomeFragments extends ListFragment {
int[] ust_icon = {R.drawable.title,R.drawable.t2,R.drawable.t2};
String[] live_on = {"NA","NA","NA"};
String[] total_subtopics = {"3","3","3"};
String[] title = {"semiconductor devices ##","Equations of state ##","pnjunction ##"};
String[] pos = {"1","5","6"};
String[] completed_subtopics = {"3","3","3"};
String[] live = {"false","false","false"};
String[] id = {"13","14","15"};
public static final boolean check = false;
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
public HomeFragments() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_fragments, container, false);
HashMap<String, String> map = new HashMap<String, String>();
for (int i=0;i<3;i++)
{
map = new HashMap<String, String>();
map.put("ust_icon", Integer.toString(ust_icon[i]));
map.put("live_on",live_on[i]);
map.put("total_subtopics",total_subtopics[i]);
map.put("title",title[i]);
map.put("pos",pos[i]);
map.put("completed_subtopics",completed_subtopics[i]);
map.put("live",live[i]);
map.put("id",id[i]);
data.add(map);
}
if(check == false)
{
String[] from = {"ust_icon","live_on","total_subtopics","title","pos","completed_subtopics","live","id"};
int[] to = {R.id.img1,R.id.txt1,R.id.txt2,R.id.txt3,R.id.txt4,R.id.txt5,R.id.txt6,R.id.txt7};
adapter = new SimpleAdapter(getActivity(),data, R.layout.list1, from, to);
setListAdapter(adapter);
check = true;
}
return view;
}
#Override
public void onStart() {
super.onStart();
}
Update :
the Grid is now displayed correctly, but the content is missing
I checked
String text[] = getResources().getStringArray(R.array.main_table);
and text is filled with the Strings and if I use the code in the activity file the data is shown.
Any ideas
Original Question
I am having problems using findViewById() in non-activity classes.
If I am on the top Level, it is no problem.
((Activity)context).findViewById(R.id.statistic_grid);
But when I am trying to reference child views, I am getting "null"
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view.findViewById(R.id.statistic_cell_text);
Can please anybody help me out.
Thanks in advance
Lars
public class MainStatisticGridView extends GridView {
public MainStatisticGridView(Context context) {
super(context);
GridView statistic = (GridView) ((Activity)context).findViewById(R.id.statistic_grid);
String[] from = new String[]{"text"};
int[] to = new int[]{R.id.statistic_cell_text};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String text[] = getResources().getStringArray(R.array.main_table);
for (int i = 0; i < text.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("text", text[i]);
fillMaps.add(map);
}
SimpleAdapter adapter = new MyCursorAdapter(((Activity)context), fillMaps, R.layout.calendar_title_cell, from, to);
statistic.setAdapter(adapter);
}
private class MyCursorAdapter extends SimpleAdapter {
public MyCursorAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Set<Integer> VALUES = new HashSet<Integer>(Arrays.asList(
new Integer[]{0, 1, 2, 3, 4, 5, 10, 15, 20}
));
View view = super.getView(position, convertView, parent);
if (VALUES.contains(Integer.valueOf(position))) {
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
TextView tv = (TextView) view.findViewById(R.id.statistic_cell_text);
tv.setTextColor(getResources().getColor(R.color.statistic_title));
view.setPadding(0, 0, 0, 0);
} else {
view.setBackground(getResources().getDrawable(R.drawable.border_intern));
}
return view;
}
}
}
You need to inflate the layout, like this. Once you inflate the layout you can get the child items.
private class MyCursorAdapter extends SimpleAdapter {
//private variables
Context context;
LayoutInflater inflater;
public MyCursorAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.context = context;
//get layoutinflater
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Set<Integer> VALUES = new HashSet<Integer>(Arrays.asList(
new Integer[]{0, 1, 2, 3, 4, 5, 10, 15, 20}
));
//inflate your layout
if(convertView==null)
{
convertView = inflater.inflater(layoutfile, parent,false);
}
if (VALUES.contains(Integer.valueOf(position))) {
convertView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
TextView tv = (TextView) convertView.findViewById(R.id.statistic_cell_text);
tv.setTextColor(getResources().getColor(R.color.statistic_title));
convertView.setPadding(0, 0, 0, 0);
} else {
convertView.setBackground(getResources().getDrawable(R.drawable.border_intern));
}
return convertView;
}
}
Please note I did not test this.
This is my adapter class:
public class MyAdapter extends ArrayAdapter {
private Context context;
private LayoutInflater inflater;
private List<HashMap<String, String>> aList=new ArrayList<HashMap<String,String>>();
int[] to;
public MyAdapter(Context context, List<HashMap<String, String>> aList) {
super(context, R.layout.layoutarray, aList);
this.context = context;
this.aList = aList;
inflater = LayoutInflater.from(context);
}
HashMap<String, String> m;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.layoutarray, parent, false);
}
TextView title1=(TextView)convertView.findViewById(R.id.posttitle);
TextView description1=(TextView)convertView.findViewById(R.id.postdesc);
//TextView image1=(ImageView)rootView.findViewById(R.id.postimage);
TextView date=(TextView)convertView.findViewById(R.id.postdate);
ImageView image1=(ImageView)convertView.findViewById(R.id.postimage);
title1.setText(aList.get(aList.get(0).get("Title")));
// Picasso.with(context).load(m.get("Image")).into(image1);
return convertView;
}
}
This is my Activity:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
final View rootView = inflater.inflate(R.layout.aa, container, false);
Firebase.setAndroidContext(getActivity());
Firebase ref = new Firebase(FIREBASE_URL);
final List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
final ListView listView = (ListView) rootView.findViewById(R.id.listView);
final MyAdapter adapter = new MyAdapter(getActivity(),aList);
listView.setAdapter(adapter);
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
for (DataSnapshot single : child.getChildren()) {
Map<String, Object> map = (Map<String, Object>) single.getValue();
namerc = (String) map.get("Namerc");
image = (String) map.get("Imagerc");
description = (String) map.get("Description");
title=(String) map.get("Title");
if (namerc!=null && description!=null && title!=null) {
String[] title1={title};
String[] desc1={description};
String[] name1={namerc};
String[] image1={image};
Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();
HashMap<String,String>data=new HashMap<String, String>();
for (int i=0; i<title1.length;i++) {
data.put("Title", title1[i]);
data.put("Desc",desc1[i]);
data.put("Name",name1[i]);
data.put("Image",image1[i]);
}
aList.add(0,data);
adapter.notifyDataSetChanged();
}
}
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return rootView;
}
}
I want to set the values from my List to text view (which is a listiview).
but when i set it it is showing only first data in the listview. i am not understanding what is going wrong ,how to fetch all the data from List and set it to listview.
please help me to solve this..
I guess you are getting the same value each time. Get value for the right position in your adapter. Something like this:
title1.setText(aList.get(position).get("Title"));
How do I paint every value that under 100 in red in my ListView?
i have ListView that connect to my_list.xml
i connect the cursor like this:
public void update_list(String NN) {
c = db.rawQuery(NN, null);
startManagingCursor(c);
String[] from = new String[]{"_id","Store","Makat","Des","Qty" };
int[] to = new int[]{R.id.txtID, R.id.txtStore,R.id.txtMakat ,R.id.txtDes ,R.id.txtQty};
SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.my_list, c, from, to);
setListAdapter(notes);
}
How to do it ?
My suggestion would be to make your own adapter which would extend SimpleCursorAdapter. In it, you should override the getView method which creates every row view.
class MyCustomAdapter extends SimpleCursorAdapter{
private Context context;
private Cursor c;
public MyCustomAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
}
#Override
public View getView(int pos, View inView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
this.c.moveToPosition(pos);
//get the value from cursor, evaluate it, and set the color accordingly
int columnIndexOfQty = 4;
int qty = c.getInt(columnIndexOfQty);
if(qty < 100){
rowView.setBackgroundColor(Color.RED);
}
return rowView;
}
}
There may be some errors, but I think that you must get the idea.