相同的文本一遍又一遍地显示在ListView中

时间:2018-11-23 14:08:47

标签: android android-listview

我在ListView中当前使用的textView会一遍又一遍地显示,而不会更改其信息,但是我看不到为什么,我能否得到一点帮助?代码:

名为Principal.java的主代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

public class Principal extends AppCompatActivity {

ListView lista;

String[][]datos = {
        {"Mediciones del día"},
        {" "},
        {"Promedios del día"},
};

int [] datosImg = {R.drawable.celeste, R.drawable.rojo, R.drawable.verde};

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

    lista = (ListView) findViewById(R.id.lvLista);

    lista.setAdapter(new Adaptador(this, datos, datosImg));
  }
}

我的适配器:

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adaptador extends BaseAdapter {

private static LayoutInflater inflater = null;

Context contexto;
String [][] datos;
int[] datosImg;

    public Adaptador (Context context, String [][] datos,int []imagenes)
{
    this.contexto = context;
    this.datos = datos;
    this.datosImg = imagenes;
    inflater = 
(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int i, View convertView, ViewGroup parent) {

    final View vista = inflater.inflate(R.layout.elemento_lista, null);
    TextView titulo = (TextView) vista.findViewById(R.id.textView);
    ImageView imagen = (ImageView) vista.findViewById(R.id.imageView);

    titulo.setText(datos[0][0]);
    imagen.setImageResource(datosImg[i]);
    /*
    imagen.setTag(i);
    */
    /*
    imagen.setOnClickListener(new View.OnClickListener(){
      @Override
              public void onClick(View v){
         Intent visorImagen = new Intent(contexto, VisorImagen.class);
         visorImagen.putExtra("IMG", datosImg[(Integer)v.getTag()]);
         contexto.startActivity(visorImagen);
        }

    });
    */
    return vista;
}

@Override
public int getCount() {
    return datosImg.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}
}

很长一段时间以来,我一直在寻找解决方案,但是我没有在网上找到任何东西。

3 个答案:

答案 0 :(得分:0)

您应该在此处给出位置,您将[0] [0]设置为evrytime会返回相同的eveytime值,请执行以下操作。

public View getView(int i, View convertView, ViewGroup parent) {

    final View vista = inflater.inflate(R.layout.elemento_lista, null);
    TextView titulo = (TextView) vista.findViewById(R.id.textView);
    ImageView imagen = (ImageView) vista.findViewById(R.id.imageView);

    titulo.setText(datos[i][i]);
    imagen.setImageResource(datosImg[i]);
    /*
    imagen.setTag(i);
    */
    /*
    imagen.setOnClickListener(new View.OnClickListener(){
      @Override
              public void onClick(View v){
         Intent visorImagen = new Intent(contexto, VisorImagen.class);
         visorImagen.putExtra("IMG", datosImg[(Integer)v.getTag()]);
         contexto.startActivity(visorImagen);
        }

    });
    */
    return vista;
}

答案 1 :(得分:0)

您可以将datos做成一个数组列表,例如:ArrayList<String>并用yourList.add()添加元素。然后在适配器中,用TextView将文本设置为titulo.setText(yourList.get(i)。 希望对您有帮助!

答案 2 :(得分:0)

我找到了解决方法!,

# Tarrifs
buy = 30.0 # buy price (c/kWh)
sell = 8.0 # sell price (c/kWh)
# formulation works only when sell <= buy

# Battery parameters
Emax = 0 # battery capacity (kWh)
eff = 0.85 # combined battery and inverter efficiency
pmax = (2/7)*Emax # battery max power (kW)
price = (3000.0 + 1000.0)/7 # battery, installation and BOS costs estm ($/kWh)
price = price*100/(15*365*24) # (c/kWh/hr)
# 15 years cycling once per day:
# http://reneweconomy.com.au/2015/tesla-says-battery-storage-already-makes-economic-sense-for-australia-consumers-40655
# We chose the continous power of 2KW for 7kWh model
# It can peak at 3.3kW but no details on for how long
# http://www.teslamotors.com/powerwall

# Battery variables
E = [LpVariable('E_'+str(i), 0, None) for i in range(T)] # battery energy (kWh)
pc = [LpVariable('pc_'+str(i), 0, None) for i in range(T)] # battery charge (kW)
pd = [LpVariable('pd_'+str(i), 0, None) for i in range(T)] # battery discharge (kW)

# Auxiliary variables
p = [LpVariable('p_'+str(i), None, None) for i in range(T)] # total house power (kW)
cpow = [LpVariable('cpow_'+str(i), None, None) for i in range(T)] # power cost (c)

# Optimisation problem
prb = LpProblem('Battery Operation')

# Objective
prb += lpSum(cpow) + price*tstep*T*Emax # sum of costs

# Constraints
for i in range(T):
    prb += p[i] == pc[i] - pd[i] + pload[i] - ppv[i] # total power
    prb += cpow[i] >= tstep*buy*p[i] # power cost constraint
    prb += cpow[i] >= tstep*sell*p[i] # power cost constraint
    prb += E[i] <= Emax # battery capacity
    prb += pc[i] <= pmax
    prb += pd[i] <= pmax

# Battery charge state constraints
# Batteries must start and finish half charged
prb += E[0] == 0.5*Emax + tstep*(eff*pc[0] - pd[0]) # starting energy
prb += E[T-1] == 0.5*Emax # finishing energy
for i in range(1, T):
    prb += E[i] == E[i-1] + tstep*(eff*pc[i] - pd[i]) # battery transitions

# Solve problem
prb.solve()

print('Status ' + str(LpStatus[prb.status]))
print('Cost ' + str(value(prb.objective)))
print('Emax ' + str(value(Emax)))

我写道:

    titulo.setText(datos[i][0]);
    imagen.setImageResource(datosImg[i]);

这意味着它仅显示一个文本,而另一个显示一个文本,然后转到下一个文本。感谢您的帮助!