【问题标题】:ListView not being changedListView 没有被改变
【发布时间】: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


【解决方案1】:

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

一种快速解决方法可能是访问选项卡主机并将现有的选项卡 2 活动替换为您刚刚创建的具有数据的活动。

【讨论】:

  • 嗯,不是真的。我真正想要做的是更新选项卡 2 中的列表视图。我在这里真正要做的是保存文件,我希望我保存的每个文件都添加到选项卡 2 中的列表视图中。我正在传递一个带有文件名(街道名称)的意图并尝试将其添加到列表视图中。
  • 现在实现这种结构的最常见方法是创建一个包含 ViewPager、一些标签栏机制(您自己的或 ActionBar 实现)和片段作为页面的单个 Activity在 ViewPager 中。在该模型中,您可以使用侦听器通知 Activity Tab1 做了某事并向其传递数据,然后 Activity 可以用数据通知 Tab2 并更新显示更改的页面。如果您想使用 Activity 并通过 Intent 传递数据,则需要确保该 Activity 正在替换没有数据的旧 Activity。
  • startActivity 将启动一个新的 Activity,它不会更新旧的。理论上,您可以将选项卡 2 中的活动替换为您刚刚创建的新活动,而不是调用 startActivity,但我不推荐该路径。
  • 此链接可能对您有所帮助stackoverflow.com/questions/25096646/…
  • 嗯,好吧。但我现在的情况是我正在使用 tabHost。如果不重新设计整个结构,我想要实现的目标是不可能的吗?它基于 TabHost,每个选项卡都通过一个活动进行修改。
猜你喜欢
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 2011-11-15
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多