【发布时间】:2015-07-13 14:48:09
【问题描述】:
我正在创建一个可以查看多个列表并希望能够搜索列表的应用。我是 java 的初学者,我确信我的代码中有一些错误,但我希望能够对其进行测试,看看我是否可以创建应用程序。如果我发现创建太难,我会放弃这个项目。我在问,因为如果这很容易解决并且我很接近,那么我想看透它。谢谢您的帮助。
主要活动
package com.rocklandrecycles.rcsw.rcswwheredoirecycle;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ImageButton imagebutton1, imagebutton2, imagebutton3, imagebutton4, imagebutton5, imagebutton6;
private EditText SearchBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagebutton1 = (ImageButton)findViewById(R.id.metal_button);
imagebutton2 = (ImageButton) findViewById(R.id.imageButton2);
imagebutton3 = (ImageButton) findViewById(R.id.imageButton3);
imagebutton4 = (ImageButton) findViewById(R.id.imageButton4);
imagebutton5 = (ImageButton) findViewById(R.id.imageButton5);
imagebutton6 = (ImageButton) findViewById(R.id.imageButton6);
SearchBox = (EditText) findViewById(R.id.search_box);
String[] MetalList= {"metal", "metal cans", "aluminum foil", "copper", "steel", "scrap metal",
"hangers", "aluminum can", "knife", "exercise equipment"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
MetalList);
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String MetalPicked = "Please put in Green Bin";
Toast.makeText(MainActivity.this, MetalPicked, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onMetalButtonClick(View view) {
Intent getmetalscreenIntent= new Intent(this,
MetalScreen.class);
final int result = 1;
getmetalscreenIntent.putExtra("callingActivity", "Main Activity");
getmetalscreenIntent.putExtra("MetalActivity" , "MetalList");
startActivityForResult(getmetalscreenIntent, result);
}
}
清单
<application
android:allowBackup="true"
android:icon="@mipmap/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=".MetalScreen"
android:label="MetalScreen"
android:theme="@style/AppTheme"/>
</application>
金属屏风
package com.rocklandrecycles.rcsw.rcswwheredoirecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class MetalScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.metal_layout);
Intent activityThatCalled = getIntent();
String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
String previousActivity2 = activityThatCalled.getExtras().getString("MetalActivity");
ListView callingActivityList = (ListView) findViewById(R.id.metal_list_view);
}
}
Activity_Main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rcswwheredoirecyclethisproject.MainActivity"
android:gravity="top"
android:background="@drawable/recyclesymbol">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/LinearLayout2">
<ImageButton
android:id="@+id/metal_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton1text"
android:onClick="onMetalButtonClick"
android:background="@drawable/metal_texture"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:cropToPadding="true"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton2text"
android:background="@drawable/biohazard"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton3text"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton4text"/>
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton5text"
android:clickable="true"/>
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/content"
android:text="@string/ImageButton6text"
android:clickable="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5"
android:id="@+id/LinearLayout2"
android:layout_below="@+id/LinearLayout3">
<EditText
android:layout_width="200dp"
android:layout_height="150dp"
android:inputType="text"
android:hint="@string/EditText"
android:id="@+id/search_box"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/LinearLayout3"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RCSW Recycle"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Where Do I Recycle This?"
android:textSize="25sp"/>
</LinearLayout>
<requestFocus />
</RelativeLayout>
金属布局
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="left"
android:id="@+id/metal_list_view"
android:padding="10dp">
</ListView>
</LinearLayout>
刺
<resources>
<string name="app_name">RCSW Where Do I Recycle</string>
<string name="content">Image</string>
<string name="menu_settings">Settings</string>
<string name="ImageButton1text">Metal</string>
<string name="ImageButton2text">Hazard</string>
<string name="ImageButton3text">Paper</string>
<string name="ImageButton4text">Glass</string>
<string name="ImageButton5text">Plastic</string>
<string name="ImageButton6text">Other</string>
<string name="EditText">Search Box</string>
<string name="action_settings">RCSWRecycle</string>
</resources>
【问题讨论】:
-
您遇到的错误是什么?您能否添加您的堆栈跟踪。
-
请发布您的错误,并且只发布与错误相关的代码。如果您有堆栈跟踪 / logcat 数据,请同时发布。
-
您是否在主要活动中制作了复制/粘贴问题?你有几行重复,如果你的代码没有编译,可能是因为这个。删除
theListView.setAdapter(theAdapter);后面的 3 行代码 -
是的,我认为这只是一个复制/粘贴错误。我会尝试粘贴错误
-
我遇到的错误是应用程序在我尝试在我的设备上测试它时显示“不幸的是,RCSW 已停止”。
标签: java android xml android-layout