用于聊天应用的自定义数组适配器

时间:2016-05-06 21:32:02

标签: android bluetooth android-arrayadapter

我正在创建蓝牙聊天应用,我对Android编程有点新意。 我的应用程序可以管理蓝牙连接和发送/接收数据。 我的问题是用字符串填充列表视图,因为我不太喜欢创建Custom ArrayAdapters。我有2个形状(红色和蓝色气泡)用于发送和接收的消息(R.drawable.rounded_corner和R.drawable.rounded_corner_get)。我正在管理活动中的蓝牙连接(ConnectedThread)(MessengerActivity),如下面的代码所示。 因此,如果有人知道我如何为给定的代码创建Custom ArrayAdapter或者知道任何其他类似的StackOverflow问题,我真的可以使用帮助。

public class MessengerActivity extends AppCompatActivity {
protected static final int MESSAGE_READ = 5;
ConnectedThread mConnectedThhread;
static BluetoothSocket blueSocket;
String nameOfDevice;
public String message;
Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Log.i("tag", "in handler");
        switch (msg.what) {
            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                String string = new String(readBuf);
                try {
                    getMessage(string);
                } catch (Exception e) {}
                break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_messenger);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    blueSocket = BluetoothSocketGet.blueSocket;
    nameOfDevice = blueSocket.getRemoteDevice().getName();
    toolbar.setTitle(nameOfDevice);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mConnectedThhread = new ConnectedThread(blueSocket);
    mConnectedThhread.start();

}

public void messageText(View v) {
    EditText edit = (EditText) findViewById(R.id.textMessage);
    String string;
    string = edit.getText().toString();
    edit.setText("");
    mConnectedThhread.write(string.getBytes());
}

public void getMessage(String s) {
    message=s;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_messenger, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

protected class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {

        mmSocket = socket;

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer;  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                buffer = new byte[1024];
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();

            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) {
        }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        if (mmInStream != null) {
            try {
                mmInStream.close();
            } catch (IOException e) {
            }
        }
        if (mmOutStream != null) {
            try {
                mmOutStream.close();
            } catch (IOException e) {
            }
        }
        if (mmSocket != null) {
            try {
                mmSocket.close();
            } catch (IOException e) {
            }
        }
    }
}}

MessengerActivity的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.milan.redtooth.MessengerActivityFragment"
tools:showIn="@layout/activity_messenger">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:transcriptMode="alwaysScroll"
    android:stackFromBottom="true"
    android:layout_weight="1"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:id="@+id/listMessages">
</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textMessage"
        android:layout_weight="1"
        android:layout_gravity="bottom" />

    <ImageButton
        android:onClick="messageText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sendMessage"
        android:layout_gravity="bottom"
        android:src="@drawable/ic_send_white_18dp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

=&GT;您必须定义两个列表行布局

1)。 item_send.xml用于发送消息

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

        <TextView
            android:id="@+id/msgr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5sp"
            android:layout_gravity="right"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="20dp"
            android:background="@drawable/ic_send"
            android:shadowColor="@color/textShadow"
            android:shadowDx="1"
            android:shadowDy="1"
            android:text="Medium Texts"
            android:textColor="@color/textColor"
            android:textSize="18sp" />

    </LinearLayout>

2)。用于接收消息的item_receive.xml

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

        <TextView
            android:id="@+id/msgr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5sp"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/ic_receive"
            android:shadowColor="@color/textShadow"
            android:shadowDx="1"
            android:layout_gravity="left"
            android:shadowDy="1"
            android:text="Medium Text"
            android:textColor="@color/textColor"
            android:textSize="18sp" />

    </LinearLayout>

    [Replace the TextView Background with your own 9 patch bubble image for each row_layout (send and received)]

=&GT;现在您必须创建自定义数组适配器

    class ChatArrayAdapter extends ArrayAdapter<ChatMessage> {

        private TextView chatText;
        private List<ChatMessage> chatMessageList = new ArrayList<ChatMessage>();
        private Context context;

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

        public ChatArrayAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
            this.context = context;
        }

        public int getCount() {
            return this.chatMessageList.size();
        }

        public ChatMessage getItem(int index) {
            return this.chatMessageList.get(index);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ChatMessage chatMessageObj = getItem(position);
            View row = convertView;
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (chatMessageObj.left) {
                row = inflater.inflate(R.layout.item_send, parent, false);
            } else {
                row = inflater.inflate(R.layout.item_receive, parent, false);
            }
            chatText = (TextView) row.findViewById(R.id.msgr);
            chatText.setText(chatMessageObj.message);
            return row;
        }
    }

=&GT;在您的主要活动onCreate()方法中,只需放置以下代码

ChatArrayAdapter chatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), R.layout.item_send);
    ListView listView = (ListView) findViewById(R.id.listMessage);
    listView.setAdapter(chatArrayAdapter);    
    listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    listView.setAdapter(chatArrayAdapter);
    //to scroll the list view to bottom on data change
    chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            listView.setSelection(chatArrayAdapter.getCount() - 1);
        }
    });

=&GT;在您的主活动中,只需放置以下方法并按下按钮

即可调用它
        private boolean receiveMessage(String msg) {
            chatArrayAdapter.add(new ChatMessage(false, msg));
            return true;
        }


        private boolean sendMessage(String msg) {
            chatArrayAdapter.add(new ChatMessage(true, msg));
            return true;
        }

        enter code here