【问题标题】:Getting a False return from listview从 listview 获得 False 返回
【发布时间】:2012-02-14 14:05:45
【问题描述】:

在框架布局/选项卡布局中实现列表视图时,我得到了错误的回报。对于我的生活,我似乎无法弄清楚为什么在实现列表视图时我的应用程序会得到错误的回报。该代码适用于外部项目,但我似乎无法将其集成到这个项目中。我将列出项目中使用的主要活动组以及字符串要出现的页面的 xml 文件。抱歉,它有很多代码,但这在过去几天里一直让我发疯,因为它应该相对简单。

xml 布局文件保存在 main.xml 中的 framelayout 编码中(未显示),所以在引用时我的 ID 是否错误?解决这个问题的任何帮助都会令人惊叹,因为我知道这是一个非常简单的解决方案,我只是想念它。谢谢大家。

如果我没有很好地解释它,错误的图片如下 在此处查看页面http://i41.tinypic.com/do04ee.png

它们没有日志错误。

主要活动 (请注意,我已删除代码以使其更加相关和清晰,并显示活动之间的链接)

package workout.fitty;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class WorkoutFittyActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

 // More

    **intent = new Intent().setClass(this, MoreActivity.class);
    spec = tabHost.newTabSpec("songs").setIndicator("More",
                      res.getDrawable(R.drawable.tab_more))
                  .setContent(intent);
    tabHost.addTab(spec);**

    tabHost.setCurrentTab(1);
}
}

更多活动

package workout.fitty;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MoreActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.more_layout);
    Intent Intent = new Intent(this, MyListActivity.class);
}
}

MyListActivity

package workout.fitty;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

    public class MyListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "BMI", "Body Measurements", "Logs",
            "Feedback", "How To Use", "About" };
    // Use your own layout
    **ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.more_layout, R.id.label, values);
    setListAdapter(adapter);**
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}

}

还有 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" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="22px"
    android:layout_height="22px"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:layout_marginTop="4px"
    android:src="@drawable/ic_launcher" >
</ImageView>

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

</LinearLayout>

带有容器和框架布局的主 XML 文件

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">

        <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" />
</LinearLayout>

</TabHost>

【问题讨论】:

  • 您到底想在MoreActivity 活动中做什么?在MoreActivity 活动的onCreate() 中,您创建了一个Intent,但实际上您并没有通过调用startActivity(Intent)startActivityForResult(Intent, int)(或者您只是删除这部分)来启动另一个活动?跨度>
  • 嗨,Slukian,非常感谢。我不知道我想用 MoreActivity 做什么。我什至没有注意到我不需要它。它是你总是想念的简单的东西哈!它现在已排序并且列表正在运行。我只需要删除 MoreActivity。非常感谢。我觉得自己像个白痴,没有注意到这样的事情。

标签: android listview android-activity android-framelayout


【解决方案1】:

改变你的:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.more_layout, R.id.label, values);

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
       R.layout.more_layout, values);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2021-11-13
    • 1970-01-01
    • 2020-03-11
    • 2015-07-15
    • 1970-01-01
    相关资源
    最近更新 更多