I am developing Android application where I want to display the json format text and image retrieved from web service in horizontal list view. I am storing json formated data into ArrayList
Asked
Active
Viewed 876 times
2 Answers
1
The first thing is how to display a horizontal ListView. I want to recommend you use RecyclerView
for smoother listView display. So how to display recyclerView in Horizontal? This is my tutorial step by step.
First, in xml file should be like this:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:scrollbars="vertical" />
Second, in the Activity's onCreate
should be like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewItem = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager itemslayoutManager = new LinearLayoutManager(getApplicationContext());
itemslayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerViewItem.setLayoutManager(itemslayoutManager);
SampleAdapter sampleAdapter = new SampleAdapter(List<SampleData>);
recyclerViewItem.setAdapter(sampleAdapter);
return true;
}
To parse JSON you should use library called: Retrofit
instead of AsyncTask
try this:
Retrofit

TranHieu
- 914
- 6
- 23
0
activity_main.xml
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="4dp"
android:stretchMode="columnWidth"
android:id="@+id/gallery_child_list"
android:background="#E4E4E4"
android:verticalSpacing="4dp" />
Mainactivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
try {
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("YOUR_URL");
HttpResponse response = client.execute(httppost);
HttpEntity resEntity = response.getEntity();
String json = EntityUtils.toString(resEntity);
JSONObject jObject = new JSONObject(json);
// Locate the array name in JSON
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonobject = jObject.optJSONObject("online");
for (int i = 0; i < jObject.length(); i++) {
map.put("date", jsonobject.getString("men"));
map.put("flag", jsonobject.getString("kids"));
map.put("flag", jsonobject.getString("women"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (Exception e1) {
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new ViewPagerAdapter(MainActivity.this, arraylist);
viewPager.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
adapter.notifyDataSetChanged();
}
}
viewpageradapter
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ViewPagerAdapter(Context context, ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,false);
// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position
resultp = data.get(position);
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), imgflag);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}

Mukeshkumar S
- 785
- 1
- 14
- 30
-
Getting JSON Data is based on your JSON URL. This JSON will work if you dont have JSON Array. – Mukeshkumar S Nov 12 '15 at 06:26