没有为自定义ArrayAdapter

时间:2016-11-08 22:03:52

标签: android listview android-arrayadapter android-adapter getview

我是Android开发新手并且正在使用Android Studio。我正在尝试构建一个基本的聊天应用程序。

我的ChatActivity.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();
    }

}

ChatAdapter.java的内容是:

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>

所以,我试图通过添加新的聊天气泡来扩展我的基本聊天布局中的ListView。 getCount()返回正确的非零值,但我认为getView()未被调用。另外,我对应该为适配器使用哪些布局感到困惑。上面的代码是混合不同教程的结果,因此它可能没有很好地构建。

问题:

我没有收到任何我想从ChatActivity.java填充的消息。

1 个答案:

答案 0 :(得分:0)

我之前有过这个问题, 我试着改变代码:     

尝试将wrap_content更改为match_parent

   <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" />

试试吧〜祝你好运