2014-10-09 79 views
0

我有一個問題,類DataListAdapter沒有寫入給它的文本視圖,留下的文本視圖留空。此顧慮的活動是這樣的:Android Base Adapter不寫入文本視圖

public class LoadData extends Activity 
{ 
    ListView dataList; 
    MachineDataAdapter adapter; 
    Context context; 

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

     dataList = (ListView) findViewById(R.id.machineList); 

     //Log.d("intent", getIntent().getStringExtra("json")); 
     dataList.setAdapter(new DataListAdapter(getIntent().getStringExtra("json"))); 
     context = this; 

    } 

    public void backClicked(View view) 
    { 
     Intent mainMenu = new Intent(getApplicationContext(), MainMenu.class); 
     startActivity(mainMenu); 
    } 

    public void launchScan(View view) 
    { 
     Intent scanLaunch = new Intent(getApplicationContext(), ScanTag.class); 
     startActivity(scanLaunch); 
    } 

    class DataListAdapter extends BaseAdapter 
    { 
     private JSONObject json; 
     private Iterator<String> keys; 
     LayoutInflater inflater; 

     public DataListAdapter(String jsonString) 
     { 
      try 
      { 
       json = new JSONObject(jsonString); 
      } catch (JSONException e) 
      { 
       e.printStackTrace(); 
      } 

      keys = json.keys(); 
      inflater = getLayoutInflater(); 

     } 


     public int getCount() { 
      return json.names().length(); 
     } 

     public Object getItem(int arg0) { 
      return null; 
     } 

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

     public View getView(int i, View convertView, ViewGroup parent) { 


      if(convertView == null) 
       convertView = inflater.inflate(R.layout.machine_data_row, parent, false); 

      TextView title = (TextView) convertView.findViewById(R.id.machineRowTitle); 
      TextView data = (TextView) convertView.findViewById(R.id.machineRowData); 

      if(keys.hasNext()) 
      { 
       String key = keys.next(); 
       String value = null; 
       try 
       { 
        value = json.getString(key); 
       } catch (JSONException e) 
       { 
        e.printStackTrace(); 
       } 
       Log.d("json", key); 
       title.setText(key); 
       data.setText(value); 
      } 

      return (convertView); 
     } 
    } 



} 

這裏是爲了完整machine_data_row.xml:

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

    <TextView 
      android:id="@+id/machineRowTitle" 
      android:textSize="20sp" 
      android:textStyle="bold" 
      android:textColor="#0000FF" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

    <TextView 
      android:id="@+id/machineRowData" 
      android:textColor="#5C002E" 
      android:textSize="17sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

</LinearLayout> 

JSON的作品就好了,我成功地打印出每一個使用Log.d.關鍵任何幫助將不勝感激。

編輯:這是JSON數據:

{"a":"b","c":"d","e":"f"} 
+0

請認真閱讀您的json內容 – silly 2014-10-09 14:06:07

+0

我已根據要求使用json數據修改了帖子。 – Derek 2014-10-09 14:21:40

回答

1

我不知道爲什麼這個工作,但拿到鑰匙使用key = json.names().getString(i); 解決了這個問題。

+0

是的,這是因爲多次調用getView(不是每行一次).. – silly 2014-10-09 14:30:42

+0

你不能在適配器中使用迭代器。一個迭代器按順序爲每個項目提供一次。一個適配器可以以任何次序要求查看任何項目的任何次數。 – njzk2 2014-10-09 14:33:23

+0

也應該知道,json對象中的鍵也不能保證它們的順序。 – njzk2 2014-10-09 14:34:11

1

通常我喜歡做這樣的事情在getView

if (convertView == null) { 
      LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 
      //Make sure the textview exists in this xml 
} else { 
      row = convertView; 
} 
TextView titleLabel = (TextView) row.findViewById(R.id.titleText); 
+0

這有效,但它只填滿了一行,這真的很奇怪。 – Derek 2014-10-09 14:17:58