目前,我正在为一个Android应用程序(Android Studio)苦苦挣扎,该应用程序不会显示我的JSON数据,但是可以正常运行和加载它。我通过在代码中的不同位置注销硬编码文本来控制此操作。我已经按照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。这意味着您的适配器将显示零行。

您应该返回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之前将新的Actor列表设置到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字段的原因。

09-04 04:43