列表ImageView共享时重复相同的图像

时间:2015-02-24 04:50:00

标签: android imageview android-arrayadapter repeat

我有一个listView,它应该接受共享消息和图像,其中图像放在ImageView中。此功能仅适用于第一条消息,但是一旦共享了一个图像,在该初始图像之后接收的每条消息都会成为同一图像的副本,即使已经设置了空白图像占位符,这只是一个像素的黑色png: holder.sharedSpecial.setImageResource(R.drawable.image_share_empty_holder);

以下是一个例子:

enter image description here

绿色文本框是收件人。他们从黄色文本框中收到了共享图片。然后黄色文本框只发送一条正常消息,我将另一个图像设置为普通消息的占位符:holder.sharedSpecial.setImageResource(R.drawable.image_share_empty_holder);

相同的先前共享图像优先。我使用notifyDataSetChanged()以便允许更新适配器,以便它能够识别不使用相同的图像,但无济于事。

如何重新构建此类,以便共享的图像仅显示正确的消息而不会复制到每个后续消息中?

ArrayAdapter类:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

    class ViewHolder {
        TextView countryName;
        ImageView sharedSpecial;
        LinearLayout wrapper;
    }

    private TextView countryName;
    private ImageView sharedSpecial;

    private MapView locationMap;
    private GoogleMap map;

    private List<OneComment> countries = new ArrayList<OneComment>();
    private LinearLayout wrapper;

    private JSONObject resultObject;
    private JSONObject imageObject;

    String getSharedSpecialURL = null;
    String getSharedSpecialWithLocationURL = null;

    String specialsActionURL = "http://" + Global.getIpAddress()
            + ":3000/getSharedSpecial/";

    String specialsLocationActionURL = "http://" + Global.getIpAddress()
            + ":3000/getSharedSpecialWithLocation/";

    String JSON = ".json";

    @Override
    public void add(OneComment object) {
        countries.add(object);
        super.add(object);
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public int getCount() {
        return this.countries.size();
    }

    private OneComment comment = null;

    public OneComment getItem(int index) {
        return this.countries.get(index);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.message_list_item, parent, false);

            holder = new ViewHolder();
            holder.wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
            holder.countryName = (TextView) row.findViewById(R.id.comment);
            holder.sharedSpecial = (ImageView) row.findViewById(R.id.sharedSpecial);

            // Store the ViewHolder as a tag.
            row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }

        Log.v("COMMENTING","Comment is " + countries.get(position).comment);

        //OneComment comment = getItem(position);
        holder.countryName.setText(countries.get(position).comment);

        // Initiating Volley
        final RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();

        // Check if message has campaign or campaign/location attached
        if (countries.get(position).campaign_id == "0" && countries.get(position).location_id == "0") {

            holder.sharedSpecial.setImageResource(R.drawable.image_share_empty_holder);

            Log.v("TESTING", "It is working");

        } else if (countries.get(position).campaign_id != "0" && countries.get(position).location_id != "0") {

            // If both were shared
            getSharedSpecialWithLocationURL = specialsLocationActionURL + countries.get(position).campaign_id + "/" + countries.get(position).location_id + JSON;

            // Test Campaign id = 41
            //      Location id = 104

            // GET JSON data and parse
            JsonObjectRequest getCampaignLocationData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialWithLocationURL, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            // Parse the JSON:
                            try {
                                resultObject = response.getJSONObject("shared");

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

                            // Get and set image
                            Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(holder.sharedSpecial);

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("Error.Response", error.toString());
                        }
                    }
            );

            requestQueue.add(getCampaignLocationData);

        } else if (countries.get(position).campaign_id != "0" && countries.get(position).location_id == "0") {

            // Just the campaign is shared
            getSharedSpecialURL = specialsActionURL + countries.get(position).campaign_id + JSON;

            // Test Campaign id = 41

            // GET JSON data and parse

            JsonObjectRequest getCampaignData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialURL, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            // Parse the JSON:
                            try {
                                resultObject = response.getJSONObject("shared");

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

                            // Get and set image
                            Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(holder.sharedSpecial);

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("Error.Response", error.toString());
                        }
                    }
            );

            requestQueue.add(getCampaignData);

            // Location set to empty
        }

        // If left is true, then yellow, if not then set to green bubble
        holder.countryName.setBackgroundResource(countries.get(position).left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
        holder.wrapper.setGravity(countries.get(position).left ? Gravity.LEFT : Gravity.RIGHT);

        return row;
    }

}

