【问题标题】:Android ListView - items from Array[JSON] [duplicate]Android ListView - 来自数组 [JSON] 的项目 [重复]
【发布时间】:2013-02-12 17:45:15
【问题描述】:

如何从数组中添加 listView(在我的布局上)的项目? 我尝试了多次,但每次 我都会得到错误:System services not avaliable before onCreate.();。是的,我知道当我尝试访问 ArrayAdapter 时会显示此 er.ror。几乎什么都试过了。 .完整代码在这里:pastebin.com/Nv5BkcS7

更新

我遵循了其他一些教程。我的代码现在可以工作,但没有数据。 http://pastebin.com/D8UFKC7i 和 xml http://pastebin.com/mJfwjGcB 有人可以告诉我为什么它不起作用。?调试器说数组有所有的整数

【问题讨论】:

    标签: java android android-listview


    【解决方案1】:

    您的 onCreate() 方法在 addcontacts 活动中为空白,这就是问题所在:

    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> 
    

    【讨论】:

    • 现在应用卡在 onCreate()
    【解决方案2】:

    1.创建自定义适配器扩展BaseAdpter或ArrayAdpter,并在构造函数中传递数组或ArrayList。

    2.在布局中创建视图(行)

    3.在自定义Adapter的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 );
    
    
    }
    

    你就完成了。

    【讨论】:

    • 感谢您的回答。 Java 抛出异常An error occured while executing doInBackground() 可能不是因为我没有把它放在onCreate() 中,但是我必须在检查登录后加载联系人列表,所以在我的一项后台任务之后。这是我的新粘贴:pastebin.com/84nKS7VQ
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多