1

Web で見つけたいくつかのチュートリアルで練習しています。リスト用のデータベースと通信しています。さまざまな名前が表示されるように、リストビュー リストビュー アダプターで展開します。検索ダイアログを追加したいと思います。この例の画像のようなものです。しかし、展開のどこにもないので、さまざまな方法で試しました。これはコード例の一部です:

リスト活動:

public class List extends Activity {

private ProgressDialog pDialog;
public int i = 0;
JSONParser jParser = new JSONParser();

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

private static String url_daftar_rs = "my_url";

public static final String TAG_SUCCESS = "success";
public static final String TAG_DAFTAR_RS = "daftar_rs";
public static final String TAG_ID_RS = "id_rs";
public static final String TAG_NAMA_RS = "nama_rs";
public static final String TAG_LINK_IMAGE_RS = "link_image_rs";
public static final String TAG_ALAMA_RS = "alamat_rs";
public static final String TAG_TELEPONS_RS = "telepon_rs";

JSONArray daftar_rs = null;

ListView list;
ListAdapter adapter;
private ListadoActivity activity;

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

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

    new Activity().execute();

    activity = this;
    list = (ListView) findViewById(R.id.list);
    list.setOnItemClickListener(new OnItemClickListener() {

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

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

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

            in.putExtra(TAG_ID_RS, id_rs);

            startActivityForResult(in, 100);
        }
    });


}

public void SetListViewAdapter(ArrayList<HashMap<String, String>> daftar) {
    adapter = new ListAdapter(activity, daftar);
    list.setAdapter(adapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == 100) {

        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(List.this);
        pDialog.setMessage("wait..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {

        Conexion();
        if (i == 0) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

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

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

            try {

                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {

                    daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);

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

                        String id_rs = c.getString(TAG_ID_RS);
                        String nama_rs = c.getString(TAG_NAMA_RS);
                        String link_image_rs = c
                                .getString(TAG_LINK_IMAGE_RS);
                        String alamat_rs = c.getString(TAG_ALAMAT_RS);
                        String telepon_rs = c.getString(TAG_TELEPON_RS);

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

                        map.put(TAG_ID_RS, id_rs);
                        map.put(TAG_NAMA_RS, nama_rs);
                        map.put(TAG_LINK_IMAGE_RS, link_image_rs);
                        map.put(TAG_ALAMAT_RS, alamat_rs);
                        map.put(TAG_TELEPON_RS, telepon_rs);

                        DaftarRS.add(map);
                    }
                } else {

                }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
}


    else{ finish();
        }
        return null;
    }

JSON パーサー:

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

    }
}

リストアダプター:

public class ListAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;

public ListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.item_list_rs, null);

    TextView id_rs = (TextView) vi.findViewById(R.id.id_rs);
    TextView nama_rs = (TextView) vi.findViewById(R.id.nama_rs); 
    TextView link_image_rs = (TextView) vi.findViewById(R.id.link_image_rs);
    TextView alamat_rs = (TextView) vi.findViewById(R.id.alamat_rs); 
    TextView telepon_rs = (TextView) vi.findViewById(R.id.telepon_rs);
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.image_rs); 

    HashMap<String, String> daftar_rs = new HashMap<String, String>();
    daftar_rs = data.get(position);

    id_rs.setText(daftar_rs.get(ListadoActivity.TAG_ID_RS));
    nama_rs.setText(daftar_rs.get(ListadoActivity.TAG_NAMA_RS));
    link_image_rs.setText(daftar_rs.get(ListadoActivity.TAG_LINK_IMAGE_RS));
    alamat_rs.setText(daftar_rs.get(ListadoActivity.TAG_ALAMAT_RS));
    telepon_rs.setText(daftar_rs.get(ListadoActivity.TAG_TELEPON_RS));
    imageLoader.DisplayImage(daftar_rs.get(ListadoActivity.TAG_LINK_IMAGE_RS),
            thumb_image);
    return vi;
}


}
4

1 に答える 1