开发者

Android internal passing of data from ListView to activity asynchronously

I'm trying to learn how list item choices can be passed within the same activity in an Android application. Below, I've got a simple page layout that creates a ListView which is populated by a static string array. I have three other static string arrays listed below that. When the page first loads, it loads up the first array into the ListView. Alot of the code on this page I've cobbled together by reading articles on Android here on stackoverflow, so if I'm doing something dumb, I want to hear about it. My goal is ultimately to have these static string arrays actually populated from an inflated xml file that is synced to the app, but first things first. I'm trying to build my basics first.

So I have a listener set on the item click for the ListView. What I need help with is, how do I capture what item they clicked on, and then recreate the activity with the new string array loaded into it? I saw one example which I emulated and it made the app look like it was actually changing pages everytime. Considering the screen will have some static elements on it (a menu bar), I'd like it to refresh itself independent of the overall layout (I guess the best way to describe it in web terminology would be asynchronously, like with AJAX or jQuery).

The order of events is TEST_1 is the root with one choice. Click on the choice and it loads up TEST_2. If item 1 on TEST_2 is chosen, load TEST_3a, and vice versa with 2, TEST_3b.

Hopefully this makes sense... let me know if I can elaborate further.

public class MyLayout extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setListAdapter(new ArrayAdapter<String>(this, R.layout.listlayout, TEST_1));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

        //What do I do here?
    }
  });
}

static final String[] TEST_1 = new String[] {
        "Members"
      };

static final String[] TEST_2 = new String[] {
    "Joe A. Member", "Paula B. User开发者_运维问答"
  };

static final String[] TEST_3a = new String[] {
    "Personal", "Medical", "Financial"
  };

    static final String[] TEST_3b = new String[] {
    "Personal", "Medical", "Financial"
  };

}


Starting an activity is really easy and straight forward. In an Activity Context call this:

Intent myIntent = new Intent(this, OtherActivity.class); startActivity(myIntent);

What you will want to do to accomplish what you looking for is have three activities that each have the static string array in them. As you click one option it will forward you to the next Activity.

NOTE: Don't forget to place the name of the activity in the manifest.

http://developer.android.com/guide/topics/fundamentals/activities.html

Edit 1: to address the comment

1) Your onItemClickListener is good. The only issue you will have here is that you have to preform checks completely different for each string you want to add to your list.

2 & 3) What you want to do here is create a new adapter for the ListView and feed the adapter the new string array. Once this is done simply call invalidate().

Edit 2: Sample of what you may do

class MyActivity extends ListActivity {
    String[] mContent;

    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Set content view and widgets set up
        mContent = inflateFromXml(R.xml.basexml);
        ListView lv = getListView();
        lv.setAdapter(MyAdapter(this, R.layout.listlayout, mContent);
        // Set up your onItemClickListener()
        // This is quick and dirty
        lv.setOnItemClickListener(new OnItemClickListener() {
            public boolean onItemClick(Adapter<?> a, View v, int pos, long something) {
                 // Now when the user clicks an option, you will want to inflateFromXml(id)
                 // the option they clicked. Then create a new adapter and assign the 
                 // adapter to the list view and do something like
                 ((MyAdapter)lv.getAdapter()).notifyDataSetChanged();
    }

    public String[] inflateFromXml(int id) {
        // Here is where you will inflate your xml list of options
    }

    private class MyAdapter extends ArrayAdapter<String> {
        // ...
    }
}

This is real quick and dirty by it should help. Now inveresly, what you could do instead of reseting the adapter, is have MyAdapter directly reference mContent from MyActivity. This when ever you change mContent call notifyDataSetChanged().


lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String selectedItem = (String) parent.getItemAtPosition(position);

        // then lookup whatever you need to based on that selectedItem (e.g., via HashMap lookup, etc)
        ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
        adapter.clear();
        adapter.addAll(TEST_2); // or whatever the results were of your lookup above ^
        adapter.notifyDataSetInvalidated();
    }
});

See AdapterView#getItemAtPosition(). I'd suggest not hard-coding your lists like what your example shows (TEST_1, TEST_2, etc..), and instead define a logical mapping structure for what you want to occur, and let your UI reflect the cohesiveness of the data structure. As mentioned in the comment above, a good place to start would be HashMap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