1

配列からlistView(レイアウト上)からアイテムを追加するにはどうすればよいですか? 何度も試しましたが、毎回エラーが発生します: System services not avaliable before onCreate.();. はい、アクセスしようとすると、この er.ror が表示されることはわかっていますArrayAdapter。ほとんどすべてを試しました。.完全なコードはこちら: pastebin.com/Nv5BkcS7

アップデート

私はいくつかの他のチュートリアルに従いました。私のコードは動作するようになりましたが、データがありません。http://pastebin.com/D8UFKC7iと xml http://pastebin.com/mJfwjGcB なぜ機能しないのか誰か教えてくれませんか? デバッガーは、配列にはすべての整数があると言っています

4

2 に答える 2

2

addcontacts アクティビティで onCreate() メソッドが空白になっています。これが問題です。

public class addcontacts extends ListActivity {

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

また、Layout フォルダーに 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> 
于 2013-02-12T17:54:27.970 に答える
1

1. BaseAdpter または ArrayAdpter を拡張するカスタム アダプターを作成し、コンストラクターで配列または ArrayList を渡します。

2. レイアウトでビューを作成します (行の)

3. カスタム アダプタの getview 関数でこの xml をインフレートし、データを設定します。

活動 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 );


}

これで完了です。

于 2013-02-13T15:41:13.980 に答える