-1

私はAndroid Developmentを初めて使用しています。また、Android Studioを使用しています。私は基本的なチャットアプリケーションを構築しようとしています。私ChatActivity.javaのgetView()がカスタムArrayAdapterのために呼び出されていません

内容は次のとおりです。ChatAdapter.javaの

public class ChatActivity extends AppCompatActivity implements View.OnClickListener { 

    private ListView mDrawerList; 
    private ArrayAdapter<String> mAdapter; 

    private EditText msg_edittext; 
    private String user1 = "anaviltripathi", user2 = "cmaria"; 
    private Random random; 
    ListView msgListView; 
    public static ChatAdapter chatAdapter; 
    public static ArrayList<ChatMessage> chatlist; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chat); 

     mDrawerList = (ListView)findViewById(R.id.navList); 

     Intent intent = getIntent(); 
     Bundle extras = intent.getExtras(); 
     String channel_name = extras.getString("CHANNEL_NAME"); 
     Log.i("ChannelListActivity", channel_name); 
     this.setTitle(channel_name); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     // Show the Up button in the action bar. 
     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.setDisplayHomeAsUpEnabled(true); 
     } 

// 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     ViewGroup container = (ViewGroup)findViewById(R.id.msgListView); 

//  getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
//  getSupportActionBar().setHomeButtonEnabled(true); 

//  To make the navigation bar items clickable 
//  mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
//   @Override 
//   public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
//    Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show(); 
//   } 
//  }); 

//  ListView item = (ListView) findViewById(R.id.msgListView); 
//  View child = getLayoutInflater().inflate(R.layout.chat_bubble_left, null); 
//  item.addFooterView(child); 

     View view = inflater.inflate(R.layout.activity_chat, container, false); 
     random = new Random(); 
     msg_edittext = (EditText) view.findViewById(R.id.messageEditText); 
     msgListView = (ListView) view.findViewById(R.id.msgListView); 
     ImageButton sendButton = (ImageButton) view 
       .findViewById(R.id.sendMessageButton); 
//  sendButton.setOnClickListener(this); 

     // ----Set autoscroll of listview when a new message arrives----// 
     msgListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); 
     msgListView.setStackFromBottom(true); 

     chatlist = new ArrayList<ChatMessage>(); 
     chatlist.add(new ChatMessage(user1, user2, "YO", "1", true)); 
     chatlist.add(new ChatMessage(user2, user1, "YO", "2", false)); 
     chatlist.add(new ChatMessage(user1, user2, "BRO", "3", true)); 
     chatAdapter = new ChatAdapter(container.getContext(), R.layout.chat_bubble_left, chatlist); 
     msgListView.setAdapter(chatAdapter); 
     chatAdapter.add(new ChatMessage(user2, user1, "BRuuuuuuuuuuu", "4", true)); 
     chatAdapter.notifyDataSetChanged(); 
     Log.d("chatActivityLog", "coming to onCreate"); 
    } 

    private void addDrawerItems() { 
     String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" }; 
     mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray); 
     mDrawerList.setAdapter(mAdapter); 
    } 

    public void sendTextMessage(View v) { 
     String message = msg_edittext.getEditableText().toString(); 
     if (!message.equalsIgnoreCase("")) { 
      final ChatMessage chatMessage = new ChatMessage(user1, user2, 
        message, "" + random.nextInt(1000), true); 
      chatMessage.setMsgID(); 
      chatMessage.body = message; 
      chatMessage.Date = CommonMethods.getCurrentDate(); 
      chatMessage.Time = CommonMethods.getCurrentTime(); 
      msg_edittext.setText(""); 
      chatAdapter.add(chatMessage); 
      chatAdapter.notifyDataSetChanged(); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.sendMessageButton: 
//    sendTextMessage(v); 
     } 
     chatAdapter.notifyDataSetChanged(); 
    } 

} 

内容は次のとおりです。チャットレイアウトの

