How can I parse a local JSON file from assets folder into a ListView?
As Faizan describes in their answer here:
First of all read the Json File from your assests file using below code.
and then you can simply read this string return by this function as
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("yourfilename.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
and use this method like that
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("formules");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
Log.d("Details-->", jo_inside.getString("formule"));
String formula_value = jo_inside.getString("formule");
String url_value = jo_inside.getString("url");
//Add your values in your `ArrayList` as below:
m_li = new HashMap<String, String>();
m_li.put("formule", formula_value);
m_li.put("url", url_value);
formList.add(m_li);
}
} catch (JSONException e) {
e.printStackTrace();
}
For further details regarding JSON Read HERE
How to parse json from a local json file into a ListView in android?
create a customList class(dont use hashmap)
change your custom list code (this is your code)
public class CustomList extends ArrayAdapter<String>
{
public final ArrayList<HashMap<String, String>> urlist;
private Activity context;
public CustomList(Activity context,ArrayList<HashMap<String, String>> urlist)
{
super(context, R.layout.list_single);
this.context = context;
this.urlist=urlist;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setText((CharSequence) urlist);
return rowView;
}
}
to
public class customList extends ArrayAdapter<String>{
Context context;
int layoutResourceId;
ArrayList<String> data = null;
public customList(Context context, int layoutResourceId, ArrayList<String> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (CustomHolder)row.getTag();
}
String s = data.get(postition);
holder.txtTitle.setText(s);
return row;
}
static class CustomHolder
{
TextView txtTitle;
}
}
and change your json parsing code and parse your json like
// JSONArray has four JSONObject
ArrayList<String> messages = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
messages.add(message);
}
and finally replace your this code
CustomList adapter = new CustomList(MainActivity.this,urlist);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
with this one
customList adapter = new customList(MainActivity.this,R.layout.list_single,messages); //messagses is your arraylist in which you added string by parsing your json (see before this code mean upward)
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
Solution 2
since you are showing only single text , you dont need to create your own custom adapter instead you can simply do.
ArrayList<String> messages = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
String message = jsonObj.getString("msg");
messages.add(message);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, messages);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "TEST.........", Toast.LENGTH_SHORT).show();
}
});
Related Topics
Sending Arrays with Intent.Putextra
Android Camera Will Not Work. Startpreview Fails
How to Know Whether Enough Memory Is Free to Deploy a New Application on a Linux MAChine
How to Get Desktop Class Supported Under Linux
Run Custom Textsecure (Signal) Server
Determine Linux Version from Java
Ugly Fonts in Java Applications on Ubuntu
How to Change a Jfreechart'S Size
Android: How to Enable/Disable Wifi or Internet Connection Programmatically
Change the System Brightness Programmatically
How to Get the First Day of the Current Week and Month
How to Open a Unc Path from Linux in Java