2012-02-04 143 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

相关问题