仅发送普通邮件但可以接收图像邮件并设置为适配器的邮件类:

public class GroupMessaging extends Activity {

    private static final int MESSAGE_CANNOT_BE_SENT = 0;
    public String username;
    public String groupname;

    private Button sendMessageButton;
    private Manager imService;
    private InfoOfGroup group = new InfoOfGroup();
    private InfoOfGroupMessage groupMsg = new InfoOfGroupMessage();
    private StorageManipulater localstoragehandler;
    private Cursor dbCursor;

    private com.example.feastapp.ChatBoxUi.DiscussArrayAdapter adapter;
    private ListView lv;
    private EditText editText1;

    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
            imService = ((MessagingService.IMBinder) service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {
            imService = null;
            Toast.makeText(GroupMessaging.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.message_activity);

        lv = (ListView) findViewById(R.id.listView1);

        adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.message_list_item);

        lv.setAdapter(adapter);

        editText1 = (EditText) findViewById(R.id.editText1);

        sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

        Bundle extras = this.getIntent().getExtras();

        group.userName = extras.getString(InfoOfGroupMessage.FROM_USER);
        group.groupName = extras.getString(InfoOfGroup.GROUPNAME);
        group.groupId = extras.getString(InfoOfGroup.GROUPID);
        String msg = extras.getString(InfoOfGroupMessage.GROUP_MESSAGE_TEXT);

        setTitle("Group: " + group.groupName);

        // Retrieve the information
        localstoragehandler = new StorageManipulater(this);
        dbCursor = localstoragehandler.groupGet(group.groupName);


        if (dbCursor.getCount() > 0) {
            // Probably where the magic happens, and keeps pulling the same
            // thing
            int noOfScorer = 0;
            dbCursor.moveToFirst();

            while ((!dbCursor.isAfterLast())
                    && noOfScorer < dbCursor.getCount()) {
                noOfScorer++;

            }
        }
        localstoragehandler.close();


        if (msg != null) {
            // Then friends username and message, not equal to null, recieved
            adapter.add(new OneComment(true, group.groupId + ": " + msg, "0", "0"));
            adapter.notifyDataSetChanged();

            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .cancel((group.groupId + msg).hashCode());

        }

        // The send button
        sendMessageButton.setOnClickListener(new OnClickListener() {
            CharSequence message;
            Handler handler = new Handler();

            public void onClick(View arg0) {

                message = editText1.getText();
                if (message.length() > 0) {

                    // When general texting, the campaign and location will always be "0"
                    // Only through specials sharing is the user permitted to change the campaign and location to another value
                    adapter.add(new OneComment(false, imService.getUsername() + ": " + message.toString(), "0", "0"));
                    adapter.notifyDataSetChanged();

                    localstoragehandler.groupInsert(imService.getUsername(), group.groupName,
                            group.groupId, message.toString(), "0", "0");

                    // as msg sent, will blank out the text box so can write in
                    // again

                    editText1.setText("");
                    Thread thread = new Thread() {
                        public void run() {
                            try {

                                // JUST PUTTING "0" AS A PLACEHOLDER FOR CAMPAIGN AND LOCATION
                                // IN FUTURE WILL ACTUALLY ALLOW USER TO SHARE CAMPAIGNS
                                if (imService.sendGroupMessage(group.groupId,
                                        group.groupName, message.toString(), "0", "0") == null) {

                                    handler.post(new Runnable() {

                                        public void run() {

                                            Toast.makeText(
                                                    getApplicationContext(),
                                                    R.string.message_cannot_be_sent,
                                                    Toast.LENGTH_LONG).show();

                                            // showDialog(MESSAGE_CANNOT_BE_SENT);
                                        }

                                    });
                                }
                            } catch (UnsupportedEncodingException e) {
                                Toast.makeText(getApplicationContext(),
                                        R.string.message_cannot_be_sent,
                                        Toast.LENGTH_LONG).show();

                                e.printStackTrace();
                            }
                        }
                    };
                    thread.start();

                }

            }
        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        int message = -1;
        switch (id) {
            case MESSAGE_CANNOT_BE_SENT:
                message = R.string.message_cannot_be_sent;
                break;
        }

        if (message == -1) {
            return null;
        } else {
            return new AlertDialog.Builder(GroupMessaging.this)
                    .setMessage(message)
                    .setPositiveButton(R.string.OK,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                                    int whichButton) {
                                    /* User clicked OK so do some stuff */
                                }
                            }).create();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(groupMessageReceiver);
        unbindService(mConnection);

        ControllerOfGroup.setActiveGroup(null);

    }

    @Override
    protected void onResume() {
        super.onResume();
        bindService(new Intent(GroupMessaging.this, MessagingService.class),
                mConnection, Context.BIND_AUTO_CREATE);

        IntentFilter i = new IntentFilter();
        i.addAction(MessagingService.TAKE_GROUP_MESSAGE);

        registerReceiver(groupMessageReceiver, i);

        ControllerOfGroup.setActiveGroup(group.groupName);

    }

    // For receiving messages form other users...
    public class GroupMessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extra = intent.getExtras();

            //Log.i("GroupMessaging Receiver ", "received group message");

            String username = extra.getString(InfoOfGroupMessage.FROM_USER);
            String groupRId = extra.getString(InfoOfGroupMessage.TO_GROUP_ID);
            String message = extra
                    .getString(InfoOfGroupMessage.GROUP_MESSAGE_TEXT);

            // NEED TO PLACE INTO THE MESSAGE VIEW!!
            String received_campaign_id = extra.getString(InfoOfGroupMessage.CAMPAIGN_SHARED);
            String received_location_id = extra.getString(InfoOfGroupMessage.LOCATION_SHARED);

            // NEED TO INTEGRATE THIS INTO LOGIC ABOVE, SO IT MAKES SENSE
            if (username != null && message != null) {
                if (group.groupId.equals(groupRId)) {

                    adapter.add(new OneComment(true, username + ": " + message, received_campaign_id, received_location_id));

                    localstoragehandler
                            .groupInsert(username, groupname, groupRId, message, received_campaign_id, received_location_id);

                    Toast.makeText(getApplicationContext(), "received_campaign: " + received_campaign_id +
                            " received_location:" + received_location_id, Toast.LENGTH_LONG).show();

                    received_campaign_id = "0";
                    received_location_id = "0";

                } else {
                    if (message.length() > 15) {
                        message = message.substring(0, 15);
                    }
                    Toast.makeText(GroupMessaging.this,
                            username + " says '" + message + "'",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    ;

    // Build receiver object to accept messages
    public GroupMessageReceiver groupMessageReceiver = new GroupMessageReceiver();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (localstoragehandler != null) {
            localstoragehandler.close();
        }
        if (dbCursor != null) {
            dbCursor.close();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我已经完成了你的代码,这是listview的常见问题,图像开始重复。在你的情况下,我认为你已经将图像分配给Imageview,如果是,否则条件,但如果条件都不满足,则使用前一个图像。

我建议调试getview方法并在setImageResource上放置断点。我使用volley进行这些图像加载,它有一个名为defaultImage的方法,这样如果没有url,那么图像将获得默认值。所以添加默认情况并查看它是否有效。

如果上述任何一点不明确,请随时发表评论。