适配器和列表活动

时间:2016-01-22 10:40:06

标签: android

我正在尝试为Android 5.0.1开发应用。我坚持使用适配器。我无法让布局显示出来。我对Listview不太实用。我抬头寻找解决方案,但我并不幸运。谢谢您的帮助。 这是我的代码:

适配器类:

package esp1415.xyz;

import java.util.List;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ListaFarmaciAdapter extends BaseAdapter {

    private Context contesto;
    private List<DettagliFarmaco> lfarm;

    public ListaFarmaciAdapter(Context context, List<DettagliFarmaco> list) {
        contesto = context;
        lfarm = list;
    }

    public void setFarmaci(List<DettagliFarmaco> list) {
        lfarm = list;
    }
    @Override
    public int getCount() {
        if (lfarm != null) {
            return lfarm.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (lfarm != null) {
            return lfarm.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        if (lfarm != null) {
            return lfarm.get(position).id;
        }
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) contesto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.grafico, parent, false);
        }
        DettagliFarmaco mod = (DettagliFarmaco) getItem(position);

        TextView txtNome = (TextView) view.findViewById(R.id.nome_farmaco);
        txtNome.setText(mod.nome);

        TextView txtDesc = (TextView) view.findViewById(R.id.desc_farmaco);
        txtDesc.setText(mod.descrizione);

        TextView txtQnt = (TextView) view.findViewById(R.id.qnt_farmaco);
        txtQnt.setText(mod.quantità);

        TextView txtInd = (TextView) view.findViewById(R.id.ind_farmaco);
        txtInd.setText(mod.indice_associato);

        TextView txtSveglia = (TextView) view.findViewById(R.id.sveglia);
        txtSveglia.setText(String.format("%02d : %02d", mod.ore, mod.minuti));

        modificaColoreTesto((TextView) view.findViewById(R.id.l), mod.getRipGiorno(DettagliFarmaco.LUNEDÌ));
        modificaColoreTesto((TextView) view.findViewById(R.id.ma), mod.getRipGiorno(DettagliFarmaco.MARTEDÌ));
        modificaColoreTesto((TextView) view.findViewById(R.id.me), mod.getRipGiorno(DettagliFarmaco.MERCOLEDÌ));
        modificaColoreTesto((TextView) view.findViewById(R.id.g), mod.getRipGiorno(DettagliFarmaco.GIOVEDÌ));
        modificaColoreTesto((TextView) view.findViewById(R.id.v), mod.getRipGiorno(DettagliFarmaco.VENERDÌ));
        modificaColoreTesto((TextView) view.findViewById(R.id.s), mod.getRipGiorno(DettagliFarmaco.SABATO));        
        modificaColoreTesto((TextView) view.findViewById(R.id.d), mod.getRipGiorno(DettagliFarmaco.DOMENICA));

        ToggleButton tog = (ToggleButton) view.findViewById(R.id.tog);
        tog.setChecked(mod.attiva);
        tog.setTag(Long.valueOf(mod.id));
        tog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ((ListaFarmaciActivity) contesto).setSvegliaAttiva(((Long) buttonView.getTag()).longValue(), isChecked);
        }
    });

            view.setTag(Long.valueOf(mod.id));
            view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            ((ListaFarmaciActivity) contesto).startSveglia(((Long) view.getTag()).longValue());
        }
    });

    view.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            ((ListaFarmaciActivity) contesto).deleteSveglia(((Long) view.getTag()).longValue());
            return true;
        }
    });

    return view;
}

