I have three arrays and I want to populate list view with them, till now I managed to populate list view with one array and array adapter, but now I need to put all three arrays in every item of list view.
Simple code would help lots.
Thank you in advance.
Join the three array into a single one and Populate it as before
Refer this code to create Custom Adapter
public class ListViewAdapter extends BaseAdapter {
private ArrayList<TimeRecord> record = new ArrayList<TimeRecord>();
TextView txtTime, txtNote;
public ListViewAdapter() {
record.add(new TimeRecord("38:23", "Feeling good!"));
record.add(new TimeRecord("49:01", "Tired. Needed more caffeine"));
record.add(new TimeRecord("26:21", "I’m rocking it!"));
record.add(new TimeRecord("29:42",
"Lost some time on the hills, but pretty good."));
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return record.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return getItem(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position , View convertView , ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.row_xml, parent, false);
}
TimeRecord time = record.get(position);
txtTime = (TextView) convertView.findViewById(R.id.txtTime);
txtTime.setText(time.getTime());
txtNote = (TextView) convertView.findViewById(R.id.txtNote);
txtNote.setText(time.getNote());
return convertView;
}
}
Related
Hi I working with my android, I have two spinners and using retrofit to populate my spinners. I manage to populate first spinner. But I don't know how to get my second spinner populated under id selected from first spinner. I'm using codeigniter rest controller to pass the id and want to retrieve it using retrofit for second spinner, but I don't know how to do this? Please help me.
I already created Rest Controller by passing the id. Already created ApiInterface. And beans to get details data. First spinner working good.
Declaration
classSpinner = (Spinner) findViewById(R.id.classSpinner);
divSpinner = (Spinner) findViewById(R.id.divSpinner);
classSpinner.setAdapter(mAdapter);
mApiInterface = ApiClient.getClient().create(ApiInterface.class);
getAllProvinsi();
ApiInterface
public interface ApiInterface {
//first spinner
#GET("Testpage/provinsi")
Call<GetProvinsi> getProvinsi();
//second spinner that I needed how to pass the id? and get data by id selected from first spinner
#GET("Testpage/kotabyprovid/provinsi_id/")
Call<GetKota> getKota();
}
GetProvinsi ( First Spinner )
public class GetProvinsi {
#SerializedName("result")
List<Provinsi> listDataProvinsi;
public List<Provinsi> getListDataProvinsi() {
return listDataProvinsi;
}
public void setListDataProvinsi(List<Provinsi> listDataProvinsi) {
this.listDataProvinsi = listDataProvinsi;
}
}
GetKota ( Second Spinner )
public class GetKota {
#SerializedName("result")
List<Kota> listDataKota;
public List<Kota> getListDataKota() {
return listDataKota;
}
public void setListDataKota(List<Kota> listDataKota) {
this.listDataKota = listDataKota;
}
}
First Spinner to get all data. I'm trying to select the id to populate second spinner from the adapter.
private void getAllProvinsi() {
Call<GetProvinsi> kontakCall = mApiInterface.getProvinsi();
kontakCall.enqueue(new Callback<GetProvinsi>() {
#Override
public void onResponse(Call<GetProvinsi> call, Response<GetProvinsi>
response) {
List<Provinsi> ProvinsiList = response.body().getListDataProvinsi();
Log.d("Retrofit Get", "Jumlah data Kontak: " +
String.valueOf(ProvinsiList.size()));
//mAdapter = new KontakAdapter(KontakList, getBaseContext());
mAdapter = new ProvinsiAdapter(ProvinsiList, getBaseContext(), new ProvinsiAdapter.DetailsAdapterListener() {
#Override
public void classOnClick(View v, int position) {
//showClass(mTrainDetails, position);// do something or navigate to detailed classes
}
#Override
public void daysOnClick(View v, int position) {
//showDays(movieList, position);// do something or navigate to running days
}
});
classSpinner.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<GetProvinsi> call, Throwable t) {
Log.e("Retrofit Get", t.toString());
}
});
First Spinner adapter implement with OnItemSelectedListner that I need help with the method what should be?
public class ProvinsiAdapter extends BaseAdapter implements AdapterView.OnItemSelectedListener {
private LayoutInflater inflater;
List<Provinsi> mProvinsiList;
public ProvinsiAdapter.DetailsAdapterListener onClickListener;
public ProvinsiAdapter(List<Provinsi> provinsiList, Context context, ProvinsiAdapter.DetailsAdapterListener listener) {
inflater = LayoutInflater.from(context);
this.mProvinsiList = provinsiList;
this.onClickListener = listener;
}
public int getCount() {
// TODO Auto-generated method stub
return mProvinsiList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.lsv_provinsi_list, null);
holder = new ViewHolder();
holder.txtMenuName = (TextView) convertView.findViewById(R.id.tv_namaProv);
holder.txtQuantity = (TextView) convertView.findViewById(R.id.tv_idProv);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtMenuName.setText(mProvinsiList.get(position).getNamaProvinsi());
holder.txtQuantity.setText(String.valueOf(mProvinsiList.get(position).getId()));
return convertView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Please help what should I put in here to populate second spinner
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
static class ViewHolder {
TextView txtMenuName, txtQuantity, txtPrice;
}
//region Interface Details listener
public interface DetailsAdapterListener {
void classOnClick(View v, int position);
void daysOnClick(View v, int position);
}
}
JSON Result with id Testpage/kotabyprovid/provinsi_id/1 for second spinner
{"result":[{"id_kota":"17","provinsi_id":"1","nama_kota":"Badung"},{"id_kota":"32","provinsi_id":"1","nama_kota":"Bangli"},{"id_kota":"94","provinsi_id":"1","nama_kota":"Buleleng"},{"id_kota":"114","provinsi_id":"1","nama_kota":"Denpasar"},{"id_kota":"128","provinsi_id":"1","nama_kota":"Gianyar"},{"id_kota":"161","provinsi_id":"1","nama_kota":"Jembrana"},{"id_kota":"170","provinsi_id":"1","nama_kota":"Karangasem"},{"id_kota":"197","provinsi_id":"1","nama_kota":"Klungkung"},{"id_kota":"447","provinsi_id":"1","nama_kota":"Tabanan"}],"0":200}
Please help and Thank You
Replace
#GET("Testpage/kotabyprovid/provinsi_id/")
Call<GetKota> getKota();
with
#GET("Testpage/kotabyprovid/provinsi_id/{prov_id}")
Call<GetKota> getKota(#Path("prov_id") int provId);
Now you can pass id to the method, the retrofit will take care of passing provId to the URL path.
Hi guys I need your help. I want to set listner imageview and button in gridview and when I click to imageview app should show dialog with image and when I click to button app should do something.
like this:
enter image description here
You need to create Custom Adapter for your GridView with Custom UI with ImageView and Button, and then inside the getVieew(){} you need to make click listener for Image and button, and there you can do your action with the help of position with your array list.
here I am posting demo adapter for your reference:
here you can change TextView to Button.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
ref:-https://caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html
I have a listview and I am populating that list view from a ArrayList of custom objects.
This is the custom adapter code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO=holder.file;
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
FilesPOJO file;
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
There are two buttons one for file delete and other for file download. I have implemented on click listeners for these two buttons. The problem is when I click any button the fileName in the Toast message is different from the file name that I see on the screen display. For ex: I have 5 files with names
delete.png
upload.png
share.png
referesh.png
copy.png
and now when I click delete button corresponding to referesh.png file the toast pop ups and in that the file name is different than referesh.png.
how to resolve this issue.
EDITS:
This is how I set up this adapter
FilesAdapter adapter=new FilesAdapter(rootView.getContext(), R.layout.file_list_view_item_row, Application.files);
View header = (View) View.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
fileListHeading.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
filesListView.addHeaderView(header);
filesListView.setAdapter(adapter);
I think the problem is with your fPOJO variable when onClick is called it may have different value because after every getView you're changing it's value.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
final FileHolder holder;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+holder.fileName, Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
Just make holder final and get you're file name from it.
You store your list item in holder, this is your problem, ListView adapter creates as much convertViews as needed to fill the screen so if you have list of 10+ elements, only few will be added to row.
First of all don't store your list element in holder, you gave adapter method called getItem(positoin) to get it.
In your holder view store only references to layout views (TextView, Buttons etc) and (if(row == null) call findViewById on them and setTag.
Then set text, listeners outside the if statement, like so:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
row.setTag(holder);
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO= getItem(position);
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(null);
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(null);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
Also you can do some code optimalization, for example don't initialize LayoutInflater in getView() method, you can do it in adapter Constructor
i have an ArrayList and one BaseAdapter to inflate my layout, when one array from arraylist is empty, the value on layout show text "NaN%" it is so annoying, i want handle an empty array from my arraylist with setText as (0%). so how to handle empty value from ArrayList in baseadapter android? this is my code :
public class TabelNABDetail extends BaseAdapter {
Context context;
ArrayList<NabDetail> listData;//stack
int count;
public TabelNABDetail(Context context, ArrayList<NabDetail>listData){
this.context=context;
this.listData=listData;
this.count=listData.size();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
// return count;
return (listData==null) ? 0 : listData.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
View view= v;
ViewHolder holder= null;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.layout_tabel_detailnab, null);
holder=new ViewHolder();
holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1);
view.setTag(holder);
}
else{
holder=(ViewHolder)view.getTag();
}
if (position==0) {
holder.persen_hke1.setText("0%");
} else {
holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%");
}
return view;
}
static class ViewHolder{
TextView ,persen_hke1;
}
i want to handle if listData.get(position).getpersen_hke1() is empty value, it gonna be holder.persen_hke1.setText("0%") how to solve it? thanks before
Change your getView() like this
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
View view= v;
ViewHolder holder= null;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.layout_tabel_detailnab, null);
holder=new ViewHolder();
holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1);
view.setTag(holder);
} else {
holder=(ViewHolder)view.getTag();
}
if (position==0) {
holder.persen_hke1.setText("0%");
} else {
**/////// these lines are extra lines add into your code**
NabDetail nabDetail = listData.get(position)
if(!nabDetail.getpersen_hke1().equals(""))
holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%");
} else {
holder.persen_hke1.setText("0%");
}
}
return view;
}
I want to create an ExpandableListView with custom adapter, however it only show my title group and when I click on the title (to view the children items) it doesnt do anything
(I want to have only one group item, therefore the item group count is 1).
Please help me to find where am I wrong, thanks alot.
This is my adapter:
class chatListAdapter extends BaseExpandableListAdapter {
private String objectsIdInsideAdapter;
private List<String> commentsInsideAdapter = new ArrayList<String>();
private List<String> FacebookIdInsideAdapter=new ArrayList<String>();
private List<String> NamesInsideAdapter =new ArrayList<String>();
private Context mContext;
chatListAdapter(String objectId,Context ctx,List<String> comments,List<String> face,List<String> names){
objectsIdInsideAdapter=objectId;
mContext=ctx;
commentsInsideAdapter=comments;
FacebookIdInsideAdapter=face;
NamesInsideAdapter=names;
}
#Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return NamesInsideAdapter.get(arg1);
}
#Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View v,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
if(v==null){
v = inflater.inflate(R.layout.chat_item, parent, false);
}
String comment =commentsInsideAdapter.get(position);
Button sendCommentBtn =(Button) v.findViewById(R.id.chatitemSendComment);
TextView commentView= (TextView) v.findViewById(R.id.chateitemViewComment);
commentView.setText(comment);
TextView commentName =(TextView) v.findViewById(R.id.chatitemfreindName);
commentName.setText(NamesInsideAdapter.get(position));
EditText commentText= (EditText) v.findViewById(R.id.chatitemEnterComment);
commentText.setVisibility(View.GONE);
sendCommentBtn.setVisibility(View.GONE);
return v;
}
#Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return NamesInsideAdapter.size();
}
#Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.chat_item_group_layout,parent, false);
}
TextView groupName = (TextView) v.findViewById(R.id.groupName);
TextView groupDescr = (TextView) v.findViewById(R.id.groupDescr);
groupName.setText("view comments");
groupDescr.setText("groupDSCR");
return v;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
and this is the code in the main Activity:
ExpandableListView chat =(ExpandableListView) v.findViewById(R.id.ExpList);
String faceids =facebookIdChatInside.get(position);
String comments = commentInside.get(position);
String namesChat =nameChatInside.get(position);
List<String> chatItems = new ArrayList<String>(Arrays.asList(comments.split(",")));
List<String> facebookids = new ArrayList<String>(Arrays.asList(faceids.split(",")));
List<String> namesList = new ArrayList<String>(Arrays.asList(namesChat.split(",")));
chatItems.add(" ");
facebookids.add(id);
namesList.add(name);
chatListAdapter chatA =new chatListAdapter(objectsId.get(position),profileContext,chatItems,facebookids,namesList);
chat.setAdapter(chatA);
I checked it, and those 3 Lists are not empty.
Reading your code i found 3 points that aren't so clear to me:
getGroup you return null? Why don't u return the real Object
getChildId you should return an unique id identifying your child
hasStableIds. You return false. You dont need to override this method.
If you want to have more information on ExpandableListView give a look at my blog here
You are using the ExpandableListView like a simple ListView why dont u consider to you a ListView?