简体   繁体   中英

Receive Data in a Listview

I am new to Android Studio and i am working on creating an app on my Tablet which receives the incoming data from my Wearable device and displays it in Listview. I referred to this tutorial and was successfully able to send message from Wear to my phone. But the problem is on the receiving end. I am able to receive the data but when i try to display it in a Listview, instead of adding to the listview, it keeps on overwriting on the first value.

MyService.java class:

import android.content.Intent;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

public class MyService extends WearableListenerService {
public static final String Wearable_Path = "/wearable/data/path";
public static final String TAG = "MY DATA MAP.....";




@Override
public void onMessageReceived(MessageEvent messageEvent) {
    if(messageEvent.getPath().equals(Wearable_Path)){
final String message = new String(messageEvent.getData());
        Intent intent = new Intent(this,MainActivity.class);
        intent.putExtra("message",message);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }else{
    super.onMessageReceived(messageEvent);}
}
}

MainActivity.java class:

package com.example.harshsheth1992.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

private ListView list_log;
private ArrayList<String> inc_data=new ArrayList<>();
private ArrayAdapter<String> adapter;
private String msg;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list_log = (ListView) findViewById(R.id.lv_log);
    msg = getIntent().getStringExtra("message");
    inc_data.add(msg);
    adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,inc_data);
    list_log.setAdapter(adapter);
}

I think that it will be a very small mistake or something that i am overlooking but i am not able to figure it out. Thanks in advance!

After this line,

list_log.setAdapter(adapter);

Add

adapter.notifyDataSetChanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM