Start chronometer on each row of Listview in android - java

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 :)

Related

Array Adapter's "getView()" doesn't get called [duplicate]

Here is the code of the fragment in which I am setting a custom adapter to the list.
There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. I don't see ("Inside", "GetView") in the logcat
Fragment
public class ServiceCarListFragment extends Fragment {
private String url;
private ArrayList<CarDetail> carDetailList = new ArrayList<CarDetail>();
private CarListAdapter adapter;
private ListView mList;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
url = getActivity().getIntent().getStringExtra("url");
new DownloadCarDetail().execute(url);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_service_car_list, container, false);
mList = (ListView) v.findViewById(R.id.list);
mList.setAdapter(adapter);
for (CarDetail car : carDetailList) {
// START LOADING IMAGES FOR EACH STUDENT
car.loadImage(adapter);
}
return v;
}
class DownloadCarDetail extends AsyncTask<String, String, ArrayList<CarDetail>> {
#Override
protected ArrayList<CarDetail> doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<CarDetail> carDetailList = JsonParser.parseJson(params[0]);
return carDetailList;
}
#Override
protected void onPostExecute(ArrayList<CarDetail> carDetailList) {
// TODO Auto-generated method stub
ServiceCarListFragment.this.carDetailList = carDetailList;
Log.d("dccs", String.valueOf(ServiceCarListFragment.this.carDetailList.size()));
adapter = new CarListAdapter(getActivity(), ServiceCarListFragment.this.carDetailList);
Log.d("dccs", String.valueOf((adapter.getCount())));
}
}
}
CustomAdapter
public class CarListAdapter extends BaseAdapter {
private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
private Context context;
public CarListAdapter(Context context, ArrayList<CarDetail> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(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
Log.d("Inside", "GetView");
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
ViewHolder holder = null;
CarDetail car = items.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.car_list_row, null);
holder = new ViewHolder();
holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvCarName.setText(car.getCarName());
if (car.getImage() != null) {
holder.imgCar.setImageBitmap(car.getImage());
} else {
// MY DEFAULT IMAGE
holder.imgCar.setImageResource(R.drawable.ic_action_call);
}
return convertView;
}
static class ViewHolder {
TextView tvCarName;
TextView tvDailyPriceValue;
TextView tvWeeklyPriceValue;
ImageView imgCar;
}
}
The only reasons getView is not called are:
getCount returns 0.
you forget to call setAdapter on the ListView.
If the ListView's visibility (or its container's visibility) is GONE. Thanks to #TaynãBonaldo for the valuable input.
ListView is not attached to any viewport layout. That is, mListView = new ListView(...) is used without myLayout.addView(mListView).
In the onPostExcute, after you create a new instance of CarListAdapter I will suggest you to update the new instance to your ListView. Indeed you need to call again
mList.setAdapter(adapter);
Edit: setAdapter should be always called on the ui thread, to avoid unexpected behaviours
Edit2:
The same applies to RecyclerView. Make sure that
getItemCount is returning a value grater than 0 (usually the dataset size)
both setLayoutManager and setAdapter have to be called on the UI Thread
The visibility of the widget has to be set to VISIBLE
you must verify that the list has elements might have an error when adding items to your list .
To verify , use the method:
adapter.getCount();
I faced similar problem. Here is a simple work around to solve it:
In your onCreateView, you will have to wait before the view gets created. So change your lines from this:
mList = (ListView)v.findViewById(R.id.list);
mList.setAdapter(adapter);
CHANGE THE ABOVE TWO LINES INTO:
mList = (ListView)v.findViewById(R.id.list);
mList.post(new Runnable() {
public void run() {
mList.setAdapter(adapter);
}
});
Hope this will help others who would run into similar problem
You are missing the super class in the constructor. See my example below:
public AppDataAdapter(Activity a, int textViewResourceId, ArrayList<AppData> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
What you have been doing is
In your adapter
public CarListAdapter(Context context , ArrayList<CarDetail> items) {
this.context = context;
this.items = items;
}
in your Fragment
adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
I hope you will be using FragmentActivity
You need to call
adapter = new CarListAdapter(YOUR_ACTIVITY_CONTEXT, carDetailList);
where YOUR_ACTIVITY_CONTEXT will be your FragmentActivity
I had the same problem. And after trying all tips above my getView was still not being called. So I tried to remove the ScrollView that I used outside the ListView. Then the getView worked well. Just for add one more posibility. I Hope help someone.

Android RecyclerView not populating using Volley [duplicate]

Here is the code of the fragment in which I am setting a custom adapter to the list.
There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. I don't see ("Inside", "GetView") in the logcat
Fragment
public class ServiceCarListFragment extends Fragment {
private String url;
private ArrayList<CarDetail> carDetailList = new ArrayList<CarDetail>();
private CarListAdapter adapter;
private ListView mList;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
url = getActivity().getIntent().getStringExtra("url");
new DownloadCarDetail().execute(url);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_service_car_list, container, false);
mList = (ListView) v.findViewById(R.id.list);
mList.setAdapter(adapter);
for (CarDetail car : carDetailList) {
// START LOADING IMAGES FOR EACH STUDENT
car.loadImage(adapter);
}
return v;
}
class DownloadCarDetail extends AsyncTask<String, String, ArrayList<CarDetail>> {
#Override
protected ArrayList<CarDetail> doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<CarDetail> carDetailList = JsonParser.parseJson(params[0]);
return carDetailList;
}
#Override
protected void onPostExecute(ArrayList<CarDetail> carDetailList) {
// TODO Auto-generated method stub
ServiceCarListFragment.this.carDetailList = carDetailList;
Log.d("dccs", String.valueOf(ServiceCarListFragment.this.carDetailList.size()));
adapter = new CarListAdapter(getActivity(), ServiceCarListFragment.this.carDetailList);
Log.d("dccs", String.valueOf((adapter.getCount())));
}
}
}
CustomAdapter
public class CarListAdapter extends BaseAdapter {
private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
private Context context;
public CarListAdapter(Context context, ArrayList<CarDetail> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(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
Log.d("Inside", "GetView");
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
ViewHolder holder = null;
CarDetail car = items.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.car_list_row, null);
holder = new ViewHolder();
holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvCarName.setText(car.getCarName());
if (car.getImage() != null) {
holder.imgCar.setImageBitmap(car.getImage());
} else {
// MY DEFAULT IMAGE
holder.imgCar.setImageResource(R.drawable.ic_action_call);
}
return convertView;
}
static class ViewHolder {
TextView tvCarName;
TextView tvDailyPriceValue;
TextView tvWeeklyPriceValue;
ImageView imgCar;
}
}
The only reasons getView is not called are:
getCount returns 0.
you forget to call setAdapter on the ListView.
If the ListView's visibility (or its container's visibility) is GONE. Thanks to #TaynãBonaldo for the valuable input.
ListView is not attached to any viewport layout. That is, mListView = new ListView(...) is used without myLayout.addView(mListView).
In the onPostExcute, after you create a new instance of CarListAdapter I will suggest you to update the new instance to your ListView. Indeed you need to call again
mList.setAdapter(adapter);
Edit: setAdapter should be always called on the ui thread, to avoid unexpected behaviours
Edit2:
The same applies to RecyclerView. Make sure that
getItemCount is returning a value grater than 0 (usually the dataset size)
both setLayoutManager and setAdapter have to be called on the UI Thread
The visibility of the widget has to be set to VISIBLE
you must verify that the list has elements might have an error when adding items to your list .
To verify , use the method:
adapter.getCount();
I faced similar problem. Here is a simple work around to solve it:
In your onCreateView, you will have to wait before the view gets created. So change your lines from this:
mList = (ListView)v.findViewById(R.id.list);
mList.setAdapter(adapter);
CHANGE THE ABOVE TWO LINES INTO:
mList = (ListView)v.findViewById(R.id.list);
mList.post(new Runnable() {
public void run() {
mList.setAdapter(adapter);
}
});
Hope this will help others who would run into similar problem
You are missing the super class in the constructor. See my example below:
public AppDataAdapter(Activity a, int textViewResourceId, ArrayList<AppData> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
What you have been doing is
In your adapter
public CarListAdapter(Context context , ArrayList<CarDetail> items) {
this.context = context;
this.items = items;
}
in your Fragment
adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
I hope you will be using FragmentActivity
You need to call
adapter = new CarListAdapter(YOUR_ACTIVITY_CONTEXT, carDetailList);
where YOUR_ACTIVITY_CONTEXT will be your FragmentActivity
I had the same problem. And after trying all tips above my getView was still not being called. So I tried to remove the ScrollView that I used outside the ListView. Then the getView worked well. Just for add one more posibility. I Hope help someone.

how to use notifyDataSetChanged()

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.

How do i add search functionality in gridview in android?

My android app consists of a gridview with an image and a text below it. I am passing a array of strings into the textview. I want the edittext field to act as a search bar and filter the results.I have seen other questions relating this but they seem to filter an arraylist which is passed into a Listview. How do i filter an array of strings in a gridview?
I know i should add a a TextWatcher to listen for text changes on the EditText but after that how do i filter my data?
Here is my code
Mainactivity.java
public class MainActivity extends Activity {
EditText inputSearch;
GridView grid;
public static String[] Sname = {
"one",
"two",
"three",
"four",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CustomGrid adapter = new CustomGrid(MainActivity.this, Sname);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + Sname[+position], Toast.LENGTH_SHORT).show();
}
});
inputSearch = (EditText) findViewById(R.id.editText);
}
}
CustomGrid.java
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] Sname;
public CustomGrid(Context c,String[] Sname) {
mContext = c;
this.Sname = Sname;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return Sname.length;
}
#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;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(Sname[position]);
imageView.setImageResource(R.mipmap.image1);
return grid; }}
You can add your adapter ie CustomGrid a filter(String) method.
You also add a field String [] toDisplay in your adapter.
In your constructor you add toDisplay = Sname and in all your other methos you replace Snameby toDisplay
In the filter method create a new string array with only the string from Sname that match the desired string. Set this new array to toDisplay.
Then call the notifyDataSetChanged method on your CustomGrid
Example of filtering method :
public void filter(String toMatch) {
List<String> matches = new ArrayList<String>();
for (String string : Sname) {
if (string.matches(toMatch)) {//on whatever mathcing you feel to use
matches.add(string);
}
}
toDisplay = new String[matches.size()];
toDisplay = matches.toArray(toDisplay);
notifyDataSetChanged(); //update your grid view
}

