2013-02-12 60 views
1

如何從數組中添加來自listView(在我的佈局上)的項目? 我嘗試了多次,但每次 我收到錯誤: System services not avaliable before onCreate.();。是的,我知道當我嘗試訪問 ArrayAdapter時會顯示此錯誤。嘗試幾乎所有的東西。 。全代碼是在這裏: pastebin.com/Nv5BkcS7 Android ListView - 來自Array [JSON]的項目

UPDATE

我跟一些其他的教程。我的代碼現在可以工作,但沒有數據。 http://pastebin.com/D8UFKC7i和xml http://pastebin.com/mJfwjGcB 有人可以告訴我爲什麼它不起作用。調試器說,陣列都entires

回答

1

1.創建定製適配器exteding BaseAdpter或ArrayAdpter和傳遞數組或ArrayList的在構造函數。

2.創建視圖中

3.inflate此xml定製適配器的getview功能,並設置數據(列)的佈局。

活動XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 
<ListView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/lstText" 
/> 
</LinearLayout> 

列表行XML(佈局row.xml)

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

    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/txtAlertText" /> 

    </LinearLayout> 
</LinearLayout> 

您的活動中創建適配器類

class JSONAdapter extends BaseAdapter implements ListAdapter { 

private final Activity activity; 
private final JSONArray jsonArray; 
private JSONAdapter (Activity activity, JSONArray jsonArray) { 
    assert activity != null; 
    assert jsonArray != null; 

    this.jsonArray = jsonArray; 
    this.activity = activity; 
} 


@Override public int getCount() { 
    if(null==jsonArray) 
    return 0; 
    else 
    return jsonArray.length(); 
} 

@Override public JSONObject getItem(int position) { 
    if(null==jsonArray) return null; 
    else 
     return jsonArray.optJSONObject(position); 
} 

@Override public long getItemId(int position) { 
    JSONObject jsonObject = getItem(position); 

    return jsonObject.optLong("id"); 
} 

@Override public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) 
     convertView = activity.getLayoutInflater().inflate(R.layout.row, null); 



    TextView text =(TextView)convertView.findViewById(R.id.txtAlertText); 

       JSONObject json_data = getItem(position); 
       if(null!=json_data){ 
       String jj=json_data.getString("f_name"); 
       text.setText(jj); 
       } 

    return convertView; 
} 
} 

在你的活動,則添加此。

public class main extends Activity { 
/** Called when the activity is first created. */ 

ListView lstTest; 
//Array Adapter that will hold our ArrayList and display the items on the ListView 
JSONAdapter jSONAdapter ; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Initialize ListView 
    lstTest= (ListView)findViewById(R.id.lstText); 


    jSONAdapter = new JSONAdapter (main.this,jArray);//jArray is your json array 

    //Set the above adapter as the adapter of choice for our list 
    lstTest.setAdapter(jSONAdapter); 


} 

然後你就完成了。

+0

感謝您的回答。 Java拋出異常'執行doInBackground()時發生錯誤' 可能是因爲我沒有把它放在onCreate()中,但是我必須在檢查登錄後加載聯繫列表,以便在執行後臺任務之後。這是我的新貼:http://pastebin.com/84nKS7VQ – Disa 2013-02-13 16:43:44

2

你onCreate()方法是在addcontacts活動的空白,這就是問題:

public class addcontacts extends ListActivity { 

       protected void onCreate() { 
         //set content here      
         setContentView(R.layout.activity_add_contact); 
       } 
       ... 
    ... 

,不要忘了在版式文件夾中創建activity_add_contact.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" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 
+0

現在應用程序卡在onCreate() – Disa 2013-02-12 18:04:22