ListView没有被更改(ListView not being changed)

编程入门 行业动态 更新时间:2024-10-22 10:46:19
ListView没有被更改(ListView not being changed)

首先,我是这个Android编程的初学者。

我的应用程序在TabHost中定义了2个活动,因此选项卡1和选项卡2.在选项卡1中,我收到一个EditText输入,我想在ListView中的选项卡2(活动2)中显示。

为此,我使用本教程创建了一个Custom ListView: http : //www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

现在:我在Edit 1的帮助下将EditText值作为String传递给选项卡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; } }

To start off I'm a beginner with this android programming.

My app has 2 activites defined in TabHost, hence Tab 1 and Tab 2. In Tab 1 I receive an EditText input that I would like to show in tab 2 (activity 2) in a ListView.

For this I created a Custom ListView with this tutorial: http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter

Now: I pass the EditText value as a String with the help of an intent in tab 1, to tab 2.

//Intent to tab 2 Intent intent = new Intent(getBaseContext(), Tab2Activity.class); intent.putExtra("values", streetName); startActivity(intent);

In my Tab 2 (Tab2Activity) I receive this intent like this:

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(); ... }

I have two question (two problems).

1) I know that since I call "startActivity(intent) in Tab1 this will open up the activity and not in the tab. Is it possible to just switch tab and update the listView? (This isn't necessary. Only if it's an easy solution)

2) I get the activity with the right info up on my screen, but the listView in Tab2 isn't updated with this information. How can I fix this? What I mean is, a row doesn't get added to the listview. What am I doing wrong?

Thanks for any help!

EDIT: Adding the onCreate in Activity2 as well.

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); ... }

Edit 2: Adding 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; } }

Edit 3: Adding Bostad.java

public class Bostad { String gatunamn, totalkostnad; public Bostad (){ super(); } public Bostad (String gatunamn, String totalkostnad) { super(); this.gatunamn = gatunamn; this.totalkostnad = totalkostnad; } }

最满意答案

如果我理解得当,我认为你正在做的是创建一个看起来与tab 2相同但不是同一个对象的新活动。 因此,您在startActivity()中启动的活动是唯一获取数据的活动。 我建议使用带有片段和接口的ViewPager来传递/更新父活动中的数据。 这可能有点过于先进,并不是你可以轻易写出来的答案,但是有很多关于这个过程的教程。 当你刚刚开始时,你可能想要探索更快的选择,但这就是我要做的。

快速修复可能是访问选项卡主机并将现有选项卡2活动替换为您刚刚创建的具有数据的活动。

If I understand properly, I think what you are doing is creating a new activity that looks identical to tab 2 but isn't the same object. So that activity you start in startActivity() is the only one that gets the data. What I would suggest is using a ViewPager with fragments and interfaces to pass/update data from the parent activity. That may be a bit too advanced yet and it's not something you can easily write out as an answer, however there are a lot of tutorials on the process. As you're just starting out you may want to explore faster options but it is what I'd do.

A quick fix might be to access the tab host and replace the existing tab 2 activity with the activity you just made that has the data.

更多推荐

listView,Tab,row,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-07-31 01:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ListView   changed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!