【发布时间】:2015-03-11 20:39:33
【问题描述】:
首先,我是这个 android 编程的初学者。
我的应用在 TabHost 中定义了 2 个活动,因此是 Tab 1 和 Tab 2。 在选项卡 1 中,我收到一个 EditText 输入,我想在 ListView 的选项卡 2(活动 2)中显示该输入。
为此,我使用本教程创建了一个自定义 ListView:http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
现在: 我借助选项卡 1 中的意图将 EditText 值作为字符串传递给选项卡 2。
//Intent to tab 2
Intent intent = new Intent(getBaseContext(), Tab2Activity.class);
intent.putExtra("values", streetName);
startActivity(intent);
在我的 Tab 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();
...
}
我有两个问题(两个问题)。
1) 我知道,因为我在 Tab1 中调用“startActivity(intent)”,这将打开活动而不是在选项卡中。是否可以只切换选项卡并更新 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 中添加了它 :)
标签: android listview android-intent android-listview tabs