Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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
Android 如何在数据更新时更新listview?_Android_Android Listview_Android Arrayadapter_Android Adapter - Fatal编程技术网

Android 如何在数据更新时更新listview?

Android 如何在数据更新时更新listview?,android,android-listview,android-arrayadapter,android-adapter,Android,Android Listview,Android Arrayadapter,Android Adapter,我是android新手,我想在mysql数据库中插入任何数据时更新我的列表视图。我在互联网上搜索过,我使用了adapter.notifyDataSetChanged();但我一直收到错误“类型ListAdapter的notifyDataSetChanged()方法未定义” 代码: 公共类消息扩展了BaseActivity{ 私有字符串[]navMenuTitles; 专用型雷达导航仪; 私有EditText editTextName; 共享参考sp; 私有字符串jsonResult; 私有列表视

我是android新手,我想在mysql数据库中插入任何数据时更新我的列表视图。我在互联网上搜索过,我使用了adapter.notifyDataSetChanged();但我一直收到错误“类型ListAdapter的notifyDataSetChanged()方法未定义”

代码:

公共类消息扩展了BaseActivity{
私有字符串[]navMenuTitles;
专用型雷达导航仪;
私有EditText editTextName;
共享参考sp;
私有字符串jsonResult;
私有列表视图列表视图;
专用按钮b;
编辑文本etname,et;
文本视图电视;
字符串myJSON;
私有静态最终字符串TAG=“MainActivity.java”;
私有静态最终字符串标记_RESULTS=“result”;
私有静态最终字符串标记\u USERNAME=“USERNAME”;
私有静态最终字符串标记\u NAME=“message\u recd”;
私有静态最终字符串标记\u ADD=“message\u sent”;
JSONArray-peoples=null;
ArrayList个人主义者;
列表视图列表;
公共静态最终字符串USER\u NAME=“USERNAME”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_优先);
SharedReferences myprefs=GetSharedReferences(“用户”,模式世界可读);
String session\u id=myprefs.getString(“session\u id”,null);
TextView TextView=(TextView)findViewById(R.id.fname);
setText(“欢迎”+会话id);
navMenuTitles=getResources().getStringArray(R.array.nav\u抽屉\u项目);
navMenuIcons=getResources().obtainTypedArray(R.array.nav_抽屉图标);
//从中加载图标
//strings.xml
集合(导航菜单工具、导航菜单);
editTextName=(EditText)findViewById(R.id.editTextName);
b=(按钮)findViewById(R.id.button1);
list=(ListView)findViewById(R.id.ListView);
personList=新的ArrayList();
getData();
b、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
etname=(EditText)findViewById(R.id.editTextName);
if(etname.getText().toString().trim().equals(“”){
setName.setError(“不能为空!”);
}
//TODO自动生成的方法存根
否则{
Intent=getIntent();
String fName=intent.getStringExtra(“fName”);
String message=etname.getText().toString();
insertToDatabase(消息,fName);
}
etname.setText(“”);
}
});
}
//开始发送消息
私有void insertToDatabase(字符串消息,字符串fName1){
类SendPostReqAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串paramUsername=params[0];
字符串fname=params[1];
//InputStream=null;
String message=editTextName.getText().toString();
Intent intent1=getIntent();
字符串fName=intent1.getStringExtra(“fName”);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“message”,message));
添加(新的BasicNameValuePair(“用户名”,fName));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/test/test.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
//is=entity.getContent();
}捕获(客户端协议例外e){
}捕获(IOE异常){
}
返回“成功”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG.show();
}
}
SendPostReqAsyncTask SendPostReqAsyncTask=新建SendPostReqAsyncTask();
执行(消息,fName1);
}
//发送消息停止
//获取json数据开始
受保护的无效显示列表(){
试一试{
JSONArray peoples=新的JSONArray(myJSON);

对于(int i=0;i当您对个人列表执行任何
add()
remove()
操作时, 调用
adpater.notifyDataSetChanged();
方法更新
ListView


参考:

该方法是在
BaseAdapter
中定义和实现的,它是
SimpleAdapter
的超类型。因此,只需向
BaseAdapter
SimpleAdapter

添加一个强制转换,我只是这样做了,但我得到了一个错误:类型ListAdapter的方法:-notifyDataSetChanged()未定义
public class Messages extends BaseActivity{

    private String[] navMenuTitles;
    private TypedArray navMenuIcons;
    private EditText editTextName;
    SharedPreferences sp;
    private String jsonResult;
    private ListView listView;
    private Button b;
    EditText etname, et;
    TextView tv;
    String myJSON;
    private static final String TAG = "MainActivity.java"; 
    private static final String TAG_RESULTS="result";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_NAME = "message_recd";
    private static final String TAG_ADD ="message_sent";

    JSONArray peoples = null;

    ArrayList<HashMap<String, String>> personList;

    ListView list;

    public static final String USER_NAME = "USERNAME";

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



        SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
        String session_id= myprefs.getString("session_id", null);


        TextView textView = (TextView) findViewById(R.id.fname);
        textView.setText("Welcome "+session_id);





        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
        // load icons from
                                                            // strings.xml

        set(navMenuTitles, navMenuIcons);
        editTextName = (EditText) findViewById(R.id.editTextName);
        b=(Button)findViewById(R.id.button1);
        list = (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();

        getData();


        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                etname=(EditText)findViewById(R.id.editTextName);
                if( etname.getText().toString().trim().equals("")){
                    etname.setError( "Cannot be left empty!" );    
                }
                // TODO Auto-generated method stub
                else{

                    Intent intent = getIntent();
                    String fName = intent.getStringExtra("fname");
                    String message = etname.getText().toString();
                    insertToDatabase(message, fName);

            }

                etname.setText("");
            }
        });

    }


         //send messages start
        private void insertToDatabase(String message, String fName1){
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                String paramUsername = params[0];
                String fname = params[1];

                //InputStream is = null;

                String message = editTextName.getText().toString();


                Intent intent1 = getIntent();
                String fName = intent1.getStringExtra("fname");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("message", message));
                nameValuePairs.add(new BasicNameValuePair("username", fName));
                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://10.0.2.2/test/test.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    //is = entity.getContent();


                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return "success";
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(message, fName1);
    }
         //send messages stop

        //get json data start
        protected void showList(){
            try {
                JSONArray peoples = new JSONArray(myJSON);
                    for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String name=null, address=null;

                    if(c.has("message_recd"))
                        name = c.getString("message_recd");
                     else 
                    address = c.getString("message_sent");
                    HashMap<String,String> persons = new HashMap<String,String>();
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_ADD,address);
                    personList.add(persons);

                    }
                /*ListAdapter adapter = new SimpleAdapter(
                        Messages.this, personList, R.layout.list_item,
                        new String[]{TAG_NAME,TAG_ADD},
                        new int[]{R.id.name, R.id.address}
                );*/




                    ListAdapter adapter = new SimpleAdapter(
                            Messages.this, personList, R.layout.list_item,
                            new String[]{TAG_NAME,TAG_ADD},
                            new int[]{R.id.name, R.id.address}
                    );

