无法在片段视图中的Listview中添加多行

时间:2014-10-25 20:05:01

标签: android json listview android-fragments

我有一个包含JSON数据的ListFragment。当我单击列表项时,数据将被发送到Fragment(DetalhesFragment.java)。如果用户确认数据,它将点击DetalhesFragment中的一个按钮,将这些信息发送到主要活动右侧的另一个列表(PedidosFragment.java)。

它正在工作!来自中间片段的数据显示在PedidosFragment列表视图的第一行。但是,如果我尝试选择另一个项目发送,那么该行将被新数据覆盖..不要在第一行下添加一行。

伙计,你能帮助我吗?这里,代码..

DetalhesFragment.java

package com.example.waitersoriginal;

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;

public class DetalhesFragment extends Fragment implements OnClickListener{

    TextView nomeEntrada,descrEntrada,valorEntrada,nmArm,dsArm,vlArm;
    String txtNome, txtDescr;
    View view;

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

        view = inflater.inflate(R.layout.detalhes_fragment, container, false);

        Button mButton = (Button) view.findViewById(R.id.button1);

        nomeEntrada= (TextView) view.findViewById(R.id.textView1);
        descrEntrada= (TextView)view.findViewById(R.id.textView2);
        valorEntrada= (TextView)view.findViewById(R.id.textView3);

        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 switch (v.getId()) {
                    case R.id.button1:

                        PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3); 
                        array.criaArray(txtNome,txtDescr);

                        break;
                 }
            }
        }
            );
        return view;
    }    

    public void change(String txt, String txt1){
        nomeEntrada.setText(txt);
        descrEntrada.setText(txt1);
        txtNome = txt;
        txtDescr = txt1;
        //valorEntrada.setText(txt2);

    }  

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }    
}

PedidosFragment.java

package com.example.waitersoriginal;

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

import android.app.Fragment;
import android.os.Bundle;
import android.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class PedidosFragment extends ListFragment{

    private ArrayList<HashMap<String, String>> listaPedidos;

    private static final String TAG_NM = "NOME";
    private static final String TAG_DS = "DESCR";

    public PedidosFragment(){}

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

    void criaArray(String nomeProd, String descrProd) {

        listaPedidos = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map3 = new HashMap<String, String>();

        ListAdapter adapter2 = new SimpleAdapter(getActivity(), listaPedidos,
                R.layout.list_item, 
                new String[] { TAG_NM, TAG_DS}, 
                new int[] { R.id.name, R.id.pid});

        setListAdapter(adapter2);

        map3.put(TAG_NM, nomeProd);
        map3.put(TAG_DS, descrProd);

        listaPedidos.add(map3);    

            ((BaseAdapter) adapter2).notifyDataSetChanged();
    }   
}

谢谢..!

1 个答案:

答案 0 :(得分:0)

每次使用最新项目覆盖适配器并每次都创建一个新适配器。

尝试这个(假设两个片段都在内存中):

public class PedidosFragment extends ListFragment{

    private ArrayList<HashMap<String, String>> listaPedidos;

    private static final String TAG_NM = "NOME";
    private static final String TAG_DS = "DESCR";



    public PedidosFragment(){}

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

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

         ListAdapter adapter2 = new SimpleAdapter(getActivity(), listaPedidos,
                R.layout.list_item, 
                new String[] { TAG_NM, TAG_DS}, 
                new int[] { R.id.name, R.id.pid});

         setListAdapter(adapter2);


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

    void criaArray(String nomeProd, String descrProd) {

        HashMap<String, String> map3 = new HashMap<String, String>();
        map3.put(TAG_NM, nomeProd);
        map3.put(TAG_DS, descrProd);

        listaPedidos.add(map3);    

        ((BaseAdapter) adapter2).notifyDataSetChanged();
    }   
}

另外,请避免将constructorsfragments一起使用,而应使用Bundles

希望这有帮助