我无法访问listview中的所有孩子(eclipse)

时间:2016-05-30 18:12:08

标签: android listview

使用Log.i("Number Children", Integer.toString(lista.getChildCount()));我得到2,但事实上我有4个孩子。这使我的生活变得复杂,因为我必须实时编辑两张图表而且我无法访问所有孩子。

这是我创建列表的代码:

@Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState){
        // TODO Auto-generated method stub

        //interface
        final View view=inflater.inflate(R.layout.layout_monitoramento, container,false);       

        //lista
        lista = (ListView)view.findViewById(R.id.listViewmonitoramento);                    
        String[] de = {"nome", "potencia", "custo"}; 
        int[] para = { R.id.nome_corrente, R.id.consumo_potencia, R.id.consumo_custo};              

        SimpleAdapter adapter = new SimpleAdapter(getContext(), listarCorrentes(), R.layout.modelo_lista_correntes, de, para);

        //adaptador de string para reles                
        lista.setAdapter(adapter);

        lista.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {     
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "adfngfnfbdvs", Toast.LENGTH_SHORT).show();

                //here I create my graphs
                gerarGraficos(lista);

            }
        });



        super.onCreate(savedInstanceState);

        final LinearLayout mainLayout = (LinearLayout) inflater.inflate(R.layout.layout_monitoramento, null); 
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                //Remove the listener before proceeding
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                /////////HERE, return a wrong number of children///////////////             
                Log.i("Number Children", Integer.toString(lista.getChildCount()));
                gerarGraficos(lista);

            }
        }); 

        return(view);

    }

这是我的xml界面项listView:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginBottom="50dp" 
    android:onClick="showDatePickerDialog"> 
            <TextView 
               android:id="@+id/nome_corrente"
               android:text="Sendor Corrente 1" 
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:layout_marginBottom="10dp"
               android:gravity="center"
               android:textAppearance="?android:attr/textAppearanceLarge"/>          
            <TextView 
                android:id="@+id/consumo_potencia"
                android:text="Potencia consumida: " 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" />
            <TextView 
                android:id="@+id/consumo_custo"
                android:text="Valor estimado: R$ " 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" />
            <com.jjoe64.graphview.GraphView
                android:layout_width="match_parent"
                android:layout_height="200dip"
                android:id="@+id/graph_diario" />
            <com.jjoe64.graphview.GraphView
                android:layout_width="match_parent"
                android:layout_height="200dip"
                android:id="@+id/graph_mensal" />
</LinearLayout>

interface listView

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:onClick="showDatePickerDialog"
        android:background="#ffffff" >
        <TextView
            android:id="@+id/data"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="27/05/2016" 
            android:layout_marginBottom="10dp"
            android:gravity="right"
            android:onClick="showDatePickerDialog"  
        />  
        <ListView
            android:id="@+id/listViewmonitoramento"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true" >
        </ListView>
</LinearLayout>

这是我班上的其余部分,在我的适配器上构建itens:

    public class Monitoramento extends Fragment {


    ListView lista;
    private List<Map<String, Object>> correntes;



    private List<Map<String, Object>> listarCorrentes() { 
        correntes = new ArrayList<Map<String,Object>>();

        Map<String, Object> item;
        item = new HashMap<String, Object>(); 

        item.put("nome", "sensor de corrente 0"); 
        item.put("potencia","Potencia consumida hoje: 123 W"); 
        item.put("custo","Valor estimado: R$ 90,00");       
        correntes.add(item);

        item = new HashMap<String, Object>();
        item.put("nome", "sensor de corrente 22"); 
        item.put("potencia","Potencia consumida hoje: 162 W"); 
        item.put("custo","Valor estimado: R$ 122,00");      
        correntes.add(item);

        item = new HashMap<String, Object>();
        item.put("nome", "sensor de corrente 1"); 
        item.put("potencia","Potencia consumida hoje: 100 W"); 
        item.put("custo","Valor estimado: R$ 72,00");       
        correntes.add(item);

        item = new HashMap<String, Object>();
        item.put("nome", "sensor de corrente 2"); 
        item.put("potencia","Potencia consumida hoje: 16 W"); 
        item.put("custo","Valor estimado: R$ 12,00");       
        correntes.add(item);


        return correntes;
    }
////method onCreateView here///////
    }

2 个答案:

答案 0 :(得分:0)

当你尝试通过list.getChildCount获取项目(而不是 childrens )时,你做错了事! ListView有一个适配器,用于管理/填充数据并将此数据设置为当前适配器位置的当前项。这个过程并不像我在现实生活中所描述的那么简单。因此,为了获得物品数量,您应该使用:

yourLstView.getAdapter().getCount()

我想,你应该访问Developers Android并阅读更多内容。

对于需要修改数据的情况,如您所说,要在textView和GraphView中更改某些值,您应该使用适配器。管理数据并将其设置为视图 - 确切地说,适配器在做什么。 所以,你有一些数据的集合,你想在text / graphView中显示,无论。创建定制适配器。将此数据传递给适配器,并在 getView 方法中将此数据设置为所需的字段。 以下是begginers how to implement adapter with ViewHolder pattern的示例。这是正确的方法,要解决您的问题,请确保

如果你感到困惑,请尝试谷歌问题。

如果我的帖子对你有好处的话,请尽量回答我的答案。

答案 1 :(得分:0)

我想要一个循环,它遍历我列表中的所有项目,修改它们具有的TextView和图形 我在这里的代码是这样的:

  for (int i = 0; i < lista.getAdapter().getCount(); i++) {             
            GraphView graph1 = new GraphView(lista.getContext()); 
            graph1=(GraphView)lista.findViewById(R.id.graph_diario);
            LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
                    new DataPoint(0, -1),
                    new DataPoint(1, 5),
                    new DataPoint(2, 3),
                    new DataPoint(3, 2),
                    new DataPoint(4, 10)
            });
        }