It might be silly question, but I am having bit hard time to make it work.
I have a custom list view adapter which some textview and imageview.
But when I am updating listview using custom adapter it is only updating last value. here is my code
Adapter code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
for(int i = 0; i < values.size(); i++){
Log.d(TAG, "DATA"+values.get(i).type);
type.setText(values.get(i).type);
}
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
Code to populate adapter
info_panel = new InfoListAdapter(esActivity.this, allData);
list.setAdapter(info_panel);
But I do not know why, it is only showing last values from values list. In addition I want to change image based on condition and data inside values, any good way to do this.
use following code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
type.setText(values.get(position).type);
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
First of all, you have to override getCount() method of your adapter like this
#Override
public int getCount() {
return values .size();
}
and then in your getView() method you have to do something like this
#Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.mycartslistcontents,
null);
holder.yourImageView= (ImageView) convertView
.findViewById(R.id.image);
holder.yourTextView= (TextView) convertView
.findViewById(R.id.textview);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// here set your textView text and image resource
return convertView;
}
Don't forget to take a global variable layoutInflater at the top like this
private LayoutInflater layoutInflater;
and initialise it in your constructor this way
layoutInflater = LayoutInflater.from(yourContext);
and here is your ViewHolder class
static class ViewHolder {
ImageView yourImageView;
TextView yourTextView;
}
Related
I'm trying to fetch a image path from a mysql database using php and displaying the image in a gridview.
My problem is that am getting a NPE (NullPointerException) in the adapter when setting the images.
Here my GridViewAdapter:
public class GridViewAdapter extends BaseAdapter {
private Context context;
public ArrayList<HashMap<String,String>> mThumbIds = new ArrayList<HashMap<String, String>>();
AQuery aQuery;
private LayoutInflater mInflater;
public GridViewAdapter(Context context,ArrayList<HashMap<String,String>> data) {
this.context= context;
this.mThumbIds = data;
this.aQuery = new AQuery(context);
}
#Override
public int getCount() {
return mThumbIds.size();
}
#Override
public Object getItem(int position) {
return 0;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photo_gallery_list_item, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
aQuery.id(holder.imageView).image(mThumbIds.get(position).get("flag"), true, true);
return convertView;
}
class ViewHolder {
ImageView imageView;
}
}
The Line that fire the NPE is the following:
convertView = mInflater.inflate(R.layout.photo_gallery_list_item, null);
Can anyone suggest where am wrong?
Please initialize your mInflater like below.
mInflater = LayoutInflater.from(context);
mInflater is null. You never assigned it.
Either pass one in or get it similar to this in your GridViewAdapter
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Only cause you didn`t init your mInflater;
Add this line in your constructor;
mInflater = LayoutInflater.from(context);
My layout named main_layout has a RelativeLayout with at least two elements like Textview and Imageview inside it.
public class SigninFragment extends Fragment {
private List<Test> list= null;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Test tes= new Test ();
tes.setId(1);
tes.setDesc("descabc");
list= new ArrayList<>();
list.add(prof);
tesListAdapter =
new TesListAdapter(
rootView.getContext()
,R.layout.list_row_adapter
,list);
autocompletetextview.setThreshold(3);
autocompletetextview.setAdapter(tesListAdapter );
My Adapter Class :
public class ProfissoesListAdapter extends ArrayAdapter<Test> {
private LayoutInflater inflater;
private int resource;
private Context context;
public TesListAdapter(Context activity, int resource, List<Test> listaProf) {
super(activity, resource, listaProf);
this.resource = resource;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.main_layout, parent, false);
holder = new ViewHolder();
holder.idProfissao = (TextView) convertView.findViewById(R.id.textViewId);
//holder.descProfissao = (TextView) convertView.findViewById(R.id.textDescProf);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Test item = getItem(position);
holder.idProfissao.setText(item.getId_prof()); **<=== BUG HERE**
return convertView;
}
Debugging adapter code I got in the line holder.idProfissao.setText(item.getId_prof()); Resource id cannot be found....
The method setText() of the TextView class it's overloaded to either use a String or an integer representing an id in the form of R.string.some_text. If item.getId_prof() doesn't return an id then you need to make it a String before setting it as text:
holder.idProfissao.setText("" + item.getId_prof());
I have made a customize list in which i have to give different colors to different list items.I tried doing it but the text color is always black.I don't know why this is happening;
Code-
private Context context;
private List<CommonModel> commonList = Collections.emptyList();
private LayoutInflater inflater;
private String from;
private int[] languageColours = {R.color.green, R.color.pink, R.color.grey, R.color.chetna_red};
public CommonAdapter(Context context, List<CommonModel> commonList, String from) {
this.context = context;
this.commonList = commonList;
this.from = from;
inflater = inflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.common_custom_list_item, parent, false);
holder.tvContent = (TextView) convertView.findViewById(R.id.tv_common_content);
holder.ivImage = (ImageView) convertView.findViewById(R.id.iv_common_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ivImage.setImageResource(commonList.get(position).getImage());
holder.tvContent.setText(commonList.get(position).getContent());
holder.tvContent.setTextColor(languageColours[position]);
return convertView;
}
private class ViewHolder {
TextView tvContent;
ImageView ivImage;
}
I had checked that the position which i am getting is correct then to the color is not changing.
instead of using languageColours[position] use context.getResource.getColor(languageColours[position]);
Simply use this
holder.tvContent.setTextColor(R.color.Red);
So I populate a ListView from the local database using Custom Adapter. What I want to do now is when I click a button the lists in the listview will change its order like what shown in the image. Can someone help me. Here's my code.
This is the current ListView --> SEE IMAGE
This is what I want to do --> SEE IMAGE
CustomAdapter.java
public CustomAdaper(Context context,ArrayList<SampleObj> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
final SampleObj i = items.get(position);
if (i != null) {
final SampleObj ei = (SampleObj)i;
if (v == null) {
v = vi.inflate(R.layout.log_row, null);
holder = new ViewHolder();
holder.date = (TextView)v.findViewById(R.id.date);
holder.time = (TextView)v.findViewById(R.id.time);
holder.buttonID = (TextView)v.findViewById(R.id.buttonID);
v.setTag(holder);
}
else {
holder = (ViewHolder)v.getTag();
}
holder.date.setText(ei.date);
holder.time.setText(ei.time);
holder.buttonID.setText(ei.buttonID);
}
return v;
}
private static class ViewHolder {
public TextView date;
public TextView time;
public TextView buttonID;
}
}
you need to use Expandale List
First see this
then this
then this
I would like to create ListView using CustomAdapter that extends BaseAdapter. I have CustomAdapter(Context context, List<Map<String, String>> dataList).
How do I get data from Map and set my views in the getView() method? Before I've tried to use SimpleAdapter but I need to set bitmap stored on SD, and path to image I've stored in map. Every help is appreciated.
Here's my source of CustomAdapter:
public class CustomAdapter extends BaseAdapter {
private Context context;
private String pricePrecision = null;
private List<Map<String, String>> data;
public CustomAdapter(Context context, List<Map<String, String>> data) {
super();
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return (data == null) ? 0 : data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
return null;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}
}
Change your getView method as :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
viewHolder.itemThumbnail.setImageBitmap(data.get(position).get("thumbnail"));
viewHolder.itemName.setText(data.get(position).get("name"));
viewHolder.itemPrice.setText(""+data.get(position).get("price"));
return viewHolder;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}
A little addition to (ρяσѕρєя K)'s code.
You might want to set listeners on your thumbnails.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
viewHolder.itemThumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
viewHolder.itemThumbnail.setImageBitmap(data.get(position).get(
"thumbnail"));
viewHolder.itemName.setText(data.get(position).get("name"));
viewHolder.itemPrice.setText("" + data.get(position).get("price"));
return viewHolder;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}