2012-01-30 142 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

相关问题