Android-使用列表适配器和其他字段创建视图不会';我什么也不做

Android-使用列表适配器和其他字段创建视图不会';我什么也不做,android,android-layout,Android,Android Layout,我在玩弄这种观点: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+i

我在玩弄这种观点:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

<TextView
    android:id="@+id/solution_title"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Label1"
    />  

<TextView
    android:id="@+id/solution_description"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Label2"
    />  

    <ListView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </ListView>

</LinearLayout>

我想做的是使用ListAdapter来显示ListView和项目列表,并对前两个项目使用简单的文本标签

现在,当我尝试只显示前两个文本字段时,没有得到任何渲染:

public class SuggestedSolutionActivity extends ListActivity 
{
    ArrayAdapter<SolutionTopic> adapter;        

    ArrayList<SolutionTopic> problems = new ArrayList <SolutionTopic>( );   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);        
        adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution, R.id.label, problems);

        final TextView solution_title  = (TextView) findViewById(R.id.solution_title);        
        final TextView solution_description = (TextView) findViewById(R.id.solution_description);

}
公共类SuggestedSolutionActivity扩展了ListActivity
{
阵列适配器;
ArrayList problems=新的ArrayList();
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
适配器=新阵列适配器(此,R.layout.solution,R.id.label,问题);
最终文本视图解决方案标题=(文本视图)findViewById(R.id.solution\u标题);
最终文本视图解决方案描述=(文本视图)findViewById(R.id.solution\u描述);
}
知道为什么TextView字段不能为我呈现吗


谢谢!

为什么不将最后一个
文本视图
替换为?

为什么不将最后一个
文本视图
替换为

将此更改为

android:id="@android:id/list"
自定义ListActivity时,请确保始终将listview id用作@android:id/list

将此更改为

android:id="@android:id/list"
自定义ListActivity时,请确保始终将listview id用作@android:id/list。

尝试以下操作:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load your layout
    setContentView(R.layout.your_layout);

    adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution, R.id.label, problems);

    final TextView solution_title  = (TextView) findViewById(R.id.solution_title);        
    final TextView solution_description = (TextView) findViewById(R.id.solution_description);

     // Bind to our new adapter.
     setListAdapter(adapter);
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.your_布局);
适配器=新阵列适配器(此,R.layout.solution,R.id.label,问题);
最终文本视图解决方案标题=(文本视图)findViewById(R.id.solution\u标题);
最终文本视图解决方案描述=(文本视图)findViewById(R.id.solution\u描述);
//绑定到我们的新适配器。
setListAdapter(适配器);
}
在布局中,您应该更改ListView的id:

<ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
</ListView>

我几乎可以肯定它会起作用:)

PD:为什么要对获取的文本视图使用final?如果你不想更改文本视图的属性或内容,你甚至不需要使用findViewByID获取它们,它们只需要使用setContentView(R.layout.your_layout)呈现。如果你想更改它们(可能添加一个侦听器),我想你不能使用“final”,但我不太确定。

试试这个:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load your layout
    setContentView(R.layout.your_layout);

    adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution, R.id.label, problems);

    final TextView solution_title  = (TextView) findViewById(R.id.solution_title);        
    final TextView solution_description = (TextView) findViewById(R.id.solution_description);

     // Bind to our new adapter.
     setListAdapter(adapter);
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.your_布局);
适配器=新阵列适配器(此,R.layout.solution,R.id.label,问题);
最终文本视图解决方案标题=(文本视图)findViewById(R.id.solution\u标题);
最终文本视图解决方案描述=(文本视图)findViewById(R.id.solution\u描述);
//绑定到我们的新适配器。
setListAdapter(适配器);
}
在布局中,您应该更改ListView的id:

<ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
</ListView>

我几乎可以肯定它会起作用:)

PD:为什么要对获取的文本视图使用final?如果你不想更改文本视图的属性或内容,你甚至不需要使用findViewByID获取它们,它们只需要使用setContentView(R.layout.your_layout)呈现。如果你想更改它们(可能添加一个侦听器),我想你不能使用“final”,但我不能完全肯定。

吉克杜特

你上面提供的代码有很多问题。当然,首先也是最重要的(我知道你已经解决了)正在将id设置为
@android:id/list
。但我确实看到没有人解释为什么必须这样做。
ListActivity
TabActivity
要求您在创建
ListView
TabHost
时使用特殊id(等等)这与其说是活动本身,不如说是它所使用的适配器。如果不这样做,适配器往往会(很快)损坏

现在,关于渲染。有两种可能:a)它无法获取数据;或者b)它对如何渲染数据感到困惑。虽然a)可能是一种可能性,但我不认为这是真正的问题,因为您在这里做的大多数事情都是正确的。事实上,您使用适配器是最有可能的问题。原因如下:

super.onCreate(savedInstanceState);        
adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution, R.id.label, problems);
现在真正的问题来了:ArrayAdapter以一种非常特殊的方式工作。您可以对其进行扩展,但如果不进行扩展,您将坚持其工作方式。您用于创建适配器的调用是:

new ArrayAdapter<Class>(context, rowItemLayout, textViewId, objects);
您使用它的方式将是:

new ArrayAdapter<SolutionTopic>(this, R.layout.list_item, R.id.mylistitem, problems);
这确实是不太坏的。在扩展时还有一些其他的事情要考虑,但是只有当你做了更复杂的事情时。

最后,还有一点额外的帮助。你应该在ActivityLayout文件中创建一个id
android:id/empty
的文本视图。如果你的列表没有数据,它将呈现该框。这将毫无疑问地告诉你它是呈现还是丢失数据

希望这有帮助

模糊逻辑

你上面提供的代码有很多问题。当然,首先也是最重要的(我知道你已经解决了)正在将id设置为
@android:id/list
。但我确实看到没有人解释为什么必须这样做。
ListActivity
TabActivity
要求您在创建
ListView
TabHost
时使用特殊id(等等)这与其说是活动本身,不如说是它所使用的适配器。如果不这样做,适配器往往会(很快)损坏

现在,关于渲染,有两种可能:a)它没有获得数据;或者b)它对如何渲染数据感到困惑。虽然a)可能是一种可能性,但我不认为这是t
    @Override public View getView(final int position, View convertView, 
    final ViewGroup parent)
    {   final SolutionTopic _app = problems.get(position);

        TextView _text;
        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.list_item, null);

            _text = (TextView) convertView.findViewById(R.id.txtAppName);
            convertView.setTag(_text);
        }
        else _text = (TextView) convertView.getTag();

        _text.setText(problems.member);
        return convertView;
    }