在Android环境中实现onButtonClick的最佳位置

时间:2014-10-24 04:11:43

标签: java android json listview fragment

我正在使用包含三个片段的Activity:ListFragment,DetailsFragment和OrderFragment。这个想法是:

- 从MySQL读取一个JSON文件,并从ListFragment填充List。 (工作)

- onClickItem将向DetailsFragment发送之前点击的项目的详细信息。 (工作)

- 最后,会有一个按钮,用于将所选项目(名称和描述)中的String值发送到OrderFragment中的Listview。 (不知道是否有效,这是我的问题)

我在ListFragment中创建了“添加到订单”智能,但我无法实现它的最佳实施位置。我甚至无法验证我的逻辑是否正常工作。

请考虑我的代码只是为了更好地了解我的项目:

ListaFragment.java

package com.example.waitersoriginal;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListaFragment extends ListFragment {

private ProgressDialog pDialog;

 private static final String URL_SELECT ="http://192.168.0.3/waiters/select_bebidas.php";   

//JSONTAGS
private static final String TAG_NOME = "NOME";
private static final String TAG_BEBIDAS = "bebidas";
private static final String TAG_DESCR = "DESCR";

public String txt1;
public String txt2;

private JSONArray mProdutos = null;
private ArrayList<HashMap<String, String>> mProdutosList;
private ArrayList<HashMap<String, String>> mPedidosList;


public ListaFragment(){}

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{ 
  return inflater.inflate(R.layout.lista_fragment, container, false); 
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //loading the comments via AsyncTask
    new LoadEvents().execute();

  }
  public void updateJSONdata() {

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


    JSONParser jParser = new JSONParser();

    JSONObject json = jParser.getJSONFromUrl(URL_SELECT);

    try {


        mProdutos = json.getJSONArray(TAG_BEBIDAS);


        for (int i = 0; i < mProdutos.length(); i++) {
            JSONObject c = mProdutos.getJSONObject(i);

            String nome = c.getString(TAG_NOME);
            String descr = c.getString(TAG_DESCR);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_NOME, nome);
            map.put(TAG_DESCR, descr);

            mProdutosList.add(map);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
private void updateList() {

    mPedidosList = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map2 = new HashMap<String, String>();

ListAdapter adapter = new SimpleAdapter(getActivity(), mProdutosList,
        R.layout.list_item, 
        new String[] { TAG_NOME, TAG_DESCR}, 
        new int[] { R.id.name, R.id.pid});

            setListAdapter(adapter);

            ListView lv = getListView();    
            lv.setOnItemClickListener(new OnItemClickListener() {

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

                    DetalhesFragment txt = (DetalhesFragment)getFragmentManager().findFragmentById(R.id.fragment2);

                    txt.change(mProdutosList.get(position).get(TAG_NOME).toString(),mProdutosList.get(position).get(TAG_DESCR).toString());
                    //txt.change(Entradas[position],Descricao[position],"Preço: "+Valor[position]);
                    //txt.change(AndroidOS[position]);
                    getListView().setSelector(android.R.color.transparent);

                    txt1 = mProdutosList.get(position).get(TAG_NOME).toString();
                    txt2 = mProdutosList.get(position).get(TAG_DESCR).toString();

                    }            
                });
}

public class LoadEvents extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Carregando cardápio, aguarde por favor!");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... arg0) {

        updateJSONdata();
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();

        updateList();
        }
      }

public void btnAddOnClick(String nmProd, String dsProd){

    PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3); 
    array.criaArray(txt1,txt2);

}

PedidosFragment.java(或OrderFragment):

package com.example.waitersoriginal;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PedidosFragment extends Fragment{

    String[] pedidos;
    String[] descr;
    private ListView lv;
    private View myFragmentView;

    public PedidosFragment(){}

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
    { 

        myFragmentView = inflater.inflate(R.layout.pedidos_fragment, container, false);
        return myFragmentView;
        //return inflater.inflate(R.layout.pedidos_fragment, container, false); 
    }

    public void criaArray(String nomeProd, String descrProd)
    {
        for (int i = 0; i < pedidos.length; i++) {
            String concat = nomeProd+descrProd;
            concat = pedidos[i];

            lv = (ListView) myFragmentView.findViewById(R.id.listview2);
            ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, pedidos);

        }

    }

}

我不确切地知道我的逻辑是否可以接受,对此感到抱歉。

先谢谢你们。

0 个答案:

没有答案