I am parsing json data and displaying in custom ListView using BaseAdapter. I have no problem in parsing the data. Also don't have any problem setting the data to ModelClass.java (setters and getters class) only question is how to get the data from ModelClass.java. Please help.
This is my MainActivity.java
public class MainActivity extends ActionBarActivity {
// url to make request(to get the latest 20 feeds).
private static String url = "http;.....";
// JSON Node names
private static final String TAG_BODY = "body";
private static final String TAG_CREATED_AT = "created_at";
private static final String TAG_DATE_TIME = "date_time";
private static final String TAG_DEPARTEMENT = "department";
private static final String TAG_ID = "id";
private static final String TAG_INCLUDE = "include";
private static final String TAG_MEDI_TYPE = "mediaType";
private static final String TAG_PRIORITY = "priority";
private static final String TAG_TITLE = "title";
private static final String TAG_UPDATED_AT = "updated_at";
private static final String TAG_USER_ID = "user_id";
ProgressDialog progressDialog;
ListView listViewFeeds;
// String mTitle, mBody;
List<ModelClass> model = new ArrayList<ModelClass>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* To display the feed */
listViewFeeds = (ListView) findViewById(R.id.listViewFeeds);
listViewFeeds.setAdapter(new NewAdapter(this));
Feeds feeds = new Feeds();
feeds.execute();
}
public class Feeds extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Loading ...");
progressDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Accept", "application/json");
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
return null;
}
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
String jsonData = stringBuilder.toString();
// Displaying json data in logcat.
Log.d("Latest 20 feeds", jsonData);
JSONArray array = new JSONArray(jsonData);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String mTitle = object.getString("title");
String mBody = object.getString("body");
System.out.println("Titlte: " + mTitle);
System.out.println("Body: " + mBody);
// Setting the data to ModelClass
ModelClass mc = new ModelClass(mTitle, mBody);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
super.onPostExecute(result);
}
}
}
class NewAdapter extends BaseAdapter {
ArrayList<ModelClass> list;
Context context;
NewAdapter(Context c) {
context = c;
}
@Override
public int getCount() {
return 0;
// return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row_feed_view, parent,
false);
TextView title = (TextView) row.findViewById(R.id.textViewTitleFeeds);
TextView body = (TextView) row.findViewById(R.id.textViewBody);
ModelClass temp = list.get(position);
title.setText(temp.title);
body.setText(temp.body);
return row;
}
}
This is my ModelClass.java
public class ModelClass {
String title;
String body;
ModelClass(String title, String body) {
this.title = title;
this.body = body;
}
}
activity_main.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewFeeds"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
single_row_feed_view.xml file(row appears in ListView)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewTitleFeeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/viewColor"
android:ellipsize="end"
android:maxLines="2"
android:text="TextView"
android:textSize="12dp" />
<TextView
android:id="@+id/textViewBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewTitleFeeds"
android:layout_marginTop="3dp"
android:ellipsize="end"
android:maxLines="2"
android:text="TextView"
android:textSize="8dp" />
</RelativeLayout>
Its a bit hard to work out the exact question because you are asking how to get the value from setters and getters class but your ModelClass has no getters or setters.
You can put getters into your ModelClass, but you may not technically need them if its in the same package.
You should look at this tutorial. It helped me a lot. http://www.vogella.com/tutorials/AndroidListView/article.html