开始我是一个初学者,这个android编程。ListView不被改变
我的应用程序在TabHost中定义了2个活动,因此选项卡1和选项卡2 在选项卡1中,我收到了一个EditText输入,我想在ListView中的选项卡2(活动2)中显示。
为此,我创建了一个自定义的ListView本教程:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
现在: 我传递的EditText值与标签1的意图的帮助字符串,以选项卡2.
//Intent to tab 2
Intent intent = new Intent(getBaseContext(), Tab2Activity.class);
intent.putExtra("values", streetName);
startActivity(intent);
在我的标签2(Tab2Activity)我收到这个通知是这样的:
public void onResume(){
super.onResume();
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
updateData(bundle);
}
}
public void updateData (Bundle bundle) {
String data = bundle.getString("values");
Toast.makeText(this, data, Toast.LENGTH_LONG).show();
Bostad bostad_data[] = new Bostad[]{new Bostad (streetName, money)};
adapter = new BostadAdapter(this, R.layout.listview_item_row, bostad_data);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
...
}
我有两个问题(两个问题)。我知道,因为我在Tab1中调用“startActivity(intent)”,这会打开活动,而不是在tab中。是否可以切换选项卡并更新listView?(这不是必需的。如果这是一个简单的解决方案)
2)我得到了正确的信息在我的屏幕上的活动,但Tab2中的listView没有更新与此信息。我该如何解决这个问题? 我的意思是,一个行不被添加到列表视图我在做什么错
感谢所有帮助
编辑:?!添加次也可以在Activity2中创建onCreate。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab2);
//Create listView and connect to the .xml (activity_tab2)
listView = (ListView) findViewById(R.id.listView1);
//Arrayen which the list will receive values from
Bostad bostad_data[] = new Bostad[]{
new Bostad("", "")
};
adapter = new BostadAdapter(this, R.layout.listview_item_row, bostad_data);
View header = getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView.addHeaderView(header);
listView.setAdapter(adapter);
...
}
编辑2:添加BostadAdapter.java
public class BostadAdapter extends ArrayAdapter<Bostad> {
Context context;
int layoutResourceId;
Bostad data[] = null;
public BostadAdapter(Context context, int layoutResourceId, Bostad[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
BostadHolder holder = null;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new BostadHolder();
holder.gatunamn = (TextView)row.findViewById(R.id.gatunamn);
holder.totalkostnad = (TextView)row.findViewById(R.id.totalkostnad);
row.setTag(holder);
}
else {
holder = (BostadHolder)row.getTag();
}
Bostad bostad = data[position];
holder.gatunamn.setText(bostad.gatunamn);
holder.totalkostnad.setText(bostad.totalkostnad+" SEK per månad");
return row;
}
static class BostadHolder
{
TextView gatunamn, totalkostnad;
}
}
编辑3:添加Bostad.java
public class Bostad {
String gatunamn, totalkostnad;
public Bostad(){
super();
}
public Bostad (String gatunamn, String totalkostnad) {
super();
this.gatunamn = gatunamn;
this.totalkostnad = totalkostnad;
}
}
请将您的代码发布到BostadAdapter –
我在OP中添加了它:) – nikedabike