Created
July 4, 2015 05:47
-
-
Save anonymous/b4d3597e913327afadd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="6dp" > | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:src="@android:drawable/ic_menu_info_details" | |
android:contentDescription="icon" | |
/> | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
> | |
<TextView android:id="@+id/text1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:textAppearance="@android:style/TextAppearance.Medium" | |
/> | |
<TextView android:id="@+id/text2" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
/> | |
<TextView android:id="@+id/text3" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
/> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.eademo; | |
import android.content.Context; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class LoaderTask extends AsyncTask<Void,Void,List<Product>> { | |
private final int mFrom; | |
private final int mTo; | |
private final Context mContext; | |
private final ProdAdapter mAdapter; | |
private boolean mReachedLastPage; | |
public LoaderTask(int from, int to, Context context, ProdAdapter adapter) { | |
mFrom = from; | |
mTo = to; | |
mContext = context; | |
mAdapter = adapter; | |
mReachedLastPage = false; | |
} | |
@Override | |
protected List<Product> doInBackground(Void... voids) { | |
List<Product> list = new ArrayList<Product>(); | |
try{ | |
//--just an example, fetch this from server-- | |
JSONObject response = new JSONObject(fetch()); | |
JSONArray products = response.getJSONArray("products"); | |
for (int i = 0; i < products.length(); i++) { | |
JSONObject p = products.getJSONObject(i); | |
String name = p.getString("name"); | |
String company = p.getString("company"); | |
String address = p.getString("address"); | |
String city = p.getString("city"); | |
list.add(new Product(name, company, address, city)); | |
} | |
if(response.getBoolean("last_page")){ | |
mReachedLastPage = true; | |
} | |
}catch (Exception e){ | |
Log.e("EA_DEMO","Error fetching product list",e); | |
} | |
return list; | |
} | |
@Override | |
protected void onPostExecute(List<Product> products) { | |
super.onPostExecute(products); | |
for (Product p : products){ | |
mAdapter.add(p); | |
} | |
mAdapter.notifyDataSetChanged(); | |
if (mReachedLastPage){ | |
mAdapter.notifyNoMoreItems(); | |
} | |
} | |
private String fetch() throws InterruptedException{ | |
//--this is a fake method to generate json and take some time-- | |
//--to simulate network loading-- | |
StringBuilder sb = new StringBuilder(); | |
sb.append("{\n"); | |
sb.append("\"products\": [\n"); | |
for (int i = mFrom; i <= mTo; i++){ | |
sb.append("{\n"); | |
sb.append("\"name\": \"item_"+i+"\",\n"); | |
sb.append("\"company\": \"company_"+i+"\",\n"); | |
sb.append("\"address\": \"street_"+i+"\",\n"); | |
sb.append("\"city\": \"city_"+i+"\"\n"); | |
if(i == mTo){ | |
sb.append("}\n"); | |
}else { | |
sb.append("},\n"); | |
} | |
} | |
sb.append("],\n"); | |
if(mTo >= 40){ | |
sb.append("\"last_page\": true\n"); | |
}else { | |
sb.append("\"last_page\": false\n"); | |
} | |
sb.append("}\n"); | |
synchronized (this){ | |
wait(4000); | |
} | |
Log.i("EA_DEMO",sb.toString()); | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<ListView android:id="@+id/list_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:footerDividersEnabled="true" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.eademo; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.widget.FrameLayout; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class MyActivity extends Activity { | |
/** | |
* Called when the activity is first created. | |
*/ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footer,null); | |
TextView footer = (TextView) footerLayout.findViewById(R.id.footer); | |
ListView lv = (ListView) findViewById(R.id.list_view); | |
lv.addFooterView(footerLayout); | |
//--page size = 10-- | |
ProdAdapter ad = new ProdAdapter(this,10, footer); | |
lv.setAdapter(ad); | |
//--load first 10 items-- | |
LoaderTask t = new LoaderTask(0,10,this,ad); | |
t.execute(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.eademo; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
public class ProdAdapter extends ArrayAdapter<Product> { | |
private boolean mHasMoreItems; | |
private final int mPageSize; | |
private final TextView mFooter; | |
public ProdAdapter(Context context, int pageSize, TextView footer) { | |
super(context, android.R.layout.two_line_list_item); | |
mPageSize = pageSize; | |
mFooter = footer; | |
mHasMoreItems = true; | |
} | |
public void notifyNoMoreItems(){ | |
mHasMoreItems = false; | |
mFooter.setText("No more Items"); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if(position == getCount() - 1 && mHasMoreItems){ | |
LoaderTask t = new LoaderTask(position + 1, position + 1 + mPageSize, getContext(),this); | |
t.execute(); | |
mFooter.setText("Loading . . ."); | |
} | |
if(convertView == null){ | |
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item,parent,false); | |
TextView t1 = (TextView) convertView.findViewById(R.id.text1); | |
TextView t2 = (TextView) convertView.findViewById(R.id.text2); | |
TextView t3 = (TextView) convertView.findViewById(R.id.text3); | |
convertView.setTag(new Holder(t1,t2,t3)); | |
} | |
Product p = getItem(position); | |
Holder h = (Holder) convertView.getTag(); | |
h.t1.setText("Product "+p.name); | |
h.t2.setText("Made by "+p.company); | |
h.t3.setText("Delivered to "+p.address+" in "+p.city); | |
return convertView; | |
} | |
private static class Holder{ | |
public final TextView t1; | |
public final TextView t2; | |
public final TextView t3; | |
private Holder(TextView t1, TextView t2, TextView t3) { | |
this.t1 = t1; | |
this.t2 = t2; | |
this.t3 = t3; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.eademo; | |
public class Product { | |
public final String name; | |
public final String company; | |
public final String address; | |
public final String city; | |
public Product(String name, String company, String address, String city) { | |
this.name = name; | |
this.company = company; | |
this.address = address; | |
this.city = city; | |
} | |
@Override | |
public String toString() { | |
return "name="+ name +",co="+ company +",ad="+ address +",city="+ company; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment