2014-09-26 3 views
0

json 데이터를 구문 분석하고이를 사용자 지정 목록보기에 표시하려고합니다. U 아래내 목록보기가 채워지지 않습니다.

public class MainActivity extends Activity { 
 

 
\t ListView list; 
 
\t ArrayList<listview> array_list = new ArrayList<listview>(); 
 

 
\t class listview { 
 
\t \t String description; 
 

 
\t \t public listview(String description) { 
 
\t \t \t this.description = description; 
 
\t \t } 
 
\t } 
 
\t 
 
\t class MyAdapter extends BaseAdapter { \t \t 
 
\t \t 
 
\t \t 
 

 
\t \t public MyAdapter(Context context) { \t \t \t 
 
\t \t } 
 

 
\t \t @Override 
 
\t \t public int getCount() { 
 
\t \t \t return array_list.size(); 
 
\t \t } 
 

 
\t \t @Override 
 
\t \t public Object getItem(int position) { 
 
\t \t \t return array_list.get(position); 
 
\t \t } 
 

 
\t \t @Override 
 
\t \t public long getItemId(int position) { 
 
\t \t \t return position; 
 
\t \t } 
 

 
\t \t @SuppressLint("ViewHolder") 
 
\t \t @Override 
 
\t \t public View getView(int position, View convertView, ViewGroup parent) { 
 
\t \t \t LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
\t \t \t View view = inflater.inflate(R.layout.listview, parent, false); 
 
\t \t \t TextView description = (TextView) view 
 
\t \t \t \t \t .findViewById(R.id.description); 
 
\t \t \t listview list = array_list.get(position); 
 
\t \t \t description.setText(list.description); 
 
\t \t \t return view; 
 
\t \t } 
 
\t } 
 

 
\t class getJson_data extends AsyncTask<Void, Void, Void> { 
 

 
\t \t @Override 
 
\t \t protected Void doInBackground(Void... params) { 
 

 
\t \t \t Get_data data = new Get_data(); 
 
\t \t \t String output = ""; 
 
\t \t \t try { 
 
\t \t \t \t output = data.get_info(); 
 

 
\t \t \t \t JSONArray jarray = new JSONArray(output); 
 
\t \t \t \t for (int i = 0; i < jarray.length(); i++) { 
 
\t \t \t \t \t JSONObject jobject = jarray.getJSONObject(i); 
 
\t \t \t \t \t JSONObject product = jobject.getJSONObject("product"); 
 
\t \t \t \t \t String description = product.getString("desc"); 
 
\t \t \t \t \t array_list.add(new listview(description)); 
 
\t \t \t \t } 
 

 
\t \t \t } catch (ClientProtocolException e) { 
 
\t \t \t \t e.printStackTrace(); 
 
\t \t \t } catch (IOException e) { 
 
\t \t \t \t e.printStackTrace(); 
 
\t \t \t } catch (JSONException e) { 
 
\t \t \t \t e.printStackTrace(); 
 
\t \t \t } 
 
\t \t \t return null; 
 
\t \t } 
 
\t } 
 

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

 
\t \t if (android.os.Build.VERSION.SDK_INT > 9) { 
 
\t \t \t StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
 
\t \t \t \t \t .permitAll().build(); 
 
\t \t \t StrictMode.setThreadPolicy(policy); 
 
\t \t } 
 
\t \t new getJson_data().execute(); 
 
\t \t list = (ListView) findViewById(R.id.listview1); 
 
\t \t list.setAdapter(new MyAdapter(this)); 
 
\t } 
 

 
\t 
 
}

내 사용자 정의 레이아웃 listview.xml 찾을 수 있습니다

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" > 
 

 
    <TextView 
 
     android:id="@+id/title" 
 
     android:text="@string/text" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignTop="@+id/imageView1" 
 
     android:layout_toRightOf="@+id/imageView1" 
 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
 

 
    <ImageView 
 
     android:id="@+id/imageView2" 
 
     android:layout_width="32dp" 
 
     android:layout_height="32dp" 
 
     android:layout_alignBottom="@+id/description" 
 
     android:layout_alignTop="@+id/imageView3" 
 
     android:layout_marginRight="21dp" 
 
     android:layout_toLeftOf="@+id/imageView3" 
 
     android:contentDescription="@string/image_info" 
 
     android:src="@drawable/arrow_icon_circle" /> 
 

 
    <ImageView 
 
     android:id="@+id/imageView3" 
 
     android:layout_width="25dp" 
 
     android:layout_height="25dp" 
 
     android:layout_alignBottom="@+id/description" 
 
     android:layout_alignParentRight="true" 
 
     android:layout_marginRight="18dp" 
 
     android:contentDescription="@string/image_info" 
 
     android:src="@drawable/delete" /> 
 

 
    <ImageView 
 
     android:id="@+id/imageView4" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBottom="@+id/imageView1" 
 
     android:layout_alignLeft="@+id/title" 
 
     android:contentDescription="@string/image_info" 
 
     android:src="@drawable/verified_circle_check_icon" /> 
 

 
    <TextView 
 
     android:id="@+id/description" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignTop="@+id/imageView4" 
 
     android:layout_toRightOf="@+id/title" 
 
     android:text="@string/text" 
 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 
 

 
    <ImageView 
 
     android:id="@+id/imageView1" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_alignParentTop="true" 
 
     android:layout_marginLeft="22dp" 
 
     android:layout_marginTop="19dp" 
 
     android:contentDescription="@string/image_info" 
 
     android:src="@drawable/ic_launcher" /> 
 

 
</RelativeLayout>
다음

인 헬퍼 클래스 (작동이 잘되는) JSON 데이터, 변환을 추출하는 그것을 문자열로 변환하고 doInBackground 메소드로 다시 리턴합니다.

Get_data data = new Get_data(); 
 
String output = ""; 
 
try { 
 
\t output = data.get_info();

난 여기 헬퍼 클래스 메소드

[ 
 
    { 
 
     "@type": "item", 
 
     "ID": 1036, 
 
     "product": { 
 
      "active": true, 
 
      "categories": [], 
 
      "desc": "thubnil image icon testing", 
 
      "gtins": [], 
 
      "id": 443, 
 
      "image": "", 
 
      "modelNumber": "thubnil im", 
 
      "name": "test image" 
 
     } 
 
    }, 
 
    { 
 
     "@type": "item", 
 
     "ID": 781, 
 
     "product": { 
 
      "active": true, 
 
      "categories": [], 
 
      "desc": "cars", 
 
      "gtins": [], 
 
      "id": 46, 
 
      "image": "", 
 
      "modelNumber": "car1234", 
 
      "name": "Cars" 
 
     } 
 
    }, 
 
    { 
 
     "@type": "item", 
 
     "ID": 783, 
 
     "product": { 
 
      "active": true, 
 
      "categories": [], 
 
      "desc": "cars", 
 
      "gtins": [], 
 
      "id": 46, 
 
      "image": "", 
 
      "modelNumber": "car1234", 
 
      "name": "Cars" 
 
     } 
 
    } 
 
]

하지만 아무것도 ... 내가 잘못 어디 갔지 내 목록보기에 나타납니다에서 가져온 JSON 데이터는? 이 방법에 대한 제안은 제게 많은 도움이 될 것입니다. 미리 감사드립니다.

+1

MyAdapter 개체를 가져오고 Async 작업의 onPostExecute()에서 호출하십시오. 또는 onPostExecute 메서드에서 어댑터를 설정할 수 있습니다 –

+0

이 코드는 올바른지? JSONArray jarray = 새 JSONArray (출력); \t \t \t \t 가 (; 나는 jarray.length()을 <; I = 0 int로 난 ++) { \t \t \t \t \t 된 JSONObject하는 jobject = jarray.getJSONObject (I); \t \t \t \t \t JSONObject product = jobject.getJSONObject ("product"); \t \t \t \t \t 문자열 설명 = product.getString ("desc"); \t \t \t \t \t array_list.add (새 listview (description)); 당신이 onPostExecute'이 [링크]를 참조하십시오'에보기를 업데이트하지 않습니다 –

+0

\t \t \t \t} (http://www.androidhive.info/2012/01/android-json-parsing-tutorial/) –

답변

0

목록보기를 업데이트하려면 목록보기를 업데이트하기위한 코드를 작성해야합니다 (onPostExecute()). 이것을 link

+0

그것은 일했다 .. 고마워요 @Raditya Kurnianto –

관련 문제