ArrayAdapter:根据项目更改资源(Android)

时间:2017-03-12 20:17:40

标签: android listview adapter android-arrayadapter

我正在我的应用程序中创建一种消息传递活动,我正在使用自定义的ArrayAdapter来显示ListView中的项目。我想让它们向右或向左移动,具体取决于收到或发送它们的事实(如Whatsapp或任何其他消息应用程序)。我尝试了以下代码,但并不总是使用正确的资源显示它们:

public class MessagesAdapter extends ArrayAdapter<Messages>{
private LayoutInflater mInflater;
private int mResource;
private String username;

/**
 * Constructor
 *
 * @param context     The current context.
 * @param resource    The resource ID for a layout file containing a TextView to use when
 *                    instantiating views.
 * @param messages The objects to represent in the ListView.
 */
public MessagesAdapter(Context context, int resource, List<Messages> messages, String username )
{
    super( context, resource, messages );
    mResource = 0;
    mInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    this.username = username;
}

@Override
public Messages getItem(int position) {
    return super.getItem(getCount() - position - 1);
}

/**
 * {@inheritDoc}
 */
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
    Messages item = getItem( position );

    if (item.getSendername().contentEquals(username)){
        mResource = R.layout.message_right;
        Log.w("Messages", "TRUE" );

    } else {
        mResource = R.layout.message_left;
        Log.w("Messages", "FALSE" );
    }

    View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;

    TextView txtMessage = (TextView) view.findViewById( R.id.txtMessage );
    txtMessage.setText( item.getTextmess() );

    return view;
}

它实际上会在正确的时间打印消息“TRUE”和“FALSE”,但有时它会使用错误的资源显示消息。

2 个答案:

答案 0 :(得分:1)

当您检查convertView是否为null时,看起来如果为true,则会使您想要的资源膨胀,但如果不是,则只重用上一个视图,该视图可能不是具有正确资源的视图。

问题出现在这个三元行的false部分:

View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;

如果错误,则在您检查用户名时不会考虑mResource

您可能需要检查是否需要更改convertView的资源,或者甚至可以跳过检查convertView是否为空,并且每次只是为新视图充气。

答案 1 :(得分:0)

试试这个:适配器

public class MessagesAdapter extends BaseAdapter {

private List<Messages> arrayList;
private Context context;
private LayoutInflater layoutInflater;

String username;

public MessagesAdapter(Context context, List<Messages> arrayList, String username) {
    this.context = context;
    this.arrayList = arrayList;
    this.username = username;
    this.layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Messages getItem(int position) {
    return arrayList.get(position);
}

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

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if (arrayList.get(position).getSendername().contentEquals(username)){
        return 0;

    } else {
        return 1;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    switch (getItemViewType(position)) {

        case 0:
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.left, null);
                convertView.setTag(new ViewHolderLeft(convertView));
            }

            initializeViews((Messages) getItem(position), (ViewHolderLeft) convertView.getTag(), position);
            break;
        case 1:

            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.rigt, null);
                convertView.setTag(new ViewHolderRight(convertView));
            }

            initializeViews((Messages) getItem(position), (ViewHolderRight) convertView.getTag(), position);

            break;
    }
    return convertView;


}

private void initializeViews(Messages item, ViewHolder holder, int position) {
    holder.leftTv.setText(item.getLeft());

}

private void initializeViews(Messages item, ViewHolderRight holder, int position) {
    holder.rightTv.setText(item.getRight());
}

protected class ViewHolderLeft {

    private TextView leftTv;

    public ViewHolderLeft(View view) {
        leftTv = (TextView) view.findViewById(R.id.left_tv);
    }
}

protected class ViewHolderRight {

    private TextView rightTv;

    public ViewHolderRight(View view) {
        rightTv = (TextView) view.findViewById(R.id.right_tv);
    }
}


}