recyclerView setText位置

时间:2018-12-05 13:53:42

标签: android text android-recyclerview

我正在开发一个android项目,其中我在recyclerView中设置了cardView,在这里我需要能够设置所选cardView的setText。

当前,这有效:

添加CardView: 按3点图像(在卡片视图上)->在菜单上(添加卡片视图)->(显示警告对话框),输入文字->添加新的卡片视图,并写入您的标题。与位置配合。

删除CardView: 按3点图像(在卡片视图上)->在菜单上(删除卡片视图)->选定的卡片视图被删除。与位置配合。

更改CardView的文本:该操作无效。我需要更改所选Cardview上的文本的帮助。我可以设置一个警报对话框来输入文本,但是如何更改所选Cardview的文本?

重要信息:changeLokation(在代码中称为开关)在这里,我想更改cardView的文本。

菜单翻译:

  • 更改文字=导航名称
  • 将传感器添加到Location =TilføjSensor til Lokation(目前不可用)。

  • 添加CardView =添加CardView

  • 删除CardView =删除CardView

我感谢所有帮助。预先感谢!

enter image description here

代码如下:

public class lokationRecyclerAdapter extends RecyclerView.Adapter<lokationRecyclerAdapter.RecyclerViewHolder> {

List<String> cardList;
String test;

public lokationRecyclerAdapter(List<String> cardList) {
    this.cardList = cardList;
    notifyDataSetChanged();
}

@Override
public RecyclerViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.lokationcardview, viewGroup, false );
    return new RecyclerViewHolder( view );
}

public void deleteItem(int position) {
    cardList.remove( position );
    notifyItemRemoved( position );
    notifyItemRangeChanged( position, cardList.size() );

}

@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {
    recyclerViewHolder.cardView.setBackgroundResource( R.drawable.ic_circleshape );
    recyclerViewHolder.lokationsName.setText( cardList.get( position ) );
}

@Override
public int getItemCount() {
    return cardList.size();
}

class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public int nummer;
    private CardView cardView;
    private Button menuButton;
    private TextView lokationsName;

    public void alertDialog(final View itemView) {

        new AlertDialog.Builder( itemView.getContext() );

        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder( itemView.getContext() );
        builder.setCancelable( true );
        builder.setTitle( "Tilføj eller ændre navnet på lokation" );
        builder.setMessage( "Hold styr på dine sensorer ved at oprette lokationer i form af eksempelvis lokaler." );

        final EditText input = new EditText( itemView.getContext() );
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT );
        input.setMaxLines( 1 );
        input.setInputType( InputType.TYPE_CLASS_TEXT );
        input.setLayoutParams( lp );
        builder.setView( input );

        builder.setPositiveButton( "Ja", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {    
                test = input.getText().toString();
                cardList.add( test );
                //lokationsName.setText( test );
                System.out.println( "TEST: " + cardList );
            }
        } );

        builder.setNegativeButton( "Nej", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        } );
        android.support.v7.app.AlertDialog dialog = builder.create();
        dialog.show();
    }

    public RecyclerViewHolder(final View itemView) {
        super( itemView );

        cardView = (CardView) itemView.findViewById( R.id.lokationcard_view );
        menuButton = (Button) itemView.findViewById( R.id.menuButton );
        lokationsName = (TextView) itemView.findViewById( R.id.lokationsName );

        itemView.setOnClickListener( this );

        menuButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final PopupMenu popupMenu = new PopupMenu( itemView.getContext(), menuButton );
                popupMenu.getMenuInflater().inflate( R.menu.lokationpopmenu, popupMenu.getMenu() );

                popupMenu.setOnMenuItemClickListener( new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {

                        switch (item.getItemId()) {

                            case R.id.changeLokation:
                                System.out.println( "GET ADAPTER POSITION: " + getAdapterPosition() + " CARD LIST: " + cardList );
                                alertDialog( itemView );
                                break;

                            case R.id.addCardView:
                                String lokation = lokationsName.getText().toString();

                                /*if (lokation.equals( "TILFØJ LOKATION" )) {
                                    Toast.makeText( itemView.getContext(), "Du skal ændre navnet på lokation først!", Toast.LENGTH_LONG ).show();
                                } else {*/
                                System.out.println( "GET ADAPTER POSITION: " + getAdapterPosition() );
                                //addItem(getAdapterPosition());
                                alertDialog( itemView );
                                System.out.println( "LOKATIONS NAME: " + lokation + " CARDLIST: " + cardList );
                                // }
                                break;

                            case R.id.deleteCardView:
                                if (cardList.size() > 1) {
                                    deleteItem( getAdapterPosition() );
                                } else {
                                    Toast.makeText( itemView.getContext(), "Du kan ikke slette den sidste CardView", Toast.LENGTH_LONG ).show();
                                }
                                break;

                        }
                        return true;
                    }
                } );
                popupMenu.show();
            }
        } );

    }

    }

}

3 个答案:

答案 0 :(得分:1)

从您的代码中,我可以看到您正在尝试直接从对话框文本中更新lokationsName.setText( test );,我不建议这样做,您应该将值更新到列表中并调用{{1} }。

示例:

在此处传递商品位置,您将需要此信息来更新商品

notifyitemchanged

然后在您的public void alertDialog(final View itemView,int position) { new AlertDialog.Builder( itemView.getContext() ); android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder( itemView.getContext() ); builder.setCancelable( true ); builder.setTitle( "Tilføj eller ændre navnet på lokation" ); builder.setMessage( "Hold styr på dine sensorer ved at oprette lokationer i form af eksempelvis lokaler." ); final EditText input = new EditText( itemView.getContext() ); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ); input.setMaxLines( 1 ); input.setInputType( InputType.TYPE_CLASS_TEXT ); input.setLayoutParams( lp ); builder.setView( input ); builder.setPositiveButton( "Ja", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { test = input.getText().toString(); //change the code here cardList.set(position,test); notifyItemChanged(position); } } ); builder.setNegativeButton( "Nej", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } } ); android.support.v7.app.AlertDialog dialog = builder.create(); dialog.show(); } 中更改以下内容:

RecyclerViewHolder

如果有任何疑问,请在评论中告知我。

答案 1 :(得分:0)

我认为这是因为您正在异步执行setText,所以您再也无法访问视图。

我会尝试在您的recyclerView myRecyclerView.findViewHolderForAdapterPosition(pos);上执行此操作,然后使用正确的viewHolder更改文本。

请注意,如果视图已回收,它将返回null

答案 2 :(得分:0)

从根本上说,对RecyclerView的所有更改都应通过首先更改支持Adapter的数据,然后然后通知更改的完成。最简单的形式是,如果您有一个字符串数组,并且想要更改第三个字符串,则代码将如下所示:

array[2] = "new text";
adapter.notifyItemChanged(2);

这个基本概念是正确的,无论您的数据如何变化。如果要插入或删除值,或者甚至重置整个数据集,请先修改数据,然后通知适配器(使用notifyItemInserted()notifyItemRemoved()notifyDataSetChanged())。

对于您而言,应该将视图的适配器位置传递给您,而不是将视图传递给alertDialog()方法:

case R.id.changeLokation:
    alertDialog(getAdapterPosition());
    break;

然后,一旦用户输入了文本并单击“确定”按钮,您就可以将新文本替换为列表:

cardList.remove(position); // this is the argument passed into alertDialog()
cardList.add(position, newText);

最后,您将通知适配器:

adapter.notifyItemChanged(position);