2012-02-04 144 views
0

有人可以告訴我如何在不使用ListActivity的情況下創建ListView?如何在沒有ListActivity的情況下創建ListView

我需要把我的佈局其他幾個視圖,我希望佈局沒有完全佔用的ListView。它必須是佈局的一部分,而不是整個佈局。我想創建一個正常的活動。

代碼片段:

protected void onCreate(Bundle savedInstanceState) { 
     setContentView(R.layout.savings); 

     //... 
     ListView lvPoupanca = (ListView)this.findViewById(R.id.lvPoupanca); 

     // the adapter 
     TestRepository repo = new TestRepository(this); 

     Cursor c = repo.getCursor(); 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[]{"Nome", "ValorAtual"}, new int[] {R.id.tvNome, R.id.tvValorTotal}); 
     lvPoupancas.setAdapter(adapter); 
    // ... 

} 

編輯:

的saving.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" > 

    <ListView 
     android:id="@+id/lvPoupancas" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     > 

    </ListView> 

</LinearLayout> 

希望有人能幫助我。

感謝和抱歉我的英語不好。

+0

只是爲了解釋:當這段代碼運行時,沒有任何反應。 – fvss 2012-02-04 00:54:43

+0

有趣的部分是'lvPoupancas'的聲明。 – user802421 2012-02-04 01:15:41

+0

對不起,我現在把xml文件。 – fvss 2012-02-04 01:32:59

回答

3

就像在任何活動的任何其他視圖...

ListView l = new ListView(this); 
// OR 
ListView l = (ListView) findViewById(R.id...); 
... 
l.setAdapter(...); 
// If using first way: setContentView(l); 

例如

+0

但我需要我的列表視圖上的savings.xml – fvss 2012-02-04 00:58:35

+1

然後,將其添加到XML並獲取與findViewById() – 2012-02-04 01:42:12

+0

我參與這樣做,但我不知道爲什麼它不起作用。 – fvss 2012-02-04 01:48:11

2

沒有擴展ListActivity然後。根據您的要求,將以下代碼放在活動和onCreate()中。

setContentView(R.layout.ur_xml); 
    Listview list = (ListView)findViewById(R.id.lvPoupancas); 
    list.setAdapter(your Adapter); 
2

我已經回答過類似的問題。但那是在不同的背景下。所以,我在這裏提供了一些參考代碼。它會幫助你解決問題。

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); 
     ArrayAdapter<Object> adapt = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, str); 
     lv.setAdapter(adapt); 
     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" > 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 
    </ListView> 
    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" android:textSize="20dip"/> 
</LinearLayout> 

順便說一句,問問題之前,收集儘可能多的信息,可能別人幫你。如果它不起作用,最好把錯誤日誌放在問題中,而不是僅僅說它不起作用。

+0

謝謝,但我試過,並沒有工作。我認爲我的SimpleCursorAdapter存在一些問題,但我在LogCat上沒有收到任何錯誤,並且使用Log類進行調試,我可以看到所有的遊標。一切正常(沒有錯誤),但它沒有約束力。這個問題讓我很生氣,因爲我在Google上嘗試了很多,但找不到任何解決方案。 – fvss 2012-02-04 11:56:49

+0

嘗試爲此列表內容創建一個新項目並查看它是否有效。同樣,嘗試爲適配器創建一個單獨的項目。如果它們都起作用,那麼您可能可以將它們整合在一起。 Logcat應該給你至少一些參考當你運行一個程序時發生了什麼。當你回覆時,不要忘記粘貼日誌貓。 :) – 2012-02-04 13:36:30

+0

謝謝,這個答案完美。幫助了我一堆;) – Simon 2012-03-17 00:49:08

相關問題