Edition in Listview

Asked

Viewed 33 times

1

I created a separate xml to work better with Listview.. but I’m not able to relate his id to my onCreate

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textColor="@color/corList"
        tools:targetApi="ice_cream_sandwich"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft" />


</LinearLayout>

this xml is in /res/layout/resultado_modified.xml

Java:

package com.allsport.miyonic.allsport;

import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

        import Base.DbHelper;
        import Base.Esporte;

        import static android.os.FileObserver.DELETE;

public class ResultSimples extends AppCompatActivity {

    private ListView lista;
    private Button apagar;
    public TextView txt;

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

        lista = (ListView) findViewById(R.id.ListaTimes);
        apagar = (Button) findViewById(R.id.btndeletar);
        txt = (TextView) findViewById(R.id.text1); 
        apagar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbHelper dd = new DbHelper(ResultSimples.this);
                dd.delete(); // É necessário passar o parâmetro
            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();

        DbHelper dbhe = new DbHelper(this);
        List<Esporte> listaResultPartida = dbhe.selectTodosResult();

        ArrayAdapter<Esporte> adp = new ArrayAdapter<Esporte>(this, R.layout.resultado_modificado, listaResultPartida);

        lista.setAdapter(adp);
    }
}

Thank you...

  • Nathan, you would have to post the xml of the Adapter which is passing to Listview. There is no way to change the color of the item directly from ListView.

  • So @Wakim how it works... I tried to look on the internet and I couldn’t understand... it seems that the Adapter needs a TextView or something like.... how it works?

  • So Nathan, I recommend you take a look at this link: https://developer.android.com/guide/topics/ui/declaring-layout.html#Adapterviews. Has a very detailed documentation about the customization of ListView.

  • @Wakim, I need to create a separate xml with textview and then in java I call it as if it were in simple_list_item_1 that’s right?

  • That exact, as the simple_list_item_1 is from the framework, you cannot change the xml. There are two alternatives, or you create your own Adapter and change the color programmatically when the View is created, or creates a similar xml and changes the color in xml. I recommend the second approach, becomes clearer and less work.

  • I created an xml, and I put it in java in the onResume that line ArrayAdapter<Esporte> adp = new ArrayAdapter<Esporte>(this, R.layout.resultado_modificado, listaResultPartida); and is presenting me with this mistake in Logcat java.lang.Illegalstateexception: Arrayadapter requires the Resource ID to be a Textview only that in onCreate I am not managing to relate it, calling the ID - created a private on top (private TextView txt;) and inside onCreate txt = (TextView) findViewById(R.id.txt1); and it’s not going.

Show 2 more comments

1 answer

1

I managed with help from @Wakim, I needed to pass the id inside the Adpter builder

thus:

@Override
    public void onResume(){
        super.onResume();

        DbHelper dbhe = new DbHelper(this);
        List<Esporte> listaResultPartida = dbhe.selectTodosResult();
        ArrayAdapter<Esporte> adp = new ArrayAdapter<Esporte>(this, R.layout.resultado_modificado, android.R.id.text1, listaResultPartida);

        lista.setAdapter(adp);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.