我正在解析json数据并使用BaseAdapter在自定义ListView中显示。我在解析数据时没有问题。也没有任何问题将数据设置为ModelClass.java(setters和getters类),只有问题是如何从ModelClass.java获取数据。请帮忙。
这是我的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>
答案 0 :(得分:0)
这是你要求的吗?
//Inside your Model class:
public String getTitle(){
return this.title;
}
public String setTitle(String givenTitle){
this.title = givenTitle;
}
public String getBody(){
return this.body;
}
public String setTitle(String givenBody){
this.body = givenBody;
}
答案 1 :(得分:0)
有点难以解决确切的问题,因为你问的是如何从setter和getters类中获取值,但你的ModelClass没有getter或setter。
你可以将getter放入你的ModelClass中,但如果它在同一个包中,你可能在技术上不需要它们。
你应该看看这个教程。这对我帮助很大。 http://www.vogella.com/tutorials/AndroidListView/article.html
答案 2 :(得分:0)
正如其他人提到的那样,你需要在模型类中加入getter和setter。在将值放入列表并格式化列表项方面,您需要一个自定义列表适配器。然后创建要放入列表适配器的项目列表..然后显示列表。以下是一个例子。
enter code here
public class ExampleListAdapter extends ArrayAdapter<ExampleModel> {
private Context context;
private ArrayList<ExampleModel> items;
public ExampleListAdapter(Context context, ArrayList<ExampleModel> items) {
super(context, R.id.result_list_item_direction_icon, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = null;
View rowView = convertView;
//Get rowView from inflater
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.id.yourcustom_layout, parent, false);
// Get reference ids from the rowView
//example
TextView item = (TextView) rowView.findViewById(R.id.itemID);
// Set the values for items in custom list view
item.setText(items.get(position).yourGetter());
return rowView;
}
}
然后在课程中你要使用它......
enter code here
ArrayList<t> yourList = new ArrayList<t>();
yourList = getValuesFromDBOROtherSource;//make sure the return value is list!
yourCustomAdapter = new ExampleListAdapter(getActivity(), yourList);
setListAdapter(yourCustomAdapter);