我正在开发一个应用程序,我可以选择"发布",当您按下发布按钮时,您将转到另一个我们有EditText
和{{}的活动{1}},一旦按下Button
,就应该拍摄文字并发布""在Button
。
现在,当我按下ListView
时,一切正常,但列表视图中没有任何内容。
以下是代码:
发表:
Button
应该看到消息的类:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class Post extends ActionBarActivity {
EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
// android:id="@+id/input"
input = (EditText) findViewById(R.id.input);
}
public void addToList(View v) {
Editable text = input.getText();
Intent intent = new Intent();
intent.putExtra("result", text);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
xml文件:
发表:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView list;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
list.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
}
}
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();
int id2 = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id2 == R.id.action_cart) {
startActivityForResult(new Intent(MainActivity.this , Post.class), 1);
}
return super.onOptionsItemSelected(item);
}
}
主:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.ali.test1.Post">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList"
android:layout_below="@id/input"/>
</RelativeLayout>
问题是列表视图中没有显示任何内容 有什么帮助吗?
答案 0 :(得分:0)
在添加和设置结果之前,请尝试检查数据的空值。
if(data != null && data.getStringExtra("result") != null){
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
而不是String result = data.getStringExtra(“result”)尝试这种方式:
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle.getString("result");