无法转换为java.lang.String

时间:2019-06-13 07:24:56

标签: java android

chatMessages = new ArrayList<>();
    listView = findViewById(R.id.list_of_messages);

    Message(topic_id);

    listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    listView.setAdapter(chatAdapter);



    buttonSend = findViewById(R.id.send);
    fileSend = findViewById(R.id.file_send);
    chatText = (EditText) findViewById(R.id.mychat);





    //  delete_btn = findViewById(R.id.);


    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        // View select;
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, long id) {


                    if ( view != null){

                  view.setBackgroundColor(0xFFB2DFDB);

                 }



            String value = (String) listView.getItemAtPosition(position);
            msgid = chatMessages.get(position).getMessageID();
            message = chatMessages.get(position).getMessageText();
            userid1 = chatMessages.get(position).getUserId();
            username = chatMessages.get(position).getMessageUser();
            replyuser = chatMessages.get(position).getReply_user();





            if (sno.equals(userid1)) {
                view.setBackgroundColor(0xFFB2DFDB);
               // Toast.makeText(getApplicationContext(),"Hello"+userid1+msgid,Toast.LENGTH_SHORT).show();

               // ImageView edit = (ImageView) findViewById(R.id.delete_icon);
             //   edit.setVisibility(View.VISIBLE);
             //   );
                topic.setText(getString(R.string.edit));
                myToolbar.setLogo(R.drawable.edit);


              //  view.setBackgroundColor(Color.WHITE);
                topic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        chatMessages.remove(position);
                        view.setBackgroundColor(Color.TRANSPARENT);
                        userid1 = null;
                        chatAdapter.notifyDataSetChanged();
                        chatText.setText(message);


                    }
                });
                myToolbar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        chatMessages.remove(position);
                        userid1 = null;
                        chatAdapter.notifyDataSetChanged();
                        chatText.setText(message);



                    }
                });

            } else {
                view.setBackgroundColor(0xFFB2DFDB);
               // Toast.makeText(getApplicationContext(),"Hello"+userid1+msgid,Toast.LENGTH_SHORT).show();

                topic.setText(getString(R.string.reply));
                myToolbar.setLogo(R.drawable.replay);
                // view.setBackgroundColor(Color.WHITE);
             //   reply = (ImageView) findViewById(R.id.reply_icon);
             //   reply.setVisibility(View.VISIBLE);
                topic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        replylayout = (LinearLayout) findViewById(R.id.reply_layout);
                        replylayout.setVisibility(View.VISIBLE);

                        TextView replay_name = (TextView) findViewById(R.id.reply_user);
                        TextView replay_text = (TextView) findViewById(R.id.reply_text);
                        ImageButton cancell = (ImageButton) findViewById(R.id.cancell_btn);

                        replay_name.setText(username);
                        replay_text.setText(message);


                        cancell.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                replylayout.removeAllViews();
                                startActivity(getIntent());

                            }
                        });


                    }
                });
           myToolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                replylayout = (LinearLayout) findViewById(R.id.reply_layout);
                replylayout.setVisibility(View.VISIBLE);

                TextView replay_name = (TextView) findViewById(R.id.reply_user);
                TextView replay_text = (TextView) findViewById(R.id.reply_text);
                ImageButton cancell = (ImageButton) findViewById(R.id.cancell_btn);

                replay_name.setText(username);
                replay_text.setText(message);


                cancell.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        replylayout.removeAllViews();
                        startActivity(getIntent());

                    }
                });





            }
        });




            }

            return true;

        }

   });

我要在长按列表项时在列表视图上实现setOnItemLongClickListener,然后执行其他活动,例如删除和编辑功能。长按时,则不能将错误赋予java.lang.String

3 个答案:

答案 0 :(得分:2)

以此替换您的代码

String value = (String) listView.getItemAtPosition(position);

有了这个

String value = String.valueOf(listView.getItemAtPosition(position));

答案 1 :(得分:0)

主要问题在这一行

String value = (String) listView.getItemAtPosition(position);

我绝对不确定您的列表视图中是否填充了String项,稍后在您调用的代码中

chatMessages.get(position).getMessageID()

所以您的值应该类似于

ChatMessage value = (ChatMessage) listView.getItemAtPosition(position);

在您的适配器中,您需要更改这些方法(如果尚未更改):

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

答案 2 :(得分:0)

from psd_tools import PSDImage

psd_name = "your_name"
x = 0
psd = PSDImage.open('your_file.psd')

for layer in psd:
    x+=1
    if layer.kind == "smartobject":
        image.conmpose().save(psd_name + str(x) + "png")

视图不能转换为字符串。我认为上面的代码应该是(假定列表视图中的项目内容为文本):

String value = (String) listView.getItemAtPosition(position);