2012-01-30 55 views
1

大家好,感謝您看看這個。我使用Android的開發人員指南創建了一個動態ListView。目前,ListView活動似乎在屏幕上顯示,而不需要XML文件或setContentView ...這是一個問題。我已經創建了一個動態ListView - 我如何將它連接到我的XML佈局文件?

我想使用XML佈局,因此我可以將其他視圖添加到屏幕上,而不是將整個活動專門用於顯示列表。我創建了一個XML佈局,其中包含一個空白的ListView,並且我希望我的列表進入分配的空間......所以我的問題是:如何讓我的ListActivity使用我的佈局XML文件?

public class MainList extends ListActivity { 
    static SharedPreferences statusSettings; 
    String jsonString; 
    static JSONObject json; 
    static JSONArray arrayTest; 
    static int bob = 3; 



    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 

     try { 
      JSONObject e = arrayTest.getJSONObject(position); 
      Intent intent = new Intent(); 
      intent.putExtra("clientName", e.getString("proj_name")); 
      intent.putExtra("clientAddress", e.getString("c_address")); 
      intent.putExtra("clientComments", e.getString("comments")); 
      intent.putExtra("clientOrder", e.getString("order")); 
      intent.setClass(MainList.this, ClientDetails.class); 
      MainList.this.startActivity(intent); 


     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      Log.e("JSON", "Problem creating object from array!"); 
      e.printStackTrace(); 
     } 
    } 

    private static class EfficientAdapter extends BaseAdapter { 
     private LayoutInflater mInflater; 


     public EfficientAdapter(Context context) { 
      // Cache the LayoutInflate to avoid asking for a new one each time. 
      mInflater = LayoutInflater.from(context); 

     } 

     public int getCount() { 
      return arrayTest.length(); 

     } 

     public Object getItem(int position) { 
      return position; 
     } 

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

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

      ViewHolder holder; 

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.mainlist, null); 

       holder = new ViewHolder(); 
       holder.textName = (TextView) convertView.findViewById(R.id.tvMainName); 
       holder.textAddress = (TextView) convertView.findViewById(R.id.tvMainAddress); 


       convertView.setTag(holder); 
      } else { 

       holder = (ViewHolder) convertView.getTag(); 
      } 


      JSONObject e; 
      try { 
       e = arrayTest.getJSONObject(position); 
       holder.textName.setText(e.getString("proj_name")); 
       holder.textAddress.setText(e.getString("c_address")); 


       switch (statusSettings.getInt(e.getString("order"), 0)){ 
        case 1: 
         convertView.setBackgroundColor(0x00000000); 
         break; 
        case 2: 
         if(bob == 3){ 
          convertView.setBackgroundColor(0xFFFF6600); 
          bob = 5; 
         } 
         break; 
        case 3: 
         convertView.setBackgroundColor(0xFF00CC00); 
         break; 
        case 4: 
         convertView.setBackgroundColor(0xFFCC0000); 
         break; 
       } 



      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       Log.e("JSON", "Couldn't put one of JSON arrays into object"); 
       e1.printStackTrace(); 
      } 



      return convertView; 
     } 

     static class ViewHolder { 
      TextView textName; 
      TextView textAddress; 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     jsonString = getIntent().getExtras().getString("jsonString"); 
     try { 
      json = new JSONObject(jsonString); 
      arrayTest = json.getJSONArray("client_list"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      Log.e("JSON", "Couldn't create the JSON Array"); 
      e.printStackTrace(); 
     } 
     setListAdapter(new EfficientAdapter(this)); 

    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     bob = 3; 

     statusSettings = getSharedPreferences("status", 0); 



     setListAdapter(new EfficientAdapter(this)); 
    } 


} 

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="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:layout_weight="1"> 
    </ListView> 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 

的事情是,他們創造它,而無需使用任何XML佈局 - 我不完全瞭解,但我相信它有事情做與LayoutInflater。我試過: convertView = mInflater.inflate(R.id.list,null);

而不是 convertView = mInflater.inflate(R.layout.mainlist,null);

我放置了setContentView(R.layout.test); 在我的onCreate ...但沒有工作。你提供的任何幫助將不勝感激,謝謝!

+0

是你創建的R文件是否正確? – shiraz 2012-01-30 10:22:55

+0

是的,我沒有看到它的問題,程序編譯np。 – Wildblood 2012-01-30 10:23:56

+0

順便說一句,這裏是一個很好的資源來學習Android開發,http://www.youtube.com/playlist?list = PLE953C0B85B50AB62&功能= plcp通過幾個最初的視頻瀏覽,看看是否有幫助 – shiraz 2012-01-30 10:28:15

回答

0

這很簡單。我已經快速生成了一些代碼供您參考。

public class ListandtextActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final String [] str = {"ONE","TWO","THREE"}; 
     final ListView lv = (ListView) findViewById(R.id.listView1); 
     final TextView tv = (TextView)findViewById(R.id.tv1); 
     lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, str)); 
     lv.setOnItemClickListener(new OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       tv.setText("You Clicked Something"); 
      } 
     }); 
    } 
} 

而在你的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" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

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

</LinearLayout> 

隨意問,如果您有任何疑問。

+0

我試過這個並取而代之: lv.setAdapter(new ArrayAdapter (getApplicationContext(),android.R.layout.simple_list_item_1,str)); 與我的高效適配器擴展BaseAdapter ...不能工作不幸:( – Wildblood 2012-01-30 11:22:06

+0

該問題可能與您的適配器。我已經在我的機器上運行此代碼。如果出現一些錯誤,您可以pastebin logcat? – 2012-01-30 11:39:39

+0

01-30 13:25:17.103:E/AndroidRuntime(25869):致命例外:主 01-30 13:25:17.103:E/AndroidRuntime(25869):java.lang.NullPointerException 01-30 13:25:17.103:E/AndroidRuntime(25869):\t at hal.makios.v3.MainList2 $ EfficientAdapter.getView(MainList2.java:151) – Wildblood 2012-01-30 11:50:15

相關問題