마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

listview에json에서받아온데이터출력이됐다안됐다해요

0 추천
class MyListAdapter extends BaseAdapter{
 Context main;
 LayoutInflater inflater;
 ArrayList<MyItem> arSrc;
 int layout;
 
 public MyListAdapter(Context context,int alayout,ArrayList<MyItem> aarSrc){
  main=context;
  inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  arSrc = aarSrc;
  layout = alayout;
 }

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

 public Object getItem(int position) {
  return arSrc.get(position).name;
 }
 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
  final int pos = position;
  
 if(convertView == null){
  convertView = inflater.inflate(layout, parent, false);
  }
  
  TextView id = (TextView)convertView.findViewById(R.id.id);
  TextView name = (TextView)convertView.findViewById(R.id.name);
  TextView rank = (TextView)convertView.findViewById(R.id.rank);
 
  id.setText(Integer.toString(arSrc.get(position).id));
  name.setText(arSrc.get(position).name);
  rank.setText(Integer.toString(arSrc.get(position).rank));
  
  Toast.makeText(main, arSrc.get(position).name, 0).show();
  
  return convertView;
 }
 
}

listview를 제가 따로 생성해서 텍스트뷰 3개에 json에서 받아온 데이터를 출력하는 방식인데요.

데이터가 잘 들어오는 것 같은데 출력하는 부분으로 넘어갈때 됐다가 안됐다가 하네요..ㅠㅜㅠㅜㅠ

오류문제인가 하고 봤는데 될때랑 안될때랑 차이가 없어요.. 그래서 더 난감합니다.

될때랑 안 될때도 차이가 있는 것도 아니고 진짜 복불복같이 어쩔땐 되고 안되고 해요...

도저히 감이 잡히지 않습니다 ㅠㅜ 왜이럴까요ㅠㅠㅠㅠ 살려주세요..

 

위 클래스는 제가 구현한 어뎁터 코드입니다.

좀 특이 사항이 있다면 출력할 때 로드캣에

senduserAction mView() == null이 뜹니다. 관련이 있는지는 모르겠습니다.  될때나 안될때나 이 코드가 나와서요ㅠㅜ

뇽뇽이 (440 포인트) 님이 2014년 11월 19일 질문
이 앱의 메인코드입니다

public class MainActivity extends Activity {
     
    // json object response url
     
    // json array response url
    private String urlJsonArry = "http://1.234.79.112:8080/worldcup2014/countries";
 
    private static String TAG = MainActivity.class.getSimpleName();
 
    // Progress dialog
    private ProgressDialog pDialog;
 
    
    // temporary string to show the parsed response
    
    public ArrayList<MyItem> arItem;
     
    
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);

        arItem = new ArrayList<MyItem>();
        
        makeJsonArrayRequest();
        
        MyListAdapter myAdapter = new MyListAdapter(this, R.layout.listview, arItem);
       
        ListView myList;
        myList=(ListView)findViewById(R.id.list);
        myList.setAdapter(myAdapter);

    }
 
    /**
     * Method to make json object request where json response starts wtih {
     * */
  
    /**
     * Method to make json array request where response starts with [
     * */
    private void makeJsonArrayRequest() {
        Toast.makeText(MainActivity.this, "aa", 0).show();
        showpDialog();
 
        JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
                new Response.Listener<JSONArray>() {
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        MyItem mi;
                        
                        try {
                            // Parsing json array response
                            // loop through each json object
                            for (int i = 0; i < response.length(); i++) {
 
                                JSONObject countries = (JSONObject)response.get(i);
 
                                String name = countries.getString("name");
                                int id = countries.getInt("id");
                                int ranking = countries.getInt("fifaranking");
                                
                                mi=new MyItem(countries.getInt("id"), countries.getString("name"), countries.getInt("fifaranking"));
                                arItem.add(mi);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
 
                        hidepDialog();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();
                        hidepDialog();
                    }
                });
 
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }
 
    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }
 
    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

1개의 답변

+1 추천
 
채택된 답변

/*호출순서 변경*/

        MyListAdapter myAdapter = new MyListAdapter(this, R.layout.listview, arItem);
        
        ListView myList;
        myList=(ListView)findViewById(R.id.list);
        myList.setAdapter(myAdapter);

        makeJsonArrayRequest();

 

 

arItem.add(mi); 이후에 

myAdapter.notifyDataSetChanged(); 

호출

 

 

샤이닝고 (560 포인트) 님이 2014년 11월 19일 답변
뇽뇽이님이 2014년 11월 19일 채택됨
이제잘되네요!!ㅠㅠㅜ감사합니다!혹시호출순서를왜변경하는지알려주실수있으신가요??제가초보라서잘모르겠습니다ㅠㅜㅜ
...