私はHashMapsの完全なnoobであり、私が必要とするものを見つけるために一週間のうちに良いところを過ごしました。Android Project - JSON配列をHashMapに保存してマップ値を取得する
HTTPリクエストからJSON応答を解析しています。私が問題を抱えているのは、各JSON行の各データ変数/値をHashMapに保存し、後でこれらのデータ値を取得することです。
JSONレスポンスを解析している私のforループでは、putコマンドに正しい構文を使用していることを確認する必要があります。
もっと重要なことに、getBottles()で、名前のHashMapの最初のレコードにo1.setname_abbrを設定する方法を知る必要があります。最終的にはforループに変えて、JSONレスポンスのすべての名前を割り当て、getBottles()のHashMapからすべて戻す必要があります。
何か助けてくれてありがとうございました!ここで
は私は完全にあなたの質問を理解していないと思いますが、あなたは私が見る方法によって(ArrayListにすべてのボトルハッシュマップを追加した場合には、ArrayListを持っている私のコード...public class Bottles extends ListActivity {
private int category_id;
private int subcategory_id;
String subcategory_idString = "false";
String postQuery;
private int bottleID;
String name_abbr;
String name_abbrArray[];
String bottlePicture;
private ProgressDialog m_ProgressDialog = null;
private ArrayList<Bottles> m_bottles = null;
private BottleAdapter m_adapter;
private Runnable viewBottles;
ArrayList<HashMap<String, String>> bottleNameArray = new ArrayList<HashMap<String, String>>();
HashMap<String, String> bottleNamesMap = new HashMap<String, String>();
String name[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.bottleslist);
String result = "";
Bundle extras = getIntent().getExtras();
if(extras != null){
category_id = getIntent().getExtras().getInt("id");
subcategory_id = getIntent().getExtras().getInt("subid");
if(subcategory_id==0){
subcategory_idString = "";
}
}
InputStream is = null;
//http post
try{
if(subcategory_idString==""){
postQuery = "my api call goes here";
}
if(subcategory_idString=="false"){
postQuery = "different api call goes here";
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(postQuery);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
JSONObject row = new JSONObject(result);
JSONArray array = row.getJSONArray("response name");
for (int i = 0; i < array.length(); i++) {
row = array.getJSONObject(i);
bottleID = row.getInt("id");
name_abbr = row.getString("name_abbr");
bottleNamesMap = new HashMap<String, String>();
bottleNamesMap.put("name", name_abbr);
bottlePicture = row.getString("bottlePicture");
}
}catch(Exception e){
Log.e("log_tag", "Error parsing JSON "+e.toString());
}
m_bottles = new ArrayList<Bottles>();
this.m_adapter = new BottleAdapter(this, R.layout.bottlelistimagelayout, m_bottles);
setListAdapter(this.m_adapter);
viewBottles = new Runnable(){
public void run() {
getBottles();
}
};
Thread thread = new Thread(null, viewBottles, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(Bottles.this,
"Please wait...", "Retrieving data ...", true);
}
public class Bottle{
public String name_abbrArray;
//public String lastName;
//public int age;
}
private void getBottles(){
try{
//will put this into a for loop after I figure out how to reference the HashMap values
m_bottles = new ArrayList<Bottles>();
Bottles o1 = new Bottles();
o1.setname_abbr("I want to get the HashMap value of the first name here");
o1.setbottlePicture("Pending");
Bottles o2 = new Bottles();
o2.setname_abbr("I want to get the HashMap value of the second name here");
o2.setbottlePicture("Completed");
m_bottles.add(o1);
m_bottles.add(o2);
Thread.sleep(2000);
Log.i("ARRAY", ""+ m_bottles.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
private Runnable returnRes = new Runnable() {
public void run() {
if(m_bottles != null && m_bottles.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_bottles.size();i++)
m_adapter.add(m_bottles.get(i));
}
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
private class BottleAdapter extends ArrayAdapter<Bottles> {
private ArrayList<Bottles> items;
public BottleAdapter(Context context, int textViewResourceId, ArrayList<Bottles> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.bottlelistimagelayout, null);
}
Bottles o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText("Name: "+o.getname_abbr()); }
if(bt != null){
bt.setText("Picture: "+ o.getbottlePicture());
}
}
return v;
}
}
}
はあなたに感謝 - 私はまだなく、かなりそこに近づいて、しかしです。私はbottleNameArray.add(bottleNamesMap)を追加しました。あなたが正しいので、配列リストは空でした。私は今、bottleNameArray.get(0)を参照することができ、この{name = sample bottle name}のような最初のボトル名を取得します。私はbottleNamesMap.get(0)として参照しようとしましたが、うまくいきませんでした。また、bottleNamesMap.get(name [0])を試しましたが、その構文が気に入らなかった。私は位置iの名前の実際の値を取得する必要があります(この場合は(0))。思考? – Rmilligan2372
'bottleNamesMap.get(position).getString(" name ")'あなたが "サンプルボトル名"を得るべきであるように、 – sebataz
私はそれをちょっと分解しなければなりませんでしたが、必要 - あなたの助けに感謝! – Rmilligan2372