0

これは、更新および保存後に結果ページとして開く ArrayList ページです。UIの変更を反映するために、何らかの方法で更新する必要があると思います。私は notifyDataSetChanged() を呼び出そうとしましたが、私の経験レベルでは運がありません。誰かがそれを実装する方法を親切に教えてもらえますか?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_requests);

    requestsList = new ArrayList<HashMap<String, String>>();

    new LoadAllRequests().execute();

    ListView list = getListView();

    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String request_id = ((TextView) view.findViewById(R.id.request_id)).getText().toString();


            Intent in = new Intent(getApplicationContext(),
                    ViewRequestActivity.class);

            in.putExtra(TAG_ID, request_id);


            startActivityForResult(in, 100);
        }
    });

}

// Response from ViewRequestActivity when delete a request reload this page again

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {            
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}


class LoadAllRequests extends AsyncTask<String, String, String> {



    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_all_requests, "GET", params);


        Log.d("All Requests: ", json.toString());

        try {

            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {


                requests = json.getJSONArray(TAG_REQUESTS);


                for (int i = 0; i < requests.length(); i++) {
                    JSONObject c = requests.getJSONObject(i);


                    String request_id = c.getString(TAG_ID);
                    String request_title = c.getString(TAG_TITLE);


                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, request_id);
                    map.put(TAG_TITLE, request_title);


                    requestsList.add(map);
                }
            } else {


                Intent i = new Intent(getApplicationContext(),
                        NewRequestActivity.class);

                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
4

2 に答える 2

2

ListView 用のアダプターを作成する必要があります。アダプターは、表示するためにデータをフィードするものです。このチュートリアルを読むことをお勧めします。

http://www.vogella.com/articles/AndroidListView/article.html

したがって、アダプタを作成してlv.setAdapter(<adapter>)を呼び出したら、 を呼び出すことができます<adapter>.notifyDataSetChanged()。これにより、アダプター自体を更新する必要があることがアダプターに通知されます。

于 2012-08-06T14:32:02.483 に答える
1

アダプターに notifyDataSetChanged() メソッドを使用できます。リストビューを更新したい場合はいつでも、次の方法で使用できます。

 adapter.notifyDataSetChanged(); 
于 2012-08-06T14:23:04.053 に答える