列表视图应在单击列表项后显示文本视图

时间:2014-09-26 11:52:00

标签: android list listview textview

作为初学者,我正在尝试开发一个简单的睡前故事应用程序(Android上第一个大约10到30个故事。

我们的想法是拥有一个列表视图,其中每个列表项都显示故事的标题。 - 我不知道如何动态添加一个特定的标题(想想最好的方法是从.txt文件的标题),所以每个列表项都成为名称" story" + i(我为计算的列表项目。)

在下一步中 - 如果点击一个项目(故事) - 应用程序应该只显示一个简单的文本视图,可以阅读全文(大约200个单词)。

正如您所看到的,我从网上获取故事文本的想法是在.txt文件中编写故事,输入assests顺序然后通过fileInpitStream读取它。

文本文件是以第一个和#34; story1.txt" " story2.txt"等等。

所以我最大的问题是如何将每个列表项与以下匹配的文本文件结合起来" - .txt"何时点击列表项?

如果您需要任何进一步的信息(XML文件 - 但主要是标准的 - 或其他),请告诉我。我非常感谢每一个答案,帮助我实现我的小想法!

MainActivity:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


List valueList = new ArrayList<String>();

//filling List Items
for(int i=1;i <30; i++){
    valueList.add("Story " +i);
}

//List Adapter
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),
        R.layout.custom_textview, valueList);  
final ListView lv = (ListView)findViewById(R.id.listView1);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3)
{
Intent intent = new Intent();
intent.setClassName(getPackageName(), getPackageName()+".ListViewShow");
intent.putExtra("selected", lv.getAdapter().getItem(arg2).toString());
startActivity(intent);
}
});

}
}

ListViewShow(从listview到textview的组合应该是):

public class ListViewShow extends Activity {

private static final String TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.textview_show);
      AssetManager assetManager = getResources().getAssets();
      InputStream inputStream = null;

          try {
              inputStream = assetManager.open("test.txt");
                  if ( inputStream != null)
                      Log.d(TAG, "It worked!");
              } catch (IOException e) {
                  e.printStackTrace();
              }


}}

LogCat错误消息:

&#13;
&#13;
	09-27 17:37:56.797: W/dalvikvm(5622): threadid=1: thread exiting with uncaught exception (group=0x4163eba8)
	09-27 17:37:56.827: E/AndroidRuntime(5622): FATAL EXCEPTION: main
	09-27 17:37:56.827: E/AndroidRuntime(5622): Process: fm.de.geschichten, PID: 5622
	09-27 17:37:56.827: E/AndroidRuntime(5622): android.content.ActivityNotFoundException: Unable to find explicit activity class {fm.de.geschichten/fm.de.geschichten.ListViewShow}; have you declared this activity in your AndroidManifest.xml?
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.Activity.startActivityForResult(Activity.java:3424)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.Activity.startActivityForResult(Activity.java:3385)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:840)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.Activity.startActivity(Activity.java:3627)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.Activity.startActivity(Activity.java:3595)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at fm.de.geschichten.MainActivity$1.onItemClick(MainActivity.java:47)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.widget.AdapterView.performItemClick(AdapterView.java:299)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.widget.AbsListView$3.run(AbsListView.java:3645)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.os.Handler.handleCallback(Handler.java:733)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.os.Handler.dispatchMessage(Handler.java:95)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.os.Looper.loop(Looper.java:136)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at android.app.ActivityThread.main(ActivityThread.java:5001)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at java.lang.reflect.Method.invokeNative(Native Method)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at java.lang.reflect.Method.invoke(Method.java:515)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
	09-27 17:37:56.827: E/AndroidRuntime(5622): 	at dalvik.system.NativeStart.main(Native Method)
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

错误说:

ActivityNotFoundException: Unable to find explicit activity class {fm.de.geschichten/fm.de.geschichten.ListViewShow}; have you declared this activity in your AndroidManifest.xml?

因此,请确保您已在清单文件中添加了活动ListViewShow.java以及Application标记下的其他活动。