Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取arraylist中的所有项并将其显示在listview中 公共类ChatBubbleActivity扩展了AppCompative活动{ 私有静态最终字符串TAG=“ChatActivity”; 工具栏; TextView tv_名称; 列表数据列表; int用户标识; int msg_type=1; 数据库助手数据库助手; 私有列表视图列表视图; 私人编辑文本; 私人图像按钮发送; 私有字符串名称、消息、时间; 私人chatarayadapter chatarayadapter; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u chat); name=getIntent().getStringExtra(“名称”); user_id=getIntent().getIntExtra(“user_id”,0); databaseHelper=新的databaseHelper(此); toolbar=findviewbyd(R.id.toolbar); 设置支持操作栏(工具栏); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_-back); setTitleTextColor(getResources().getColor(R.color.white)); setSubtitleTextColor(getResources().getColor(R.color.white)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); receiveMessage(); tv_name=findviewbyd(R.id.tv_name); buttonSend=findViewById(R.id.buttonSend); listView=findViewById(R.id.listView1); chatText=findviewbyd(R.id.chatText); listView.setTranscriptMode(AbsListView.TRANSCRIPT\u MODE\u ALWAYS\u SCROLL); setAdapter(chatArrayAdapter); registerDataSetObserver(新的DataSetObserver()){ @凌驾 更改后的公共无效(){ super.onChanged(); setSelection(chatarayAdapter.getCount()-1); } }); tv_name.setText(名称); } 私有void receiveMessage(){ dataList=newarraylist(); dataList=databaseHelper.getSingleUserMsg(用户id); 对于(int i=0;i_Java_Android_Arraylist - Fatal编程技术网

Java 如何获取arraylist中的所有项并将其显示在listview中 公共类ChatBubbleActivity扩展了AppCompative活动{ 私有静态最终字符串TAG=“ChatActivity”; 工具栏; TextView tv_名称; 列表数据列表; int用户标识; int msg_type=1; 数据库助手数据库助手; 私有列表视图列表视图; 私人编辑文本; 私人图像按钮发送; 私有字符串名称、消息、时间; 私人chatarayadapter chatarayadapter; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u chat); name=getIntent().getStringExtra(“名称”); user_id=getIntent().getIntExtra(“user_id”,0); databaseHelper=新的databaseHelper(此); toolbar=findviewbyd(R.id.toolbar); 设置支持操作栏(工具栏); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_-back); setTitleTextColor(getResources().getColor(R.color.white)); setSubtitleTextColor(getResources().getColor(R.color.white)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); receiveMessage(); tv_name=findviewbyd(R.id.tv_name); buttonSend=findViewById(R.id.buttonSend); listView=findViewById(R.id.listView1); chatText=findviewbyd(R.id.chatText); listView.setTranscriptMode(AbsListView.TRANSCRIPT\u MODE\u ALWAYS\u SCROLL); setAdapter(chatArrayAdapter); registerDataSetObserver(新的DataSetObserver()){ @凌驾 更改后的公共无效(){ super.onChanged(); setSelection(chatarayAdapter.getCount()-1); } }); tv_name.setText(名称); } 私有void receiveMessage(){ dataList=newarraylist(); dataList=databaseHelper.getSingleUserMsg(用户id); 对于(int i=0;i

Java 如何获取arraylist中的所有项并将其显示在listview中 公共类ChatBubbleActivity扩展了AppCompative活动{ 私有静态最终字符串TAG=“ChatActivity”; 工具栏; TextView tv_名称; 列表数据列表; int用户标识; int msg_type=1; 数据库助手数据库助手; 私有列表视图列表视图; 私人编辑文本; 私人图像按钮发送; 私有字符串名称、消息、时间; 私人chatarayadapter chatarayadapter; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u chat); name=getIntent().getStringExtra(“名称”); user_id=getIntent().getIntExtra(“user_id”,0); databaseHelper=新的databaseHelper(此); toolbar=findviewbyd(R.id.toolbar); 设置支持操作栏(工具栏); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_-back); setTitleTextColor(getResources().getColor(R.color.white)); setSubtitleTextColor(getResources().getColor(R.color.white)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); receiveMessage(); tv_name=findviewbyd(R.id.tv_name); buttonSend=findViewById(R.id.buttonSend); listView=findViewById(R.id.listView1); chatText=findviewbyd(R.id.chatText); listView.setTranscriptMode(AbsListView.TRANSCRIPT\u MODE\u ALWAYS\u SCROLL); setAdapter(chatArrayAdapter); registerDataSetObserver(新的DataSetObserver()){ @凌驾 更改后的公共无效(){ super.onChanged(); setSelection(chatarayAdapter.getCount()-1); } }); tv_name.setText(名称); } 私有void receiveMessage(){ dataList=newarraylist(); dataList=databaseHelper.getSingleUserMsg(用户id); 对于(int i=0;i,java,android,arraylist,Java,Android,Arraylist,} 这是我的聊天屏幕活动。我从sqlite数据库检索值并将其存储在arraylist中。当我试图从arraylist中获取数据并在listview中显示时,它还显示arraylist中的最后一项。如何获取arraylist中的所有项并将其加载到listview中。如何做到这一点?请帮忙做这件事 public class ChatBubbleActivity extends AppCompatActivity { private static final String TAG = "ChatAct

}

这是我的聊天屏幕活动。我从sqlite数据库检索值并将其存储在arraylist中。当我试图从arraylist中获取数据并在listview中显示时,它还显示arraylist中的最后一项。如何获取arraylist中的所有项并将其加载到listview中。如何做到这一点?请帮忙做这件事

public class ChatBubbleActivity extends AppCompatActivity {
private static final String TAG = "ChatActivity";
Toolbar toolbar;
TextView tv_name;
List<ListData> dataList;
int user_id;
int msg_type = 1;
DatabaseHelper databaseHelper;
private ListView listView;
private EditText chatText;
private ImageButton buttonSend;
private String name, message, time;
private ChatArrayAdapter chatArrayAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    name = getIntent().getStringExtra("name");
    user_id = getIntent().getIntExtra("user_id", 0);
    databaseHelper = new DatabaseHelper(this);

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
    toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    toolbar.setSubtitleTextColor(getResources().getColor(R.color.white));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    receiveMessage();
    tv_name = findViewById(R.id.tv_name);
    buttonSend = findViewById(R.id.buttonSend);
    listView = findViewById(R.id.listView1);
    chatText = findViewById(R.id.chatText);
    listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    listView.setAdapter(chatArrayAdapter);
    chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            listView.setSelection(chatArrayAdapter.getCount() - 1);
        }
    });
    tv_name.setText(name);
}

