5

我正在Listview使用 json 解析器显示图像和数据,

但是,当我单击其中一个列表项图像时,下一个活动(即详细活动)中未显示。

我正在使用以下代码来显示图像。任何人都可以引导我走上正确的道路吗?任何帮助都将不胜感激。

// Launching new screen on Selecting Single ListItem
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String content = ((TextView) view.findViewById(R.id.content)).getText().toString();
             // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(URL);
             try {
                 posts = json.getJSONArray(KEY_POSTS);

    // looping through all song nodes <song>
            for(int i = 0; i < posts.length(); i++){
                JSONObject c = posts.getJSONObject(i);
                // Storing each json item in variable

                String url = null;
                String slug = null;
                try {
                JSONArray atta = c.getJSONArray("attachments");
                for(int j = 0; j < atta.length(); j++){
                    JSONObject d = atta.getJSONObject(j);

                    slug = d.getString(KEY_SLUG);

                    JSONObject images = d.getJSONObject(KEY_IMAGES);

                    JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                    url = thumbnail.getString(KEY_URL);

                }
                } catch (Exception e) {
                    e.printStackTrace();

                }



            // Starting new intent
            Intent in = new Intent(getApplicationContext(),SampleDesp.class);
            in.putExtra(KEY_TITLE, title);
            in.putExtra(KEY_DATE, date);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_CONTENT, content);
            in.putExtra(KEY_URL, url);      
            startActivity(in);

        }


             }catch (JSONException e) {
                    e.printStackTrace();

                    }
        }

    });   

}

}

Singlemenuitem.java中

 // getting intent data
        Intent in = getIntent();
        final String url1 = in.getStringExtra(KEY_URL);

        ImageView imgv = (ImageView) findViewById(R.id.imgdesc);
        ImageLoader imageLoader = new ImageLoader(getApplicationContext());
        imageLoader.DisplayImage(url1, imgv);





        // Get JSON values from previous intent
        final String title = in.getStringExtra(TAG_TITLE);
        String date = in.getStringExtra(TAG_DATE);
        String name = in.getStringExtra(TAG_NAME);
        final String content = in.getStringExtra(TAG_CONTENT);

        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblCost = (TextView) findViewById(R.id.email_label);
        TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
        TextView lblCont = (TextView) findViewById(R.id.content_label);

        lblName.setText(title);
        lblCost.setText(date);
        lblDesc.setText(name);
        lblCont.setText(content);
4

5 回答 5

1

谢谢大家的帮助和支持,只需在 MainActivity 类中添加以下代码

    // Launching new screen on Selecting Single ListItem
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            HashMap<String, String> map = songsList.get(position);

            Intent in = new Intent(MainActivity.this, SampleDesp.class);
            in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
            in.putExtra(KEY_DATE, map.get(KEY_DATE));                         
            in.putExtra(KEY_NAME, map.get(KEY_NAME));
            in.putExtra(KEY_CONTENT, map.get(KEY_CONTENT));
            in.putExtra(KEY_URL, map.get(KEY_URL));

            startActivity(in);
        }
于 2013-01-24T04:14:51.583 回答
0

首先尝试在这里初始化 url

 String url = null;// here
  in.putExtra(KEY_URL, url);  

由于这个空值,您在下一个活动中没有得到任何东西

于 2013-01-23T10:57:38.600 回答
0

我可能不确定,更改为 BitMap 并将 tat BitMap 值传递给另一个活动

于 2013-01-23T11:10:43.353 回答
0

不确定您想从哪里获取网址,但这无济于事:-

字符串 url = null;

于 2013-01-23T10:57:30.883 回答
0

在这里,您可以使用以下代码将图像从列表视图加载到下一个活动:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(getApplicationContext(), DetailActivity.class);8
                    String pic =array.get(position).getImage();
                    intent.putExtra("Photo", pic);

                    startActivity(intent);
                }

            });

这里 DetailActivity.java 类:

public class DetailActivity extends AppCompatActivity {

    ImageLoader imageLoader=AppController.getmInstance().getmImageLoader();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        NetworkImageView imageView= (NetworkImageView) findViewById(R.id.image_view_detail);
        imageView.setImageUrl(getIntent().getExtras().getString("Photo"),imageLoader);
    }
}

谢谢

于 2017-02-13T17:37:37.847 回答