private void modificaColoreTesto(TextView v, boolean att) {
    if (att) {
        v.setTextColor(Color.CYAN);
    } else {
        v.setTextColor(Color.LTGRAY);
    }
}
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="@drawable/bg10" />

      <TextView
            android:id="@+id/nome_farmaco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="@string/nome"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/desc_farmaco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nome_farmaco"
            android:text="@string/desc"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/qnt_farmaco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/desc_farmaco"
            android:textColor="#FAEBD7"
            android:text="@string/qnt"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/ind_farmaco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/qnt_farmaco"
            android:textColor="#FAEBD7"
            android:text="@string/ind"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/sveglia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ind_farmaco"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="8dp"
            android:textColor="#FAEBD7"
            android:text="@string/tem"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/l"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sveglia"
            android:text="@string/l"
            android:layout_marginLeft="10dp"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/ma"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/l"
            android:layout_below="@+id/sveglia"
            android:layout_marginLeft="4dp"
            android:text="@string/m"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" /> 

        <TextView
            android:id="@+id/me"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/ma"
            android:layout_below="@+id/sveglia"
            android:layout_marginLeft="4dp"
            android:text="@string/m"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" /> 

        <TextView
            android:id="@+id/g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/me"
            android:layout_below="@+id/sveglia"
            android:layout_marginLeft="4dp"
            android:text="@string/g"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" /> 

        <TextView
            android:id="@+id/v"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/g"
            android:layout_below="@+id/sveglia"
            android:layout_marginLeft="4dp"
            android:text="@string/v"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" /> 

        <TextView
            android:id="@+id/s"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/v"
            android:layout_below="@+id/sveglia"
            android:layout_marginLeft="4dp"
            android:text="@string/s"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" /> 

        <TextView
            android:id="@+id/d"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/s"
            android:layout_below="@+id/sveglia"
            android:layout_marginLeft="4dp"
            android:text="@string/d"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/data_ins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/l"
            android:text="@string/data"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/ora_ins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/data_ins"
            android:text="@string/ora"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/data_ins"
            android:layout_below="@+id/g"
            android:layout_marginLeft="4dp"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/ora"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/ora_ins"
            android:layout_below="@+id/data"
            android:layout_marginLeft="4dp"
            android:textColor="#FAEBD7"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ToggleButton
            android:id="@+id/tog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/sveglia" />

</ListView>

以及活动是否有帮助:

package esp1415.xyz;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class ListaFarmaciActivity extends ListActivity {

    private ListaFarmaciAdapter adap;
    private DBHelper dbHelper = new DBHelper(this);
    private Context contesto;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    contesto = this;    
    setContentView(R.layout.grafico);

    adap = new ListaFarmaciAdapter(this, dbHelper.getFarmaci());

    setListAdapter(adap);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.listafarmaci, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.agg: {
            startSveglia(-1);
            break;
        }
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        adap.setFarmaci(dbHelper.getFarmaci());
        adap.notifyDataSetChanged();
    }
}

public void setSvegliaAttiva(long id, boolean att) {
    ManagerSveglia.deleteSveglia(this);

    DettagliFarmaco mod = dbHelper.getFarmaco(id);
    mod.attiva = att;
    dbHelper.updateFarmaco(mod);

    ManagerSveglia.setSveglia(this);
}

public void startSveglia(long id) {
    Intent intent = new Intent(this, Sveglia.class);
    intent.putExtra("id", id);
    startActivityForResult(intent, 0);
}

public void deleteSveglia(long id) {
    final long SvegliaId = id;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Sei Sicuro?")
    .setTitle("Vuoi eliminare il farmaco selezionato?")
    .setCancelable(true)
    .setNegativeButton("No", null)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ManagerSveglia.deleteSveglia(contesto);
            dbHelper.deleteFarmaco(SvegliaId);
            adap.setFarmaci(dbHelper.getFarmaci());
            adap.notifyDataSetChanged();
            ManagerSveglia.setSveglia(contesto);
        }
    }).show();
}

}

最后我的LOGCAT:

01-22 10:40:49.961: E/AndroidRuntime(30751): FATAL EXCEPTION: main
01-22 10:40:49.961: E/AndroidRuntime(30751): Process: esp1415.xyz, PID: 30751
01-22 10:40:49.961: E/AndroidRuntime(30751): java.lang.RuntimeException: Unable to start activity ComponentInfo{esp1415.xyz/esp1415.xyz.ListaFarmaciActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3149)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.os.Looper.loop(Looper.java:145)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.ActivityThread.main(ActivityThread.java:6872)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at java.lang.reflect.Method.invoke(Native Method)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at java.lang.reflect.Method.invoke(Method.java:372)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
01-22 10:40:49.961: E/AndroidRuntime(30751): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.widget.AdapterView.addView(AdapterView.java:711)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:822)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.Activity.setContentView(Activity.java:2358)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at esp1415.xyz.ListaFarmaciActivity.onCreate(ListaFarmaciActivity.java:22)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.Activity.performCreate(Activity.java:6550)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
01-22 10:40:49.961: E/AndroidRuntime(30751):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
01-22 10:40:49.961: E/AndroidRuntime(30751):    ... 10 more

1 个答案:

答案 0 :(得分:1)

  

01-22 10:40:49.961:E / AndroidRuntime(30751):引起:   java.lang.UnsupportedOperationException:addView(View,LayoutParams)   AdapterView

不支持

<强>唐&#39;吨

view = inflater.inflate(R.layout.grafico, parent, false);

<强>不要

view = inflater.from(context).inflate(R.layout.grafico, null);

你应该试试这个

    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = null;
    if(view == null)
    {

    view = inflater.from(context).inflate(R.layout.community_common_list_layout, null);
    }