Android listview中没有显示任何内容
我正在开发一个应用程序,在这个应用程序中,我可以选择发布,当你按下“发布”按钮时,你会转到另一个活动,其中有一个EditText和一个按钮,一旦按下按钮,文本就会被提取并发布到列表视图的提要页面中 现在,当我按下按钮时,一切正常,但listview中没有显示任何内容 代码如下: 职位: 应在其中查看消息的类:Android listview中没有显示任何内容,android,Android,我正在开发一个应用程序,在这个应用程序中,我可以选择发布,当你按下“发布”按钮时,你会转到另一个活动,其中有一个EditText和一个按钮,一旦按下按钮,文本就会被提取并发布到列表视图的提要页面中 现在,当我按下按钮时,一切正常,但listview中没有显示任何内容 代码如下: 职位: 应在其中查看消息的类: package com.example.ali.test1; import android.app.Activity; import android.content.Intent; im
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);
}
}
xml文件:
职位:
主要内容:
问题是listview中没有显示任何内容
有什么帮助吗?在添加和设置结果之前,请尝试检查数据是否为空值
if(data != null && data.getStringExtra("result") != null){
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
请尝试以下方法,而不是String result=data.getStringExtraresult:
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle.getString("result");
请发布您的logcat错误跟踪。如果没有错误,则有点难以确定问题所在。请编辑并发布与崩溃对应的LogCat错误。我可以在android studio中找到LogCat错误跟踪吗?logcat窗口中有数百万行,当应用程序崩溃时,它将以红色显示。好的,这可能会重复,并停止崩溃,但listview中没有显示任何帮助?谢谢。当您直接添加到适配器时,正如您通过adapter.addresult所做的那样,这会自动调用notifyDataSetChanged函数,因此请尝试删除notify函数。如果不起作用,请尝试使用list.addresult,然后调用list.getAdapter.notifyDataSetChanged。你的适配器也应该有一个函数getView,我建议你在上面的问题中也发布这个函数。他正在使用默认适配器,它已经有一个getView方法。他不能重写它吗?
<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.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
</RelativeLayout>
if(data != null && data.getStringExtra("result") != null){
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle.getString("result");