嗨,我是新来的,首先我想为我的语言道歉。我想创建我的第一个Android应用程序,并且我正在努力解决一些问题。 MainActivity,它代表我的主屏幕,包含两个按钮和spinner。在微调器用户可以选择一个文本文件,选择后应该触发spinnerActivity(带有打开的文本文件的屏幕)。我发现问题,因为并非每个来自微调器的记录触发活动与文本文件我想要的(我想要一个触发器文件和其他带有空TextView的触发器屏幕)。这是spinnerActivity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView2"
android:layout_width="199dp"
android:layout_height="89dp"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="nie dziala" />
<TextView
android:id="@+id/proximityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.0"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
主要活动代码
package com.example.myapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button click=(Button)findViewById(R.id.button2);
click.setOnClickListener(new View.OnClickListener(){ //nowy actionlistener
@Override
public void onClick(View v) {
Intent activitylauncher=new Intent(MainActivity.this,AboutActivity.class);
//tworzenie nowej intencji Create an intent for a specific component.
startActivity(activitylauncher);
}
});
final Button click2=(Button)findViewById(R.id.button1);
click2.setOnClickListener(new View.OnClickListener() { //nowy actionlistener
@Override
public void onClick(View v) {
Intent activitylauncher=new Intent(MainActivity.this,RandomActivity.class);
//tworzenie nowej intencji Create an intent for a specific component.
startActivity(activitylauncher);
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.recipie_array, android.R.layout.simple_spinner_item);
// Specify the layut to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// String i=Integer.toString(pos);
if(pos==1)
{
Intent intent3 = new Intent(MainActivity.this, SpinnerActivity.class);
intent3.putExtra("txt",(pos));
startActivity(intent3);
}
if(pos==2)
{
Intent intent3 = new Intent(MainActivity.this, SpinnerActivity.class);
startActivity(intent3);
}
if(pos==1)
{
Intent intent3 = new Intent(MainActivity.this, SpinnerActivity.class);
startActivity(intent3);
}
}
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和spinneractivity的代码
package com.example.myapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class SpinnerActivity extends ActionBarActivity implements SensorEventListener {
TextView proxText;
SensorManager sm;
Sensor proxSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
//proximitymeter
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
proxSensor=sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
proxText=(TextView)findViewById(R.id.proximityTextView);
sm.registerListener(this,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
Intent intent3 = getIntent();
int value = intent3.getIntExtra("txt", 0);
//finish();
if (value==1)
{
open("przepis1.txt");
}
if (value==2)
{
open("przepis2.txt");
}
if (value==3)
{
open("przepis3.txt");
}
}
public void open(String name){
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,name);
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader bufor = new BufferedReader(new FileReader(file));
String line;
while ((line = bufor.readLine()) != null) {
text.append(line);
text.append('\n');
}
bufor.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.textView2);
//Set the text
tv.setText(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.spinner, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//proxText.setText(String.valueOf(event.values[0]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
有人可以看看这个并帮我找到错误吗?谢谢
答案 0 :(得分:0)
位置从0开始,我认为问题在于条件, 你可以这样写 -
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Intent intent = new Intent(MainActivity.this, SpinnerActivity.class);
intent.putExtra("txt", pos + 1);
startActivity(intent);
}