问题描述
我试图加一个项目,首先通过使用添加按钮,然后将不同的活性,然后又回到原来的和在列表视图添加。我似乎无法有一个以上的项目。
I am trying to add an item, first by using an add button, then going to a different activity, then coming back to the original one and adding it in a listview. I can't seem to have more than one item.
AddScreen.class(我的第一个活动):
AddScreen.class (My first activity):
package com.painLogger;
**IMPORTS**
public class AddScreen extends Activity implements OnClickListener,
OnItemClickListener {
/** Called when the activity is first created. */
Button addButton;
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
String painLevelString, timeOfPainString, textTreatmentString,
painLocation, row1, row2;
TextView painTitle;
boolean myFirstTime;
int k;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addscreen);
// if it's the first time opening the app, then go to 'AddMyInfo.class'
SharedPreferences settings = this.getSharedPreferences("MyApp", 0);
SharedPreferences.Editor e = settings.edit();
boolean firstrun = settings.getBoolean("firstrun", true);
if (firstrun) {
e.putBoolean("firstrun", false);
e.commit();
Intent intent = new Intent(this, AddMyInfo.class);
startActivity(intent);
}
//initialize painTitle and set its font
painTitle = (TextView) findViewById(R.id.painTitle);
Typeface font = Typeface.createFromAsset(getAssets(),
"Chantelli_Antiqua.ttf");
painTitle.setTypeface(font);
listthings = (ListView) findViewById(R.id.listthings);
from = new String[] { "row_1" + painItems.size(),
"row_2" + painItems.size() };
to = new int[] { R.id.row1, R.id.row2 };
adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
from, to);
listthings.setAdapter(adapter);
listthings.setOnItemClickListener(this);
addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(this);
}
@Override
//on the activityresult,get the string extra, then add the item to the list
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(this.getIntent().hasExtra("row1")){
row1 = this.getIntent().getStringExtra("row1");
row2 = this.getIntent().getStringExtra("row2");
painLevelString =
this.getIntent().getStringExtra("com.painLogger.painLevel");
painLocation = this.getIntent().getStringExtra("painLocation");
timeOfPainString =
this.getIntent().getStringExtra("com.painLogger.painTime");
textTreatmentString =
this.getIntent().getStringExtra("com.painLogger.treatment");
addItem();
}
}
// to add the item, put it in the map, and add the map into the list
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1" + painItems.size(), row1);
map.put("row_2" + painItems.size(), row2);
painItems.add(map);
adapter.notifyDataSetChanged();
}
public void onClick(View v) {
// When '+' clicked, go to the PainLoggerActivity.java, where you can
// click enter, and that sends those strings back to here, where I can
// incorporate them into a list view, same as was there in the
// PainLogger Activity
Intent goToFields = new Intent (this, PainLoggerActivity.class);
//put the desired extras into the intent
startActivityForResult(goToFields, 1);
}
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(this, Item1.class);
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime", timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("painLocation", painLocation);
startActivity(intent);
}
}
我的PainLoggerActivity,在这里我输入的各种信息被放入名单:
My PainLoggerActivity, where I enter various info to be put into the list:
包com.painLogger; //进口 公共类PainLoggerActivity扩展活动实现OnClickListener, OnItemClickListener, OnKeyListener {
package com.painLogger; // imports public class PainLoggerActivity extends Activity implements OnClickListener, OnItemClickListener, OnKeyListener {
/** Called when the activity is first created. */
EditText txtItem, txtItem2, timeOfPain, textTreatment, painLevel;
Button btnAdd;
ListView listItems;
ArrayAdapter<String> aa;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
int[] to;
String[] from;
SimpleAdapter adapter;
String timeOfPainString, textTreatmentString, painLevelString, painLocation;
Spinner s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtItem = (EditText) findViewById(R.id.txtItem);
txtItem2 = (EditText) findViewById(R.id.txtItem2);
timeOfPain = (EditText)findViewById(R.id.timeOfPain);
textTreatment = (EditText)findViewById(R.id.textTreatment);
painLevel = (EditText)findViewById(R.id.painLevel);
btnAdd = (Button) findViewById(R.id.btnAdd);
listItems = (ListView) findViewById(R.id.listItems);
btnAdd.setOnClickListener(this);
from = new String[] { "row_1", "row_2" };
to = new int[] { R.id.row1, R.id.row2 };
adapter = new SimpleAdapter(this, painItems,
R.layout.mylistlayout, from, to);
listItems.setAdapter(adapter);
listItems.setOnItemClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<?>adapter = ArrayAdapter.createFromResource(
this, R.array.planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
}
private void addItem() {
timeOfPainString = timeOfPain.getText().toString();
textTreatmentString = textTreatment.getText().toString();
painLevelString = s.getSelectedItem().toString();
painLocation = txtItem.getText().toString();
Intent intent = new Intent (this, AddScreen.class);
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime", timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("painLocation", painLocation);
setResult(1, intent);
finish();
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", txtItem.getText().toString());
map.put("row_2", txtItem2.getText().toString());
painItems.add(map);
adapter.notifyDataSetChanged();
}
public void onClick(View v) {
if (v == this.btnAdd) {
addItem();
}
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
this.addItem();
}
return false;
}
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
timeOfPainString = timeOfPain.getText().toString();
textTreatmentString = textTreatment.getText().toString();
painLevelString = s.getSelectedItem().toString();
painLocation = txtItem.getText().toString();
//create the intent
Intent intent = new Intent (this, Item1.class);
//put the desired extras into the intent
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime", timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("pain location", painLocation);
startActivity(intent);
//set the variables to "", empty
}
}
我的Item1.class只是其中的应用程序那张名单上的项目被点击时。它没有任何其他目的。
My Item1.class is simply where the app goes when an item on the list is clicked. it has no other purpose.
我有问题,因为当我在列表中添加的项目,它取代了previous之一,我只剩下一个项目。
I am having problem because when I add the item in the list, it replaces the previous one, and I am left with only one item.
推荐答案
下面是一个测试原型:
TestResultActivity.java
TestResultActivity.java
package com.arrdude.forumanswer;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class TestResultActivity extends Activity implements OnClickListener {
private static final int REQUEST_LIST_ITEM = 235; //arbitrary request code
static final String ADAPTER_VALUE_1 = "head";
static final String ADAPTER_VALUE_2 = "sub";
ArrayList<HashMap<String, String>> masterlist = null;
ListAdapter adapter = null;
ListView listview = null;
Button addbutton = null;
public ListAdapter getAdapter() {
if(adapter == null){
adapter = new SimpleAdapter(this, getMylist(), R.layout.listitem, new String[] {ADAPTER_VALUE_1, ADAPTER_VALUE_2}, new int[] {R.id.listheading, R.id.listsubheading});
}
return adapter;
}
public ListView getListview() {
if(listview==null){
listview = (ListView) findViewById(R.id.mainlistview);
listview.setAdapter(getAdapter());
}
return listview;
}
public Button getAddbutton() {
if(addbutton==null){
addbutton = (Button) findViewById(R.id.mainaddbutton);
addbutton.setClickable(true);
addbutton.setOnClickListener(this);
}
return addbutton;
}
public ArrayList<HashMap<String, String>> getMylist() {
if(masterlist==null){
masterlist = new ArrayList<HashMap<String,String>>();
}
return masterlist;
}
public void addListItem(String head, String sub){
HashMap<String, String> addme=new HashMap<String, String>();
addme.put(ADAPTER_VALUE_1, head);
addme.put(ADAPTER_VALUE_2, sub);
masterlist.add(addme);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//lazy init objects not the most efficient in Android but easier reading
getListview();
getAddbutton();
addListItem("Test Head", "Test sub heading here below"); //an initial item for testing
}
@Override
public void onClick(View v) {
Intent launchadditemI = new Intent(this, AddItemActivity.class);
startActivityForResult(launchadditemI, REQUEST_LIST_ITEM);
}
@Override
public void onActivityResult(int req, int res, Intent data){
if(req == REQUEST_LIST_ITEM && res == RESULT_OK){
String thishead = data.getStringExtra(ADAPTER_VALUE_1);
String thissub = data.getStringExtra(ADAPTER_VALUE_2);
addListItem(thishead, thissub);
}
}
}
AddItemActivity.java
AddItemActivity.java
package com.arrdude.forumanswer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AddItemActivity extends Activity implements OnClickListener {
EditText headeredittext = null;
EditText subheadedittext = null;
Button donebutton = null;
Button cancelbutton = null;
public EditText getHeaderedittext() {
if(headeredittext==null){
headeredittext = (EditText) findViewById(R.id.headedittext);
}
return headeredittext;
}
public EditText getSubheadedittext() {
if(subheadedittext==null){
subheadedittext = (EditText) findViewById(R.id.subedittext);
}
return subheadedittext;
}
public Button getDonebutton() {
if(donebutton==null){
donebutton = (Button) findViewById(R.id.adddonebutton);
donebutton.setClickable(true);
donebutton.setOnClickListener(this);
}
return donebutton;
}
public Button getCancelbutton() {
if(cancelbutton==null){
cancelbutton = (Button) findViewById(R.id.addcancelbutton);
cancelbutton.setOnClickListener(this);
}
return cancelbutton;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.additem);
//lazy init objects not the most efficient in Android but easier reading
getHeaderedittext();
getSubheadedittext();
getDonebutton();
getCancelbutton();
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.addcancelbutton:
finish();
break;
case R.id.adddonebutton:
sendResult();
break;
default:
break;
}
}
private void sendResult() {
Intent resultI = new Intent();
resultI.putExtra(TestResultActivity.ADAPTER_VALUE_1, getHeaderedittext().getText().toString());
resultI.putExtra(TestResultActivity.ADAPTER_VALUE_2, getSubheadedittext().getText().toString());
setResult(RESULT_OK, resultI);
finish();
}
}
main.xml中
main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/maintitle"/>
<ListView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/mainlistview"></ListView>
<Button android:id="@+id/mainaddbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/addbuttontext"></Button>
</LinearLayout>
additem.xml
additem.xml
<?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">
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/LinearLayout01">
<TextView android:layout_width="wrap_content" android:text="Header:" android:layout_height="wrap_content" android:id="@+id/TextView01"></TextView>
<EditText android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/headedittext" android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearLayout2">
<TextView android:text="Subhead:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/subedittext" android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearLayout1">
<Button android:text="Cancel" android:id="@+id/addcancelbutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Add Item" android:id="@+id/adddonebutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
listitem.xml
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/listheading" android:layout_width="wrap_content"
android:layout_height="26dip" android:layout_alignParentTop="true"
android:textSize="20sp" />
<TextView android:id="@+id/listsubheading"
android:layout_width="wrap_content" android:layout_height="22dip"
android:layout_below="@id/listheading" android:textSize="16sp" />
</RelativeLayout>
的strings.xml
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="maintitle">List Items:</string>
<string name="app_name">TestResultActivity</string>
<string name="addbuttontext">Add New Item</string>
</resources>
AndroidManifest.xml中
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arrdude.forumanswer"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestResultActivity"
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=".AddItemActivity"></activity>
</application>
</manifest>
编程快乐!
这篇关于加入项目的ListView传递信息通过机器人的意图后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!