0

コメント ボックスを作成するカスタム baseadapter があります。データを追加するまで、すべてがうまく機能します。データを追加しようとすると、以前のデータが削除され、新しいデータが追加されます。すべてのデータを保持するようにするにはどうすればよいですか? 私の Add メソッドは間違っていますか? これが私のベースアダプターです。

class CreateCommentLists extends BaseAdapter{
  Context ctx_invitation;
  String[] listComments;
  String[] listNumbers;
  String[] listUsernames;

  public CreateCommentLists(String[] comments, String[] usernames, String[] numbers, DashboardActivity context)
  {
    super();
    ctx_invitation = context;
    listComments = comments;
    listNumbers = usernames;
    listUsernames = numbers;
  }

  @Override
  public int getCount() {
    if(null == listComments)
    {
      return 0;
    }   

    // TODO Auto-generated method stub
    return listComments.length;
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listComments[position];
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = null;
    try
    {
      String inflater = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
      v = li.inflate(R.layout.list_item, null);

      TextView commentView = (TextView)v.findViewById(R.id.listComment);
      TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
      TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
      Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton);
      Button numberButton = (Button)v.findViewById(R.id.listNumberButton);

      commentView.setText(listComments[position]);
      NumbersView.setText(listNumbers[position]);
      usernamesView.setText(listUsernames[position]);
      usernameButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("usernameOfProfile",listUsernames[position]);
          startActivity(i);
          finish();
        }
      });

      numberButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("NumberProfile",listNumbers[position]);
          startActivity(i);
          finish();
        }
      });   
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return v;
  }

  public void add(String[] comments, String[] usernames,
                  String[] numbers) {
    listComments = comments;
    listNumbers = usernames;
    listUsernames = numbers;
  }

  public int getCount1() {
    if(null == listComments)
    {
      return 0;
    }   

    // TODO Auto-generated method stub
    return listComments.length;
  }

  public Object getItem1(int position) {
    // TODO Auto-generated method stub
    return listComments[position];
  }

  public long getItemId1(int position) {
    // TODO Auto-generated method stub
    return 0;
  }

  public View getView1(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = null;
    try
    {
      String inflater = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
      v = li.inflate(R.layout.list_item, null);


      TextView commentView = (TextView)v.findViewById(R.id.listComment);
      TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
      TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
      Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton);
      Button numberButton = (Button)v.findViewById(R.id.listNumberButton);

      commentView.setText(listComments[position]);
      NumbersView.setText(listNumbers[position]);
      usernamesView.setText(listUsernames[position]);

      usernameButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("usernameOfProfile",listUsernames[position]);
          startActivity(i);
          finish();
        }
      });

      numberButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
          Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
          i.putExtra("NumberProfile",listNumbers[position]);
          startActivity(i);
          finish();
        }
      });
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return v;
  }
}  

アダプターの設定:

final CreateCommentLists mycmlist = new CreateCommentLists(comments, usernames, numbers, DashboardActivity.this);

lstComments = (ListView)findViewById(android.R.id.list);

lstComments.setAdapter(mycmlist);

これが add メソッドの呼び方です。

mycmlist.add(comments,usernames,numbers);
mycmlist.notifyDataSetChanged();
4

2 に答える 2

1

add メソッドでは、配列を新しい値に設定していますlistComments = comments;。これにより、古いデータが新しいデータに置き換えられます。

System.arrayCopy()を使用して listArrays のサイズを新しいサイズに変更し、新しい項目を追加できます。ただし、それほど面倒ではない方法は、配列を として保存するList<String>ことです。これにより、リストのサイズ変更を気にせずに項目を追加できます。

結果は次のようになります...

public class CommentsAdapter extends BaseAdapter
{
private LayoutInflater inflater;

private List<String> comments;
private List<String> numbers;
private List<String> usernames;

public CommentsAdapter(Context context)
{
    inflater = LayoutInflater.from(context);

    comments = new ArrayList<String>();
    numbers = new ArrayList<String>();
    usernames = new ArrayList<String>();
}

public void add(String[] comments, String[] numbers, String[] usernames)
{
    this.comments.addAll(Arrays.asList(comments));
    this.numbers.addAll(Arrays.asList(numbers));
    this.usernames.addAll(Arrays.asList(usernames));

    notifyDataSetChanged();
}

@Override
public int getCount()
{
    if (comments == null)
        return 0;

    return comments.size();
}

@Override
public String getItem(int position)
{
    return comments.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_item, parent, false);
        convertView.setTag(new ViewHolder(convertView));
    }

    ViewHolder holder = (ViewHolder) convertView.getTag();
    holder.commentView.setText(comments.get(position));
    //Other view bind logic here...

    return convertView;
}

private static class ViewHolder
{
    public TextView commentView;
    public TextView numbersView;
    public TextView usernamesView;
    public Button usernameButton;
    public Button numberButton;

    public ViewHolder(View v)
    {
        commentView = (TextView) v.findViewById(R.id.listComment);
        numbersView = (TextView) v.findViewById(R.id.listNumber);
        usernamesView = (TextView) v.findViewById(R.id.listPostedBy);
        usernameButton = (Button) v.findViewById(R.id.listUsernameButton);
        numberButton = (Button) v.findViewById(R.id.listNumberButton);
    }
}
}

また、Android デベロッパー サイトの次のページを読むことを強くお勧めします: http://developer.android.com/training/improving-layouts/smooth-scrolling.html

現在のアダプターの実装は非常に非効率的であり、そのページは問題を解決するのに役立つはずです。

于 2013-08-21T23:02:38.930 に答える
0

おそらく、String[]配列を置き換えるのではなく、既存の配列に追加する必要があります。

2 つの配列を結合する次の関数を追加します (残念ながら、Java 用に実装済みのメソッドはありません)。

String[] concat(String[] A, String[] B) {
   String[] C= new String[A.length + B.length];
   System.arraycopy(A, 0, C, 0, A.length);
   System.arraycopy(B, 0, C, A.length, B.length);
   return C;
}

クレジット: サンフォーラム

そして、addメソッドを次のように変更します。

public void add(String[] comments, String[] usernames,
                  String[] numbers) {
    listComments = concat(listComments, comments);
    listUsernames = concat(listUsernames, usernames);
    listNumbers = concat(listNumbers, numbers);
}

そして、コードにタイプミスがありました。add メソッドでは、listUsernamesandlistNumbersを交換する必要があると思います..修正しました。

于 2013-08-21T22:59:04.937 に答える