我正在解析json數據並使用BaseAdapter在自定義ListView中顯示。我有在解析數據時沒有問題。也沒有任何問題設置數據到ModelClass.java(setters和getters類)只有問題是如何從ModelClass.java獲取數據。請幫忙。無法理解如何從setters和getters類中獲取值
這是我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;
}
}
這是我ModelClass.java
public class ModelClass {
String title;
String body;
ModelClass(String title, String body) {
this.title = title;
this.body = body;
}
}
activity_main.xml中文件
<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文件(行出現的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>
三江源,但這不是我的問題。如何在列表中顯示? – Shiva
如果你正在嘗試做我認爲你正在嘗試做的事,那麼你將不僅需要一個完整的Model類(你現在不是這個時候),而且還需要一個Adapter類。而且我知道這不是我的業務,但我認爲你應該在弄髒手之前閱讀它。你可以從這裏開始:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/這是一個很好的教程,它幫助我實現你的要求。 – Selz