private void receiveMessage() {
    dataList = new ArrayList<>();
    dataList = databaseHelper.getSingleUserMsg(user_id);
    for (int i = 0; i < dataList.size(); i++) {
        ListData listData=dataList.get(i);
        message = listData.getMessage();
        time = listData.getCreated_at();
        msg_type = listData.getMsg_type();
        Log.d("Message",message);
        Date dateTime = null;
        try {
            dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        DateFormat timeFormatter = new SimpleDateFormat("h:mm a");
        int layout_resource;
        if (dataList.get(i).getMsg_type() == 0) {
            layout_resource = R.layout.item_chat_left;
        } else {
            layout_resource = R.layout.item_chat_right;
        }
        chatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), layout_resource, dataList, msg_type);
        chatArrayAdapter.add(new ChatMessage(message, timeFormatter.format(dateTime)));
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.conversation_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
public List getSingleUserMsg(int-user\u-id){
List listData=new ArrayList();
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(“选择消息,创建时间,消息类型来自“+表消息+”,其中“+用户ID+”=“+用户ID+”订单由“+创建时间+”;”,null);
ListData listData1;
while(cursor.moveToNext()){
listData1=新ListData();
字符串消息=cursor.getString(cursor.getColumnIndexOrThrow(“消息”));
字符串created_at=cursor.getString(cursor.getColumnIndexOrThrow(“created_at”);
字符串msg_type=cursor.getString(cursor.getColumnIndexOrThrow(“msg_type”);
listData1.setMessage(消息);
listData1.setMsg_类型(Integer.parseInt(msg_类型));
listData1.setCreated_at(created_at);
添加(listData1);
}
返回列表数据;
}

上面的代码是数据库助手的方法。

在for循环之外写这行代码

public List<ListData> getSingleUserMsg(int user_id) {
    List<ListData> listData = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select message,created_at,msg_type from " + TABLE_MSG + " WHERE " + USER_ID + " = " + user_id + " ORDER BY " + CREATED_AT + ";", null);
    ListData listData1;
    while (cursor.moveToNext()) {
        listData1 = new ListData();
        String message = cursor.getString(cursor.getColumnIndexOrThrow("message"));
        String created_at = cursor.getString(cursor.getColumnIndexOrThrow("created_at"));
        String msg_type = cursor.getString(cursor.getColumnIndexOrThrow("msg_type"));
        listData1.setMessage(message);
        listData1.setMsg_type(Integer.parseInt(msg_type));
        listData1.setCreated_at(created_at);
        listData.add(listData1);
    }
    return listData;
}

在循环外部添加适配器代码,并刷新列表。因为调用
receiveMessage()
之后,只有设置适配器才是问题所在

1.在
listView
初始化调用
receiveMessage()

tv\u name=findviewbyd(R.id.tv\u name);
buttonSend=findViewById(R.id.buttonSend);
listView=findViewById(R.id.listView1);
chatText=findviewbyd(R.id.chatText);
listView.setTranscriptMode(AbsListView.TRANSCRIPT\u MODE\u ALWAYS\u SCROLL);
receiveMessage();
私有void receiveMessage(){
dataList=newarraylist();
dataList=databaseHelper.getSingleUserMsg(用户id);
对于(int i=0;ichatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), layout_resource, dataList, msg_type);
        chatArrayAdapter.add(new ChatMessage(message, timeFormatter.format(dateTime)));
tv_name = findViewById(R.id.tv_name);
buttonSend = findViewById(R.id.buttonSend);
listView = findViewById(R.id.listView1);
chatText = findViewById(R.id.chatText);
listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
receiveMessage();

  private void receiveMessage() {
    dataList = new ArrayList<>();
    dataList = databaseHelper.getSingleUserMsg(user_id);
    for (int i = 0; i < dataList.size(); i++) {
        ListData listData=dataList.get(i);
        message = listData.getMessage();
        time = listData.getCreated_at();
        msg_type = listData.getMsg_type();
        Log.d("Message",message);
        Date dateTime = null;
        try {
            dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a").parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        DateFormat timeFormatter = new SimpleDateFormat("h:mm a");
        int layout_resource;
        if (dataList.get(i).getMsg_type() == 0) {
            layout_resource = R.layout.item_chat_left;
        } else {
            layout_resource = R.layout.item_chat_right;
        }

    }
    chatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), layout_resource, dataList, msg_type);
    chatArrayAdapter.add(new ChatMessage(message, timeFormatter.format(dateTime)));
    listView.setAdapter(chatArrayAdapter);
}