I currently have a program that can parse a JSONArray and put each item into a listview. But if you add something to the JSONArray the listview doesn't update until you run the app again. So how would I go about being able to update the listview when new items are added to the JSONArray. Here is the code I have that parses the JSONArray:
public class jarray extends Activity {
ArrayList<Actors> actorsList;
ActorAdapter adapter;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jarray);
actorsList = new ArrayList<Actors>();
new JSONAsyncTask().execute("http://bisonsoftware.us/hhs/messages.json");
ListView listview = (ListView) findViewById(R.id.list);
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
listview.setAdapter(adapter);
button = (Button)findViewById(R.id.button);
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(jarray.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
#Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("messages");
for (int i = 0; i < jarray.length(); i++) {
Actors actor = new Actors();
String message = jarray.getString(i);
actor.setName(message);
//actorsList.add(actor);
adapter.add(actor);
}
return true;
}
//------------------>>
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
If you need any more of my code or just need some more explanation feel free to ask. Could really use some help on this, thanks.
Here is the ActorAdapter code:
public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
#Override
public void add(Actors object) {
super.add(object);
notifyDataSetChanged();
}
public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
actorList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvName = (TextView) v.findViewById(R.id.tvName);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.tvName.setText(actorList.get(position).getName());
return v;
}
static class ViewHolder {
public TextView tvName;
}
}
Instead of adding directly to the ArrayList, try adding it from your adapter.
You need to instantaite your ActorAdapter before running your AsyncTask
sample:
setContentView(R.layout.activity_jarray);
actorsList = new ArrayList<Actors>();
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
ListView listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
new JSONAsyncTask().execute("http://bisonsoftware.us/hhs/messages.json");
button = (Button)findViewById(R.id.button);
When you add items to the adapter call the add method of the adapter not your arrayList
change this:
actorsList.add(actor);
to:
adapter.add(actor);
Add this in you ActorAdapter adapter:
#Override
public void add(Actors object) {
super.add(object);
notifyDataSetChanged();
}
change you oncreate into this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jarray);
actorsList = new ArrayList<Actors>();
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
ListView listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
new JSONAsyncTask().execute("http://bisonsoftware.us/hhs/messages.json");
button = (Button)findViewById(R.id.button);
}
The way you update a listview is by remaking the adapter with the updated information, setting the listadapter and then calling NotifyDataSetChanged(). This tells the device to refresh the layout.
ListView listview = (ListView) findViewById(R.id.list);
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
listview.setAdapter(adapter);
adapter.NotifyDataSetChanged();
Related
this is my main class where i am setting the adapter and calling the notifydatasetchanged() method the aim is to show the history of user in listview and refresh as the data is changed,but having error as stated earlier
public class HistoryActivity extends AppCompatActivity {
// Log tag
private static final String TAG = HistoryActivity.class.getSimpleName();
private static final String url ="http://192.168.0.102/android_login_api/historyfetch.php";
private ProgressDialog pDialog;
private List<HistoryItems> historyList = new ArrayList<HistoryItems>();
private ListView listView;
private CustomListAdapter adapter;
private SQLiteHandler db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, historyList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> user = db.getUserDetails();
String user_email = user.get("email");
gethistory(user_email);
}
private void gethistory(final String email)
{
StringRequest stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG,response.toString());
hideDialog();
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
historyItems.setName(obj.getString("user_email"));
historyItems.setLat_from(obj.getDouble("latitude_from"));
historyItems.setLon_from(obj.getDouble("longitude_from"));
historyItems.setLat_to(obj.getDouble("latitude_to"));
historyItems.setLon_to(obj.getDouble("longitude"));
historyItems.setDate(obj.getString("created_at"));
historyList.add(historyItems);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter;
})
{
protected Map<String,String>getParams(){
Map<String, String> params=new HashMap<>();
params.put("user_email",email);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
this is my custom adapter:-
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<HistoryItems> historyItems;
public CustomListAdapter(Activity activity, List<HistoryItems> historyItems) {
this.activity = activity;
this.historyItems = historyItems;
}
#Override
public int getCount() {
return historyItems.size();
}
#Override
public Object getItem(int location) {
return historyItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
TextView user_email = (TextView) convertView.findViewById(R.id.tv_user_email);
TextView lat_from = (TextView) convertView.findViewById(R.id.tv_lat_from);
TextView lon_from = (TextView) convertView.findViewById(R.id.tv_lon_from);
TextView lat_to = (TextView) convertView.findViewById(R.id.tv_lat_to);
TextView lon_to = (TextView) convertView.findViewById(R.id.tv_lon_to);
TextView date = (TextView) convertView.findViewById(R.id.tv_date);
// getting billionaires data for the row
HistoryItems m = historyItems.get(position);
// name
user_email.setText(m.getUser_email());
//
lat_from.setText(String.valueOf(m.getLat_from()));
lon_from.setText(String.valueOf(m.getLon_from()));
lat_to.setText(String.valueOf(m.getLat_to()));
lon_to.setText(String.valueOf(m.getLon_to()));
date.setText(String.valueOf(m.getDate()));
return convertView;
}
}
i am not getting where i made mistake
any help will be helpful....
hey man i didn't see any notifydatasetchanged() in your code i just see adapter;
Solution ==> adapter.notifydatasetchanged(); should be put after catch not outside the method
you have to give notifyDataSetChanged() for your adapter...
like this
for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
.
.
.
.
historyList.add(historyItems);
}
adapter.notifyDataSetChanged();
Hope, It will help you :)
every time u add new item adapter will add new item in the listview.
for(int i=0;i<response.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
HistoryItems historyItems = new HistoryItems();
historyItems.setName(obj.getString("user_email"));
historyItems.setLat_from(obj.getDouble("latitude_from"));
historyItems.setLon_from(obj.getDouble("longitude_from"));
historyItems.setLat_to(obj.getDouble("latitude_to"));
historyItems.setLon_to(obj.getDouble("longitude"));
historyItems.setDate(obj.getString("created_at"));
historyList.add(historyItems);
adapter.notifyDataSetChanged();
}
Hi guys I am implementing a listview with a clickable button through this tutorial -> https://www.youtube.com/watch?v=ZEEYYvVwJGY
and I want to achieve the same output.
But I have a slight problem, since my list view is populated from mysql database but in the tutorial is not populated from mysql database so we have a different adapter.
Codes in the tutorial
public class MainActivity extends AppCompatActivity{
private ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
}
}
Then lets assume that this is the tutorials MyListAdapter
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvid = (TextView) convertView.findViewById(R.id.studId);
viewHolder.tvname = (TextView) convertView.findViewById(R.id.studName);
viewHolder.btnP = (Button) convertView.findViewById(R.id.present);
viewHolder.btnA = (Button) convertView.findViewById(R.id.absent);
viewHolder.btnP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
return convertView;
}
}
and my problem is in this part
the setAdapter
mylistView = (ListView) findViewById(R.id.list);
final ListAdapter adapter = new SimpleAdapter(Attendance.this,
studentList, R.layout.list_att, new String[]{
TAG_ID, TAG_NAME}, new int[]{
R.id.studId, R.id.studName});
mylistView.setAdapter(adapter);
what should I do? any help would be appreciated thanks in advance :)
this is my code
public class Attendance extends AppCompatActivity {
TextView Date;;
ListView mylistView;
private ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();
TextView Name;
private static String url = "http://10.0.2.2/MobileClassRecord/getStudent.php";
private static final String TAG_STUDENTS = "students";
private static final String TAG_ID = "stud_id";
private static final String TAG_NAME = "stud_name";
JSONArray students = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Date = (TextView) findViewById(R.id.tvDate);
final Calendar calendar = Calendar.getInstance();
int dd = calendar.get(Calendar.DAY_OF_MONTH);
int mm = calendar.get(Calendar.MONTH);
int yy = calendar.get(Calendar.YEAR);
Date.setText(new StringBuilder().append(yy).append("-").append(mm + 1).append("-").append(dd));
new JSONParse().execute();
studentList = new ArrayList<HashMap<String, String>>();
}
private class JSONParse extends AsyncTask<String, String, JSONObject>{
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
Name =(TextView) findViewById(R.id.studName);
pDialog = new ProgressDialog(Attendance.this);
pDialog.setMessage("Getting Data from Database...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... params) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
#Override
protected void onPostExecute(JSONObject jsonObject) {
pDialog.dismiss();
try {
students = jsonObject.getJSONArray(TAG_STUDENTS);
for (int i = 0; i < students.length(); i++){
JSONObject c =students.getJSONObject(i);
final String Id = c.getString(TAG_ID);
String Name = c.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, Id);
map.put(TAG_NAME, Name);
studentList.add(map);
mylistView = (ListView) findViewById(R.id.list);
final ListAdapter adapter = new SimpleAdapter(Attendance.this,
studentList, R.layout.list_att, new String[]{
TAG_ID, TAG_NAME}, new int[]{
R.id.studId, R.id.studName});
mylistView.setAdapter(adapter);
mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) (view.findViewById(R.id.studId))).getText().toString();
Toast.makeText(Attendance.this, "List Clicked " + ID, Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
preferences.edit().putString("id", ID).commit();
}
});
}
}catch (JSONException e){
e.printStackTrace();
}
}
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvid = (TextView) convertView.findViewById(R.id.studId);
viewHolder.tvname = (TextView) convertView.findViewById(R.id.studName);
viewHolder.btnP = (Button) convertView.findViewById(R.id.present);
viewHolder.btnA = (Button) convertView.findViewById(R.id.absent);
viewHolder.btnP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.tvid.setText(getItem(position));
return convertView;
}
}
public class ViewHolder{
TextView tvid, tvname;
Button btnP, btnA;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_attendance, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
Intent intent = new Intent(Attendance.this, SpecificClassRecord.class);
startActivity(intent);
return true;
case R.id.action_view_attendance:
Intent intent1 = new Intent(Attendance.this, ViewAttendance.class);
startActivity(intent1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Sample Layout
like Udi said, more information would be nice.
One strange thing in your code is in the getView() method.
you have
if(convertView==null){
...
}else{
...
}
I think the else part is wrong. Just remove the else and the brackets (not the code insight of it).
The code in the else part is important, because without it the code will not change the TextView when the View is loaded for the first time. This mean, that your getView will do nothing
EDIT
The author has changed the code. You have now two different adapters there...
In my opinion the getView() Method should look like this
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvid = (TextView) convertView.findViewById(R.id.studId);
viewHolder.tvname = (TextView) convertView.findViewById(R.id.studName);
viewHolder.btnP = (Button) convertView.findViewById(R.id.present);
viewHolder.btnA = (Button) convertView.findViewById(R.id.absent);
viewHolder.btnP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked " + position, Toast.LENGTH_LONG).show();
}
});
convertView.setTag(viewHolder);
}
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.tvid.setText(getItem(position));
return convertView;
}
}
But for me it is still not clear where exactly the problem is. Is the App crashing or is there just a behavior you don't understand? If yes, what exactly?
If the App is crashing, when exactly?
EDIT 2:
mylistView = (ListView) findViewById(R.id.list);
final ListAdapter adapter = new MyListAdapter(Attendance.this,
R.layout.list_att, new String[]{
TAG_ID, TAG_NAME});
mylistView.setAdapter(adapter);
maybe this is all. I assume the layout of your rows is given by the layoutressource R.layout.list_att and that TAG_ID and TAG_Name are defined somewhere before.
I see your new Adapter needs a List of Strings. Insert the following before you set the adapter
List<String> test = new ArrayList<String>();
test.add("1");
test.add("2"):
test.add("3");
and then your adapter should look like this
final ListAdapter adapter = new MyListAdapter(Attendance.this,
R.layout.list_att, test);
I implemented a custom list view using customadapter. When I press on refresh button, the data is fetched from database and updated in list view. But in my case the items get appended after the previous items i.e. if i have 2 items in databse and I press refresh button without changing the database items,the same items gets appended and 4 listitems get displayed. Quick help required. notifyDatasetChanged() is used in code. But I don't know if it's correct.
Here is code for MainActivity.java
public class MainActivity extends Activity {
ListView lv;
TextView tv1,tv2,tv3;
ArrayList<String> a=new ArrayList<String>();
ArrayList<String> b=new ArrayList<String>();
ArrayList<String> c=new ArrayList<String>();
ArrayList<String> d=new ArrayList<String>();
String mydata,name,name1,society,date,venue;
public String[] s1 = new String[50];
public String[] s2=new String[50];
public String[] s3=new String[50];
public String[] s4=new String[50];
public int[] img = {R.drawable.rty, R.drawable.sf, R.drawable.rty};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView);
lv = (ListView) findViewById(R.id.listView);
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = cd.isConnectingToInternet(); // true or false
if(isInternetPresent) {
new MyData().execute();
}
else
Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_SHORT).show();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = new Intent(MainActivity.this, listclick.class);
in.putExtra("position", position);
startActivity(in);
}
});
}
public void abc(View v)
{
Intent in=new Intent(MainActivity.this,webform.class);
startActivity(in);
}
public void ref(View v)
{
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = cd.isConnectingToInternet();
if(isInternetPresent) {
new MyData().execute();
}
else
Toast.makeText(MainActivity.this,"No Internet Connection",Toast.LENGTH_SHORT).show();
}
public class MyData extends AsyncTask<String,String,String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
CustomAdapter cad = new CustomAdapter(MainActivity.this, s1, img,s2,s3,s4);
lv.setAdapter(cad);
cad.notifyDataSetChanged();
}
#Override
protected String doInBackground(String... params) {
getData();
return null;
}
}
public void getData()
{
try {
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://collegeevents.esy.es/abc.php");
HttpResponse response=httpClient.execute(httpPost);
HttpEntity httpEntity=response.getEntity();
InputStream is=httpEntity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder strbuilder=new StringBuilder();
String line=null;
while ((line=reader.readLine())!=null)
{
strbuilder.append(line);
}
is.close();
mydata=strbuilder.toString();
JSONArray obj=new JSONArray(mydata);
for(int i=0;i<obj.length();i++)
{
JSONObject obj1=obj.getJSONObject(i);
a.add(i,obj1.getString("Name"));
b.add(i,obj1.getString("society"));
c.add(i,obj1.getString("venue"));
d.add(i,obj1.getString("date"));
}
String[] s = new String[a.size()];
s=a.toArray(s);
s1 = s;
String[] soc = new String[b.size()];
soc=b.toArray(soc);
s2 = soc;
String[] ven = new String[c.size()];
ven=c.toArray(ven);
s3 = ven;
String[] dat = new String[d.size()];
dat=d.toArray(dat);
s4 = dat;
}
catch (Exception e)
{
}
}
}
Here is CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String> {
Context c1;
String s1[],soc[],ven[],dat[];
int s2[];
CustomAdapter(Context c,String s[],int s3[],String society[],String venue[],String date[])
{
super(c, R.layout.listcustom, s);
this.c1=c;
this.s1=s;
this.s2=s3;
this.soc=society;
this.ven=venue;
this.dat=date;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.listcustom,parent,false);
TextView tv=(TextView)v.findViewById(R.id.textView);
TextView tv1=(TextView)v.findViewById(R.id.society);
TextView tv2=(TextView)v.findViewById(R.id.venue);
TextView tv3=(TextView)v.findViewById(R.id.date);
tv.setText(s1[position]);
tv1.setText(soc[position]);
tv2.setText(ven[position]);
tv3.setText(dat[position]);
if(position%2==0) {
tv.setTextColor(Color.parseColor("#01579B"));
tv3.setTextColor(Color.parseColor("#01579B"));
}
else{
tv.setTextColor(Color.parseColor("#00897B"));
tv3.setTextColor(Color.parseColor("#00897B"));
}
v.setTag(position);
//notifyDataSetChanged();
return v;
}
}
I finally got it. The short answer for you question — you are not clearing a, b, c, d ArrayLists. So each getData() call adds data while previous data is still there. So, you should add a.clear(), b.clear() ... etc. at the start of getData.
However i would suggest to make the following improvements:
Introduce entity for adapter data. This entity will contain 4 fields: name, society, venue, date.
Thus you don't need to use 4 arrays and 4 ArrayLists. For convience i will use "Event" as entity name. This will lok something like:
AsyncTask:
public class MyData extends AsyncTask<String, String, Event[]> {
#Override
protected Event[] doInBackground(String... params) {
return getData();
}
#Override
protected void onPostExecute(Event[] s) {
cad.clear();
cad.addAll(s)
}
}
Activity:
public class MainActivity extends Activity {
private CustomAdapter cad;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
lv = (ListView) findViewById(R.id.listView);
cad = new CustomAdapter(getApplicationContext());
}
}
CustomAdapter:
public class CustomAdapter extends ArrayAdapter<Event> {
private final LayoutInflater inflater;
public CustomAdapter(Context c, Event ev[]) {
super(context, ev[]);
inflate = LayoutInflater.from(context);
}
CustomAdapter(Context c) {
this(context, new Event[0]);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View v;
if(convertView == null) {
v = inflater.inflate(...)
} else {
v = convertView;
}
// find views or event better — use ViewHolder pattern
Event evt = getItem(position);
tv.setText(evt.getName());
...
return v;
}
}
Don't use onClick tag. Use view#setOnClickListener instead. onClick binds layout and activity implementation and that's not good.
Use clear names. Something like LoadDataTask instead of MyData.
I'm passing id form FirstFragment to SecondFragment. I need that I'd to send POST request to web service to retrieve JSON, which I need to create content of SecondFragment. SecondFragment is ListView with custom layout. I'm adding that layout with my custom adapter class (MyAdapter). For JSON I also have AsyncTask class (GetCategoryTas) The problem is that the SecondFragment is created before service give me result back. So content is always blank. How can I fix this.
FirstFragment:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putString("id", menuList.get(position).getAlias());
categoryPage = new FragmentCategoryPage();
categoryPage.setArguments(bundle);
transaction = manager.beginTransaction();
transaction.replace(R.id.mainContainer, categoryPage, "CategoryPage");
transaction.addToBackStack("CategoryPage");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
SecondFragment:
public class FragmentCategoryPage extends Fragment implements OnItemClickListener {
private ListView lvCategory;
private List<NameValuePair> pair;
private ArrayList<ItemCategory> itemCatList = new ArrayList<ItemCategory>();
private ItemCategory itemCat;
private MyAdapter myAdapter;
private Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_category, container, false);
context = getActivity();
String id = getArguments().getString("id");
pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("category", id));
new GetCategoryTask().execute();
lvCategory = (ListView) view.findViewById(R.id.lvCategory);
myAdapter = new MyAdapter();
lvCategory.setAdapter(myAdapter);
lvCategory.setOnItemClickListener(this);
return view;
}
// Creating own adapter for categories
class MyAdapter extends BaseAdapter {
#Override
public View getView(final int pos, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.item_layout_category, parent, false);
} else {
row = convertView;
}
TextView tvCatTitle = (TextView) row.findViewById(R.id.tvCategoryTitle);
TextView tvCatDate = (TextView) row.findViewById(R.id.tvCategoryDate);
ImageView ivCatImage = (ImageView) row.findViewById(R.id.ivCategoryImage);
return row;
}
#Override
public int getCount() { return itemCatList.size(); }
#Override
public Object getItem(int pos) { return null; }
#Override
public long getItemId(int pos) { return 0; }
}
public class GetCategoryTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Variables.URL_CATEGORY);
httpPost.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String data = EntityUtils.toString(httpEntity);
int status = httpResponse.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jRealObject = jArray.getJSONObject(i);
itemCat = new ItemCategory();
itemCat.setId(jRealObject.get("id").toString());
itemCat.setTitle(jRealObject.get("title").toString());
itemCat.setImage_name(jRealObject.get("image_name").toString());
itemCatList.add(itemCat);
}
return true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result == false) {
Toast.makeText(context, Variables.ERROR_MESSAGE, Toast.LENGTH_SHORT).show();
}
}
}
}
How can I fix this problem?
You should refresh your adapter in onPostExecute() by calling;
myAdapter.notifyDataSetChanged();
I dont see anywhere in your code that you have connected your listview with the itemCatList. And you should do anything you wanna do with your listview in onPostExecute, not in onCreate, so you could be sure that the list exists in that point as AsyncTask runs on its own thread
in onPostExecute
lv = (ListView) findViewById(R.id.your_list_view_id);
List<String> your_array_list = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
I've got a ListView with a 'show next results' button. The list is filled by a custom adapter extending BaseAdapter. Using it as shown below, only the new results are shown.
How can I append the new results to the list?
ListView listView = (ListView)findViewById(android.R.id.list);
// Show next results button
View footerView = ((LayoutInflater)ItemList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_listview, null, false);
listView.addFooterView(footerView);
footerView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = getIntent();
i.putExtra("firstIndex", mFirstIndex + NRES_PER_PAGE);
i.putExtra("itemCount", NRES_PER_PAGE);
startActivity(i);
}
});
mItems = json.getJSONArray("data");
setListAdapter(new ItemAdapter(ItemList.this, mType, mItems));
FIX
ListActivity
public class ItemList extends MenuListActivity{
ItemAdapter mItemAdapter;
Integer mFirstIndex = 0;
JSONArray mItems = new JSONArray();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_list);
// Set data adapter
mItemAdapter = new ItemAdapter(ItemList.this, mType, mItems);
ListView listView = (ListView)findViewById(android.R.id.list);
View footerView = ((LayoutInflater)ItemList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_listview, null, false);
listView.addFooterView(footerView);
footerView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressDialog = MyProgressDialog.show(ItemList.this, null, null);
mFirstIndex = mFirstIndex + ITEM_COUNT;
new GetItemInfoList().execute();
}
});
setListAdapter(mItemAdapter);
new GetItemInfoList().execute();
}
private class GetItemInfoList extends AsyncTask<Void, Void, JSONObject> {
protected JSONObject doInBackground(Void... params) {
// Set POST data to send to web service
List<NameValuePair> postData = new ArrayList<NameValuePair>(2);
postData.add(new BasicNameValuePair("firstindex", Integer.toString(mFirstIndex)));
postData.add(new BasicNameValuePair("itemscount", Integer.toString(ITEM_COUNT)));
JSONObject json = RestJsonClient.getJSONObject(URL_ITEMINFOLIST, postData);
return json;
}
protected void onPostExecute(JSONObject json) {
try {
// Get data from json object and set to list adapter
JSONArray jsonArray = json.getJSONArray("data");
for(int i=0; i<jsonArray.length(); i++)
mItems.put(jsonArray.get(i));
mItemAdapter.notifyDataSetChanged();
ListView listView = (ListView)findViewById(android.R.id.list);
View footerView = ((LayoutInflater)ItemList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_listview, null, false);
listView.addFooterView(footerView);
footerView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressDialog = MyProgressDialog.show(ItemList.this, null, null);
mFirstIndex = mFirstIndex + ITEM_COUNT;
new GetItemInfoList().execute();
}
});
} catch (JSONException e) {
}
}
}
}
Adapter
public class ItemAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private JSONArray mItems;
private ImageLoader mImageLoader;
private int mCategory;
public ItemAdapter(Context context, int category, JSONArray items) {
mContext = context;
mInflater = LayoutInflater.from(context);
mItems = items;
mCategory = category;
this.mImageLoader = new ImageLoader(context, true);
}
public int getCount() {
return mItems.length();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_row, null);
holder = new ViewHolder();
holder.listitem_pic = (ImageView) convertView.findViewById(R.id.listitem_pic);
holder.listitem_desc = (TextView) convertView.findViewById(R.id.listitem_desc);
holder.listitem_title = (TextView) convertView.findViewById(R.id.listitem_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
JSONObject item = mItems.getJSONObject(position);
String listitem_pic = item.getString("picture");
holder.listitem_pic.setTag(listitem_pic);
mImageLoader.DisplayImage(listitem_pic, (Activity)mContext, holder.listitem_pic);
holder.listitem_title.setText(item.getString("title"));
holder.listitem_desc.setText(item.getString("desc"));
}
catch (JSONException e) {
}
return convertView;
}
static class ViewHolder {
TextView listitem_title;
ImageView listitem_pic;
TextView listitem_desc;
}
}
It depends on your implementation of ItemAdapter, I'd recommend holding a reference to ItemAdapter, then updating the data set behind it and then calling notifyDataSetChanged() on it. something like:
ItemAdapter ia = new ItemAdapter(ItemList.this, mType, mItems);
setListAdapter(ia);
footerView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mItems.append(newItems);
ia.notifyDataSetChanged();
}
});
It is tricky without knowing what data you are using or whether you have the entire data set available at the start.