目前我正在努力使用不会显示我的JSON数据的Android应用程序(Android Studio),但运行并加载它没有问题。我通过在代码中的不同位置注销硬编码文本来控制它。我按照Wingnity的说明进行了操作。
在我的Actors.java类中,我拥有所有必需的getter和setter。
MyActivity.java(没有遗漏的导入语句)
public class MyActivity extends Activity {
private ImageView mImageView;
ListView listView;
ArrayList<Actors> actorsList;
ActorsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
listView = (ListView)findViewById(R.id.list);
actorsList = new ArrayList<Actors>();
adapter = new ActorsAdapter(this, R.layout.row, actorsList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();
}
});
ActorsAsyncTask aat = new ActorsAsyncTask();
aat.execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
}
class ActorsAsyncTask extends AsyncTask<String, Double, ArrayList<Actors>> {
ProgressDialog dialog;
@Override
protected ArrayList<Actors> doInBackground(String... params) {
ArrayList<Actors> result = new ArrayList<Actors>();
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200) {
Log.d("Status", status + "");
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("actors");
Log.d("Array", jsonArray.toString());
for(int i = 0; i < jsonArray.length(); i++) {
try{
Actors actor = new Actors();
JSONObject jsonRealObject = jsonArray.getJSONObject(i);
actor.setName(jsonRealObject.getString("name"));
actor.setDescription(jsonRealObject.getString("description"));
actor.setDob(jsonRealObject.getString("dob"));
actor.setCountry(jsonRealObject.getString("country"));
actor.setHeight(jsonRealObject.getString("height"));
actor.setSpouse(jsonRealObject.getString("spouse"));
actor.setChildren(jsonRealObject.getString("children"));
actor.setImage(jsonRealObject.getString("image"));
result.add(actor);
} catch(Exception e){
continue;
}
}
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<Actors> result) {
if(result != null) {
actorsList = result;
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ActorsAdapter.java
public class ActorsAdapter extends BaseAdapter {
ArrayList<Actors> ArrayListActors;
int Resource;
Activity context;
public ActorsAdapter(Activity context, int resource, ArrayList<Actors> objects) {
ArrayListActors = objects;
Resource = resource;
this.context = context;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(Resource, parent, false);
ImageView iv = (ImageView)rowView.findViewById(R.id.ivImage);
TextView tv = (TextView)rowView.findViewById(R.id.tvName);
TextView tvD = (TextView)rowView.findViewById(R.id.tvDescriptionn);
TextView tvDOB = (TextView)rowView.findViewById(R.id.tvDateOfBirth);
TextView tvC = (TextView)rowView.findViewById(R.id.tvCountry);
TextView tvH = (TextView)rowView.findViewById(R.id.tvHeight);
TextView tvS = (TextView)rowView.findViewById(R.id.tvSpouse);
TextView tvCh = (TextView)rowView.findViewById(R.id.tvChildren);
//new DownloadImageTask(iv).execute(ArrayListActors.get(position).getImage());
tv.setText(ArrayListActors.get(position).getName());
tvD.setText(ArrayListActors.get(position).getDescription());
tvDOB.setText("Birthday: " + ArrayListActors.get(position).getDob());
tvC.setText(ArrayListActors.get(position).getCountry());
tvH.setText("Height: " + ArrayListActors.get(position).getHeight());
tvS.setText("Spouse: " + ArrayListActors.get(position).getSpouse());
tvCh.setText("Children: " + ArrayListActors.get(position).getChildren());
return rowView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
@Override
protected Bitmap doInBackground(String... urls) {
String urlDisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dk.markflarup.jsongettime" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_my.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F1F1F1"
tools:context=".MyActivity" >
<!-- A plain TextView for trouble shooting/
Controlling my view at least will show content-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HEJ HEJ HEJ"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/row" >
</ListView>
</LinearLayout>
row.xml(我试图删除row.xml中的所有硬编码文本,导致没有不同的结果)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tom Cruise"
android:textColor="#166CED"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDateOfBirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#D64530"
android:text="Date of Birth: July 3, 1962" />
<TextView
android:id="@+id/tvHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Height: 1.80 m"
android:textColor="#D64530"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#D64530"
android:text="United States" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tvDescriptionn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#009A57"
android:text="Description" />
<TextView
android:id="@+id/tvSpouse"
android:layout_width="wrap_content" android:textColor="#166CED"
android:layout_height="wrap_content"
android:text="Spouse: Katie Holmes" />
<TextView
android:id="@+id/tvChildren"
android:layout_width="wrap_content" android:textColor="#166CED"
android:layout_height="wrap_content"
android:text="Children: Suri Cruise, Isabella Jane Cruise, Connor Cruise" />
</LinearLayout>
答案 0 :(得分:1)
您在适配器中返回0。这意味着您的适配器将显示零行。
你应该返回ArrayListActors
的大小。而是将其改为:
public int getCount() {
// wrong
// return 0;
return ArrayListActors.size();
}
此外,您的适配器不会从getItem()
方法中的数据中返回任何项目。这意味着,一旦您修复getCount()
方法,适配器仍然无法查询您的ArrayListActors
真实数据。
这应该从ArrayListActors
返回正确的项目。
public Object getItem(int i) {
// wrong
// return null;
return ArrayListActors.get(i);
}
(我将在此留下此信息,因为它可能会帮助未来的读者处理适配器。)
在调用ArrayAdapter
之前,您需要将新的Actors列表设置到notifyDataSetChanged()
。
类似的东西:
protected void onPostExecute(ArrayList<Actors> result) {
if(result != null) {
/* CHANGE IN NEXT LINE */
adaptor.ArrayListActors = result;
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
将result
设置为actorslist
会更改actorslist
指向的对象。这意味着适配器内的引用不再指向新的actorslist
,而是指向旧版本。
因此显示屏仍会显示旧数据,这是适配器所知道的。
Nightware在评论中提到的另一种选择是保留相同的引用,但清除并重新填充actorsList
。
这样适配器仍然引用相同的actorsList
对象,因此当您更改该对象的内容时,适配器就能够看到新信息。
protected void onPostExecute(ArrayList<Actors> result) {
if(result != null) {
actorsList.clear();
actorsList.addAll(result);
adapter.notifyDataSetChanged();
} else {
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
根据您在应用其他部分对actorsList
的处理方式,这可能是更好的方法。我个人不喜欢在我使用它之后让我的数据在适配器外部浮动,这就是为什么我会选择我的第一种方法,而不是打扰actorsList
字段。
答案 1 :(得分:1)
理查德的回答是你所缺少的,我创建了项目并发现这是问题,请更新适配器类中的那些:
@Override
public int getCount()
{
return ArrayListActors.size();
}
@Override
public Object getItem(int i)
{
return ArrayListActors.get(i);
}
这在你的onPostExecute方法中:
if (result != null)
{
actorsList.clear();
actorsList.addAll(result);
adapter.notifyDataSetChanged();
}
如果这对您有用,请将Richard的答案标记为正确。