如何将动态生成的数据从屏幕中的一个ListView传输到另一个?

时间:2014-04-18 14:10:47

标签: android

我的数据动态地出现在一组textview中,我在旁边的编辑文本中输入一些数据。

所有数据都在列表视图中。

我需要做的是我需要在按钮点击时将文本视图中的所有数据以及edittexts传输到另一个屏幕。

我将如何实现这一目标?

这是我的代码:

  public class S1 extends ListActivity implements OnClickListener {
  private ProgressDialog pDialog;
  ListView lv;
  Button b;
  String arry1[], category_main;
  int i,key = 0;
  private static String url = "http://ashapurasoftech.com/train/test.json";
  private static final String TAG_a = "menu",TAG_Name = "Name",TAG_Cat = "Category";
  JSONArray items = null;
  ArrayList<HashMap<String, String>> itemList;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table1);

    lv =getListView();
    category_main = "";
    new Getitems().execute(category_main);

    b =(Button) findViewById(R.id.start);
    b.setOnClickListener(this);

    itemList = new ArrayList<HashMap<String, String>>();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

        String name = ((TextView) view.findViewById(R.id.name)).getText().toString();

        Intent in = new Intent(getApplicationContext(),
        SingleContactActivity.class);

        in.putExtra(TAG_Name, name);

        startActivity(in);

        }
    });

}

@Override
public void onClick(View arg0) {

    switch(arg0.getId()){


    case R.id.start: try {
                            onbuttonclick();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;  
    }
}

private void onbuttonclick() throws JSONException {

    Set<String> arry = new HashSet<String>();

    for(int k=0;k<items.length();k++)
    {
        JSONObject c = items.getJSONObject(k);
        String category = c.getString(TAG_Cat);
        arry.add(category);
    }

    arry1 = arry.toArray(new String[arry.size()]);      

    TableRow[] tr = new TableRow[arry1.length];
    TextView[] tx = new TextView[arry1.length];
    TableLayout tl = (TableLayout) findViewById(R.id.tablelayout1);

    for (i = 0; i < arry1.length; i++) {

        final String cat = arry1[i].toString();

        tx[i] = new TextView(S1.this);
        tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tx[i].setText(cat);

        tr[i] = new TableRow(S1.this);
        tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        tl.addView(tr[i],new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));   

        tx[i].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                category_main = cat;

                if (key==0)
                {
                    key=1;
                    lv.setVisibility(View.VISIBLE);
                }
                else if(key==1)
                {
                    key=0;
                    lv.setVisibility(View.GONE);
                }

                new Getitems().execute(category_main);
            }
        });

    }

    b.setVisibility(View.GONE);     
}
private class Getitems extends AsyncTask<Void, Void, Void> {

    String cat12 = "";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(S1.this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(true);
        pDialog.show();
    }

    public void execute(String categorymain) {

        cat12 = categorymain;
        execute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler sh = new ServiceHandler();
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                items = jsonObj.getJSONArray(TAG_a);

                for (i = 0; i < items.length(); i++) {

                    final JSONObject c = items.getJSONObject(i);                        String cate12 = c.getString(TAG_Cat);
                    String name = c.getString(TAG_Name);


                    final HashMap<String, String> item = new HashMap<String, String>();


                    if(cat12.equalsIgnoreCase(cate12))
                    {
                        item.put(TAG_Name,name);
                        itemList.add(item);
                    }



                }
            }
                catch (JSONException e) {
                e.printStackTrace();
                } finally {
                }}
        else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        if (pDialog.isShowing())
            pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(
        S1.this, itemList,
        R.layout.textview, new String[] {TAG_Name},new int[] { R.id.name});

        setListAdapter(adapter);



    }
}

}

0 个答案:

没有答案