我正在尝试创建一个应用程序,其中我们在主要活动中有一个列表视图 在点击任何项目时,应用程序应通过Intent
启动新的Activity应用程序编译并运行,没有错误, 但是在点击主活动中的任何项目时,不会显示新活动。!
这是我的主要活动:
package com.panzer.papers;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
private ListView universityListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
universityListView = (ListView) findViewById( R.id.universityListView );
// Create and populate a List of university names.
ArrayList<String> universityList = new ArrayList<String>();
universityList.addAll(Arrays.asList(getResources().getStringArray(R.array.university_names)));
// Create ArrayAdapter using the university list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, universityList);
// Add more universitys. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Vidarbh University" );
// Set the ArrayAdapter as the ListView's adapter.
universityListView.setAdapter( listAdapter );
universityListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Toast.makeText(getApplicationContext(),
// ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
// selected item
String university = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SelectedUniversity.class);
// sending data to new activity
i.putExtra("university", university);
startActivity(i);
}
});
}
}
以下是我选择的活动的代码(新活动的java类)
package com.panzer.papers;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SelectedUniversity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.simplerow);
TextView txtProduct = (TextView) findViewById(R.id.selectedUniversityName);
Intent i = getIntent();
// getting attached intent data
String university = i.getStringExtra("university");
// displaying selected product name
txtProduct.setText(university);
}
}
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:gravity="center"
android:text="Select University"
android:textSize="25dp"
android:background="#ffffff" />
<ListView
android:id="@+id/universityListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_below="@id/TextView1" />
</RelativeLayout>
以下是我选择的活动的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" >
<TextView android:id="@+id/selectedUniversityName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:padding="10dip" />
</LinearLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.panzer.papers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SelectedUniversity"
android:label="Selected_University_Label">
</activity>
</application>
</manifest>
答案 0 :(得分:1)
您正在将LinearLayout转换为TextView。
要解决此问题,请更改此行: String university =((TextView)view.findViewById(R.id.selectedUniversityName))。getText()。toString();
答案 1 :(得分:1)
你可以改变这一行listAdapter = new ArrayAdapter(this,R.layout.simplerow,universityList);
用这个: listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,universityList);
如果您需要自定义列表视图,可以执行以下操作:
更换行: listAdapter = new ArrayAdapter(this,R.layout.simplerow,universityList);
用: listAdapter = new ArrayAdapter(this,R.layout.simplerow,R.id.selectedUniversityName,universityList);
并更正该行: String university =((TextView)view.findViewById(R.id.selectedUniversityName))。getText()。toString();
如上所述
它是