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 fromListView
.– Wakim
So @Wakim how it works... I tried to look on the internet and I couldn’t understand... it seems that the
Adapter
needs aTextView
or something like.... how it works?– Nathan
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
@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?– Nathan
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 theView
is created, or creates a similar xml and changes the color in xml. I recommend the second approach, becomes clearer and less work.– Wakim
I created an xml, and I put it in java in the
onResume
that lineArrayAdapter<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 theID
- created a private on top(private TextView txt;)
and inside onCreatetxt = (TextView) findViewById(R.id.txt1);
and it’s not going.– Nathan
Let’s go continue this discussion in chat.
– Nathan