3

コメント、ユーザー名、および番号の ListView を作成するカスタム BaseAdapter があります。問題は、BaseAdapter を更新できるように add メソッドを正しく実装する方法がわからないことです。これが私の現在の 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;
  }
}  

これが私が add メソッドを呼び出すと信じている方法です。

mycmlist.add(コメント、ユーザー名、番号); mycmlist.notifyDataSetChanged();

4

2 に答える 2