//                    http://www.vogella.com/tutorials/AndroidListView/article.html
//                    http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view

                list.setAdapter(adapter);
            } catch (JSONException e) {
                Log.i("tagconvertstr", "["+myJSON+"]");

            }
        }




        public void getData(){
            class GetDataJSON extends AsyncTask<String, Void, String>{

                 @Override
                protected String doInBackground(String... params) {

                    SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
                    String session_id= myprefs.getString("session_id", null);

                    InputStream inputStream = null;
                    String result = null;
                    try {

                        String postReceiverUrl = "http://10.0.2.2/progress_card/messages/get_messages.php";

                        // HttpClient
                        HttpClient httpClient = new DefaultHttpClient();

                        // post header
                        HttpPost httpPost = new HttpPost(postReceiverUrl);

                        // add your data
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                        nameValuePairs.add(new BasicNameValuePair("username", session_id));

                        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpClient.execute(httpPost);
                        HttpEntity resEntity = response.getEntity();

                        inputStream = resEntity.getContent();
                        // json is UTF-8 by default
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                        StringBuilder sb = new StringBuilder();

                        String line = null;
                        while ((line = reader.readLine()) != null)
                        {
                            sb.append(line + "\n");
                        }
                        result = sb.toString();
                    } catch (Exception e) {
                        Log.i("tagconvertstr", "["+result+"]");
                        System.out.println(e);
                    }
                    finally {
                        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                    }
                    return result;
                }

                @Override
                protected void onPostExecute(String result){
                    myJSON = result;
                    showList();
                }
            }
            GetDataJSON g = new GetDataJSON();
            g.execute();
        }
        //get json data stop

        }