public class ChatAdapter extends ArrayAdapter { 

private static LayoutInflater inflater = null; 
ArrayList<ChatMessage> chatMessageList; 


public ChatAdapter(Context context, int textViewResourceId, ArrayList<ChatMessage> list) { 
    super(context,textViewResourceId); 
    chatMessageList = list; 
    Log.d("ChatAdapter", "comes to constructor"); 

} 

@Override 
public int getCount() { 
    Log.d("chatAdaptergetCount", "size: "+chatMessageList.size()); 
    return chatMessageList.size(); 
} 

@Override 
public ChatMessage getItem(int position) { 
    return this.chatMessageList.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    Log.d("chatAdaptergetView", "coming bro"); 
    ChatMessage message = (ChatMessage) chatMessageList.get(position); 
    View vi = convertView; 
    if (convertView == null) 
     vi = inflater.inflate(R.layout.chat_bubble_left, parent, false); 

    TextView msg = (TextView) vi.findViewById(R.id.message_text); 
    msg.setText(message.body); 
    LinearLayout layout = (LinearLayout) vi 
      .findViewById(R.id.bubble_layout); 
    LinearLayout parent_layout = (LinearLayout) vi 
      .findViewById(R.id.bubble_layout_parent); 



    // if message is mine then align to right 
     if (message.isMine) { 
      msg.setBackgroundResource(R.drawable.bubble2); 
//   layout.setBackgroundResource(R.drawable.bubble2); 
      parent_layout.setGravity(Gravity.RIGHT); 
     } 
     // If not mine then align to left 
     else { 
      msg.setBackgroundResource(R.drawable.bubble1); 
//   layout.setBackgroundResource(R.drawable.bubble1); 
      parent_layout.setGravity(Gravity.LEFT); 
     } 
     msg.setTextColor(Color.BLACK); 
     return vi; 
    } 

public void add(ChatMessage object) { 
    chatMessageList.add(object); 
} 
} 

内容は次のとおりです。

<?xml version="1.0" encoding="utf-8"?<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <LinearLayout 
     android:id="@+id/chat_list_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/app_bar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <ListView 
      android:id="@+id/msgListView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/form" 
      android:divider="@null" 
      android:dividerHeight="0dp" 
      android:paddingBottom="10dp" 
      android:text="@string/hello_world" /> 
<include 
    layout="@layout/type_message_area" 
    android:layout_width="match_parent" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" /> 

</LinearLayout> 

<ListView 
    android:id="@+id/navList" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="left|start" 
    android:background="#ffeeeeee"/> 

チャの内容トンバブルがある:だから

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:id="@+id/bubble_layout_parent" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="right"> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_marginTop="0dp" 
    android:layout_weight="3" 
    android:src="@drawable/user_pacific" /> 


<LinearLayout 
    android:id="@+id/bubble_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 


    <TextView 
     android:id="@+id/message_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxEms="12" 
     android:layout_gravity="center" 
     android:text="Hi! new message" 
     android:textColor="@android:color/primary_text_light" 
     android:background="@drawable/bubble1"/> 
</LinearLayout> 

、私はそれに新しいチャットバブルを追加することによって、私のベースのチャットレイアウトでリストビューを膨らませるためにしようとしています。 getCount()は正しいゼロ以外の値を返しますが、getView()は呼び出されていないと思います。また、私はちょっと混乱して、どのレイアウトをアダプタに使うべきかについて混乱しています。上記のコードは、さまざまなチュートリアルが混在しているため、うまく構築されていない可能性があります。

問題:

私はChatActivity.javaから移入しようとしているすべてのメッセージを取得しておりません。

+0

"getView()が呼び出されていないと思います。 – njzk2

+0

はい、getView()に呼び出されていないログステートメントを置くので、呼び出されていないことを確信しています。 – Pukki

答えて

0

私は 、前にこの質問を持ってきたと私は、コードを変更しよう:

match_parentwrap_contentを変更しよう:

<ListView 
      android:id="@+id/msgListView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/form" 
      android:divider="@null" 
      android:dividerHeight="0dp" 
      android:paddingBottom="10dp" 
      android:text="@string/hello_world" /> 

それ〜幸運

のためにお試しください!
関連する問題