mycustom adapter which extends from baseadapter but when i run my main java file it shows main activity no
list view item
public class MyCustomadapter extends BaseAdapter{
private Context mycontext;
String[] mystringlist;
public MyCustomadapter(Context context,String[] strText) {
this.mycontext=context;
this.mystringlist = strText;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mystringlist.length;
}
#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 position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
LayoutInflater inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.each_row, parent,false);
TextView tv = (TextView) rowView.findViewById(R.id.rowtxt);
tv.setText(mystringlist[position]);
return rowView;
}
}
mycustom adapter which extends from baseadapter but when i run my main java file it shows main activity no
list view item
problem:
return 0;
You are return 0 means there would be no list items to be displayed in your ListView thus giving you 0 result.
You need to return the number of item in your array or the length of it.
sample:
#Override
public int getCount() {
// TODO Auto-generated method stub
return mystringlist.length;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return mystringlist.length();
}
My idea is extending ArrayAdapter, Have a look at the following codes
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, String[] items) {
super(context, 0, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// your codes for generating rows
String thisItem = getItem(position);
}
}
You're done, also you're no longer need to override other methods!
Just as an addendum your getItem() method is incorrect. It should read as follows:
#Override
public Object getItem(int arg0) {
return mystringlist[position];
}
Related
I have list items on one list layout among items i have there is a chronometer that i need to start on each row ,other items come from database, but when activity launches Chronometer stays on 00:00. Please help me , i did many research some tell me that in can use adapter but i am not familiar with it, i don't know if there is not another way to do it without using adapter .
MainActivity .java:
private ArrayList<HashMap<String, String>> userList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView)findViewById(R.id.listView);
inflatedView =
getLayoutInflater().inflate(R.layout.payement_timing_list_adapter_view, null);
chronometer = (Chronometer)
inflatedView.findViewById(R.id.chronometer);
if (userList.size() != 0) {
//Set the User Array list in ListView
adapter = new SimpleAdapter(getApplicationContext(), userList, R.layout.payement_timing_list_adapter_view,
new String[]{"Driver_fullname", "plate_no", "parking_name"},
new int[]{R.id.drivername, R.id.plateno, R.id.pname});
myList.setAdapter(adapter);
}
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Log.d("My POSITION",""+position);
((Chronometer)inflatedView.findViewById(R.id.chronometer)).myList.
(position).start();
chronometer.start();
}
});
You will need to use a custom adapter for your ListView, and declare the Chronometer in the getView method for each row.
A custom adapter could be onle like this:
public class CustomAdapter extends BaseAdapter{
ArrayList<HashMap<String, String>> users;
Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, ArrayList<HashMap<String, String>> usersList) {
// TODO Auto-generated constructor stub
users = usersList;
context=mainActivity;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.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;
...
Chronometer cr;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
//Create a custom layout for your row view with the chronometer on it and get it here
rowView = inflater.inflate(R.layout.custom_row_item, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
...
holder.cr=(Chronometer) rowView.findViewById(R.id.chronometer);
cr.start();
holder.tv.setText("GET THE TEXT YOU NEED FROM USERS LIST HERE");
return rowView;
}
}
Then declare and use your custom adapter like:
CustomAdapter myAdapter = new CustomAdapter(YourActivity.this, userList);
myList.setAdapter(myAdapter);
Hope it helps :)
I have use this class extends base adapter display values. Below code working eclipse correctly. but me newly created project using Androi Studio there this code getview function not call or working.
i also check getcount . getcount values available.
public class CustomAdapterChatActivity extends BaseAdapter {
ArrayList<ChatUsersDetailsBean> mBeans = new ArrayList<ChatUsersDetailsBean>();
Context mcontext;
LayoutInflater inflater;
CommonUtil commonUtil;
public CustomAdapterChatActivity(Context context,
ArrayList<ChatUsersDetailsBean> mBeans) {
// TODO Auto-generated constructor stub
mcontext = context;
this.mBeans = mBeans;
commonUtil = new CommonUtil(context);
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mBeans.size();
}
#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 name;
TextView content;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.e("getView",""+"getView");
if (convertView == null) {
convertView = inflater.inflate(
R.layout.chat_listview_layout_screen, null);
}
setAttributes(position, convertView);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
click(position);
}
});
return convertView;
}
public void click(int position) {
Intent mintent = new Intent(mcontext, ChatViewBackgroundActivity.class);
mintent.putExtra("name", mBeans.get(position).getOpponent_name());
((ChatActivity) mcontext).startActivityForResult(mintent, 1);
}
public void setAttributes(final int position, View convertView) {
Holder holder = new Holder();
holder.imgdispatcher = (ImageView) convertView
.findViewById(R.id.chat_list_iv_profilepic);
}
Please help me. i struggle this part.
No error will be displayed.
i check getview run or not that time getview not working anytime.
this code any changes needed
Change your getView() method in your adapter class like below code:
public View getView (final int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView
return convertView;
}
While adding newly inflated view, try to avoid passing null as view root. Lint will now warn you not to pass in null for root. Your app won’t crash in this scenario, but it can misbehave. When your child View doesn’t know the correct LayoutParams for its root ViewGroup, it will try to determine them on its own using generateDefaultLayoutParams.
#AbiK If you already done like I mentioned on above comment means, use below code of modified getView() method and setAttributes() method. I hope It works for you.
public class CustomAdapterChatActivity extends BaseAdapter {
ArrayList<ChatUsersDetailsBean> mBeans;
Context mcontext;
LayoutInflater inflater;
CommonUtil commonUtil;
public CustomAdapterChatActivity(Context context,
ArrayList<ChatUsersDetailsBean> mBeans) {
mcontext = context;
this.mBeans = mBeans;
commonUtil = new CommonUtil(context);
}
#Override
public int getCount() {
return mBeans.size();
}
#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 name;
TextView content;
ImageView imgdispatcher;
}
public Holder setAttributes(final int position, View convertView) {
Holder holder = new Holder();
holder.imgdispatcher = (ImageView) convertView.findViewById(R.id.chat_list_iv_profilepic);
// holder.name=(TextView)convertView.findViewById(R.id.); // declare Id
// holder.content=(TextView)convertView.findViewById(R.id.); // declare Id
return holder;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = null;
LayoutInflater inflater = (LayoutInflater)
mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.chat_listview_layout_screen, parent, false);
holder = setAttributes(position, convertView);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
click(position);
}
});
return convertView;
}
public void click(int position) {
Intent mintent = new Intent(mcontext, ChatViewBackgroundActivity.class);
mintent.putExtra("name", mBeans.get(position).getOpponent_name());
((ChatActivity) mcontext).startActivityForResult(mintent, 1);
}
}
Hi i have problem with my adapter, i don't know how to refresh my data in ListView when i use adapter.notifyDataSetChanged() after data changing nothing happen. I wonder if i have to override notifyDataSetChanged() in this class. I hope you will help me.
Here is my adapter class:
public class ListViewAdapter extends BaseAdapter{
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.lista_wlasnych_spolek, null);
txtFirst=(TextView) convertView.findViewById(R.id.nazwa_spolki);
txtSecond=(TextView) convertView.findViewById(R.id.wartosc_akt);
txtThird=(TextView) convertView.findViewById(R.id.wartosc_kupna);
txtFourth=(TextView) convertView.findViewById(R.id.wartosc_calosci);
}
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
}
here im adding my fragment class which use this adapter:
public class Tab2Fragment extends Fragment {
private ArrayList<HashMap<String, String>> list;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";
public ListViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.spolki_portfel, container, false);
ListView listView=(ListView) v.findViewById(R.id.listView1);
list=new ArrayList<HashMap<String,String>>();
LayoutInflater inflater1 = getActivity().getLayoutInflater();
View header = inflater1.inflate(R.layout.naglowek_wlasne_spolki, listView, false);
listView.addHeaderView(header);
for(int i = 0 ; i < MainActivity.zmienne.lista_moich_spolek.size();i++){
dodaj_spolke(MainActivity.zmienne.lista_moich_spolek.get(i).nazwa,MainActivity.zmienne.lista_moich_spolek.get(i).wartosc_aktualna,MainActivity.zmienne.lista_moich_spolek.get(i).ilosc,MainActivity.zmienne.lista_moich_spolek.get(i).cena_kupna);
}
adapter=new ListViewAdapter(getActivity(), list);
listView.setAdapter(adapter);
//THIS INCREASE VALUE
MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++;
adapter.notifyDataSetChanged();
//CHECKING IF VALUE IS HIGHER
System.out.println("cena " + MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna);
return v;
}
Not exactly, but you're probably not actually updating the adapter's list. Make a method in your adapter that looks like this
public void updateItems(ArrayList<HashMap<String, String>> newList) {
list.clear();
list.addAll(newList);
this.notifyDataSetChanged();
}
Then call this from wherever you have the adapter set up
mAdapter.updateItems(newList)
Your problem might be because you don't have the right Adapter class. Try this one, I didn't had time to test it but it should work, also call notifiyDataSetChanged() after adding new elements to that list.
public class ListViewAdapter extends ArrayAdapter<HashMap<String, String>> {
public ArrayList<HashMap<String, String>> list;
private Activity activity;
public static final String FIRST_COLUMN="First";
public static final String SECOND_COLUMN="Second";
public static final String THIRD_COLUMN="Third";
public static final String FOURTH_COLUMN="Fourth";
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
super(activity, R.layout.lista_wlasnych_spolek, list);
this.activity=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public HashMap<String, String> getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ListViewHolder();
convertView = activity.getLayoutInflater().inflate(R.layout.lista_wlasnych_spolek, null);
listViewHolder.txtFirst = (TextView) convertView.findViewById(R.id.nazwa_spolki);
listViewHolder.txtSecond = (TextView) convertView.findViewById(R.id.wartosc_akt);
listViewHolder.txtThird = (TextView) convertView.findViewById(R.id.wartosc_kupna);
listViewHolder.txtFourth = (TextView) convertView.findViewById(R.id.wartosc_calosci);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ListViewHolder) convertView.getTag();
}
HashMap<String, String> map=list.get(position);
listViewHolder.txtFirst.setText(map.get(FIRST_COLUMN));
listViewHolder.txtSecond.setText(map.get(SECOND_COLUMN));
listViewHolder.txtThird.setText(map.get(THIRD_COLUMN));
listViewHolder.txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
public class ListViewHolder {
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
}
}
Ok, im sorry for wasting your time it was my fault i missed that i was changing source of data but no exacly ListView I should change list.get(0).put("Fourth", "5"); but i was changing MainActivity.zmienne.lista_moich_spolek.get(0).cena_kupna++; and I wondered why it didn't work.
I'd like to create a spinner with a list of names, and a list of ids from many .jpg's
I have a class with a String[] wich contains the names, and a Integer[] with the ids of the drawables.
My main class is the next
public class Settings extends Activity{
private Button btnGuardar, btnCerrar;
private Spinner spOperadoras, spTarifas;
private Operadoras op = new Operadoras();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
btnGuardar = (Button)findViewById(R.id.buttonSave);
btnCerrar = (Button)findViewById(R.id.buttonClose);
spOperadoras = (Spinner)findViewById(R.id.spinnerOperador);
spTarifas = (Spinner)findViewById(R.id.spinnerTarifas);
OperadorAdapter adapter = new OperadorAdapter(this, R.layout.custom_spinner, op);
spOperadoras.setAdapter(adapter);
}
}
Then my adapter is:
public class OperadorAdapter extends BaseAdapter implements SpinnerAdapter{
Activity context;
int spLayoutId;
Operadoras operadoras;
public OperadorAdapter(Activity context, int id, Operadoras op){
//super(context, id, op);
this.context = context;
this.operadoras = op;
this.spLayoutId = id;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View item = convertView;
TextView opName;
ImageView opIcon;
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(spLayoutId, null);
opName = (TextView) item.findViewById(R.id.nombreOperador);
opIcon = (ImageView) item.findViewById(R.id.imageOperador);
opName.setText(operadoras.getOperador(position).toString());
opIcon.setImageResource(operadoras.getLogo(position));
return item;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
But I have no results in my spinner.
What's the problem???
Thanks.
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
Return your actual count.
I created custom adapter for listview which contain text and images, on click of particular list item I have to open another activity, but I am not able to fire listview click event, below is my code.
Thanks.
Adapter
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private Context myCtx;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
myCtx = ctx;
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public String getItem(int position) {
return mStrings[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
Main acitivity
public class OurWishingWellActivity extends ListActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.list_view_image);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.reg_item_names);
TypedArray icons = res.obtainTypedArray(R.array.reg_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item,
options, icons));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Respected Aafag#
You have write onclick of listview like below.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show();
System.out.println("working!!!!!!!!!");
}
Add listener
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
convertView .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity();
}
});
return convertView;
}