我想将包含来自后台服务的数据的自定义矢量对象传递给活动,以便更新该活动的UI。 我无法通过意图传递我的数据。请帮助我,我需要做什么..
以下是我正在使用的代码段。在下面的代码中,我想将文章列表传递给一个活动。
Vector<RowData> getArticleLog() throws JSONException{
Vector<RowData> articleList = new Vector<RowData>();
RowData rd;
InputStream is = null;
String result = null;
JSONArray jarray = new JSONArray();
//Add data to be send.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("article_title", "Flying Horse"));
try {
HttpClient httpclient = new DefaultHttpClient();
// for local server xampp
HttpPost httppost = new HttpPost("http://10.0.2.2/groupbook/return_article_log.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
} catch (Exception e) {
Log.e(TAG, "json error : " + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e(TAG, "Error converting result "+e.toString());
}
try {
jarray = new JSONArray(result);
} catch (JSONException e) {
e.printStackTrace();
}
//return jArray in the form of Array list;
JSONObject rowElement = null;
int viewers = 0;
int votes = 0;
for (int i = 0; i < jarray.length(); i++) {
rowElement = jarray.getJSONObject(i);
viewers = rowElement.getInt("viewers");
votes = rowElement.getInt("votes");
rd = new RowData(i,
rowElement.getString("article_title"),
Integer.toString(i),
rowElement.getString("last_post_by"),
"2 hours",
rowElement.getString("catagory_tag"),
Integer.toString(viewers),
rowElement.getString("privacy_tag"),
Integer.toString(votes),
rowElement.getString("catagory_title"));
articleList.add(rd);
}
return articleList;
}
RowData类如下:
public class RowData {
protected int mId;
public String articleTitle;
public String articleCounter;
public String lastPostBy;
public String updatedTime;
public String catagoryTag;
public String viewingNow;
public String privacyTag;
public String votes;
public String catagoryTitle;
public RowData(int mId, String articleTitle, String articleCounter,
String lastPostBy, String updatedTime, String catagoryTag,
String viewingNow, String privacyTag, String votes,
String catagoryTitle) {
this.mId = mId;
this.articleTitle = articleTitle;
this.articleCounter = articleCounter;
this.lastPostBy = lastPostBy;
this.updatedTime = updatedTime;
this.catagoryTag = catagoryTag;
this.viewingNow = viewingNow;
this.privacyTag = privacyTag;
this.votes = votes;
this.catagoryTitle = catagoryTitle;
}
}
以下方式是正确的吗?
private void DisplayingInfo() throws JSONException{
Log.d(TAG, "entered DisplayLoggingInfo");
intent.putExtra("articleLog", getArticleLog());
sendBroadcast(intent);
}
答案 0 :(得分:2)
您可以做的一件事是制作自定义类工具Serializable
(或Parcelable
),将带有putExtra()
方法的可序列化对象发送到活动并使用getSerializableExtra()
在你的活动上得到它。
编辑1: 一个简单的例子:
在您的自定义类中:
import java.io.Serializable;
@SuppressWarnings("serial")
public class YourCustomVectorClass implements Serializable {
// ...
}
在您要开始活动的服务中:
Intent intent = new Intent(yourContext, yourActivity.class);
intent.putExtra("theNameOfTheObject", yourObject);
startActivity(intent);
在您的活动中:
YourCustomVectorClass yourVector = (YourCustomVectorClass) getIntent().getSerializableExtra("theNameOfTheObject");
编辑2 :再次阅读该问题后,我意识到您正在将Vector
个RowData
个对象传递给您的活动。
由于Java Vector class
实现Serializable
,我认为您不应该做任何事情,只需使用putExtra()
将该向量传递给活动,并在活动上使用getSerializableExtra()
获取它。