How to send ArrayList into BaseAdapter Android

I want to display my JSON into gridview, before it.. i displayed my JSON into ListView, and it works. but in BaseAdapter, i don't know how to send my JSON that I have put into ArrayList into Base Adapter
so this is my source code :
Activity :
public class MainActivity extends ListActivity {
List AgenList = new ArrayList();
boolean boolStatusKoneksi=true;
private ProgressDialog Dialog;
protected Context applicationContext;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AgenAsyncTask().execute();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new AgenAdapter(this));
}
public class AgenAsyncTask extends AsyncTask<String, String, String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
Dialog = new ProgressDialog(MainActivity.this);
Dialog.setMessage("Mohon Tunggu sebentar...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
protected String doInBackground(String... args) {
String url = ("http:10.10.2/selectAgent.htm");
try{
JSONParser j=new JSONParser();
JSONArray jsonArray = j.takeJson(url);;
for(int i =0; i<jsonArray.length(); i++){
JSONObject c = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
if (c.has("atasan"))
map.put("atasan", c.get("atasan").toString());
if (c.has("nama_agen"))
map.put("nama_agen", c.get("nama_agen").toString());
if (c.has("kode_agen"))
map.put("kode_agen", c.get("kode_agen").toString());
if (c.has("no_aaji"))
map.put("no_aaji", c.get("no_aaji").toString());
if (c.has("jenis"))
map.put("jenis", c.get("jenis").toString());
AgenList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String file_url) {
}
}
This is my BaseAdapter :
public class AgenAdapter extends BaseAdapter {
public AgenAdapter(MainActivity mainActivity) {
}
#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;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layout = getLayoutInflater();
View view= layout.inflate(R.layout.list_item,parent,false);
TextView ATASAN = (TextView) findViewById(R.id.atasan);
TextView NAMA_AGEN= (TextView) findViewById(R.id.nama_agen);
TextView KODE_AGEN= (TextView) findViewById(R.id.kode_agen);
TextView NO_AAJI= (TextView) findViewById(R.id.no_aaji);
TextView JENIS= (TextView) findViewById(R.id.jenis);
return view;
}
}
}
As you see, i have ArrayList named as = AgenList but i haven't put it into my BaseAdapter.
make a parametrized constructor and pass your array list into its paramerts. so your class will be like
public class AgenAdapter extends BaseAdapter {
List<yourObj> list;
Activity a;
public AgenAdapter(Activity activity,List<yourObj> list) {
this.a=activity;
this.list=list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();///////return size of list
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;///// dont return null here
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;/////////return position as itemID
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layout = getLayoutInflater();
View view= layout.inflate(R.layout.list_item,parent,false);
TextView ATASAN = (TextView) findViewById(R.id.atasan);
TextView NAMA_AGEN= (TextView) findViewById(R.id.nama_agen);
TextView KODE_AGEN= (TextView) findViewById(R.id.kode_agen);
TextView NO_AAJI= (TextView) findViewById(R.id.no_aaji);
TextView JENIS= (TextView) findViewById(R.id.jenis);
return view;
}
}
try this.
gridview.setAdapter(new AgenAdapter(this), AgenList);
and use this line in your onPostExecute() instead of onCreate()
Like this:
#Override
protected void onPostExecute(String file_url) {
gridview.setAdapter(new AgenAdapter(this), AgenList);
}
Have a look at ArrayAdapter. It contains all of the implementation for an Adapter based on an ArrayList.
Make your adapter a subclass of ArrayAdapter, giving a parametrised type. Yours in this case being Map<String, String>
public class ResultAdapter extends ArrayAdapter<Map<String,String>> {
private int mResource;
/**
* #param context
* #param resource
*/
public ResultAdapter(Context context, int resource, ArrayList<Map<String,String>> dataList) {
super(context, resource, dataList);
this.mResource = resource;
}
...
}
Override the getView method of the ArrayAdapter to use to set the data given to the views.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
// If the view is already inflated, reuse it.
// Else inflate the view
if (convertView != null) {
view = convertView;
} else {
//inflate view
final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(mResource, parent, false);
}
TextView ATASAN = (TextView) findViewById(R.id.atasan);
...
//Get data here
Map<String, String> item = getItem(position);
//Set data to the View's e.g.
ATASAN.setText(item.get("ATASAN"));
...
return view;
}

Categories

Resources