I am trying to make list view in android .I am able to make simple list view using static data .Now I take json file in asset folder . Then I read contend from json file and display on list view .It is working fine.
I do like this
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
Log.d("==", loadJSONFromAsset());
String arr[];
try {
JSONArray js = new JSONArray(loadJSONFromAsset());
arr = new String[js.length()];
for (int i = 0; i < js.length(); i++) {
JSONObject obj = js.getJSONObject(i);
arr[i] = obj.getString("name");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("data.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;
}
}
It is working fine But I need to do different task .I need to read json file from asset folder . and create as same number of column as in json array
This is new json file
{"coulmns":[
{
"name": "Test_1"
},
{
"name": "Test_2"
},
{
"name": "Test_3"
},
{
"name": "Test_4"
}
]}
I need to create four column of equal width (because there is four object).If it is three than it show three column of equal width .can we do in android ?
any idea..?
how to do with grid view ?