Мое приложение представляет собой простой список, в котором на главном экране есть listview
нескольких элементов, полученных из базы данных через simplecursoradapter
. Я пробовал так много ссылок, но не получаю желаемого результата. Я упомянул обо всех ответах и почему они не работают на меня после всех приведенных ниже кодов.Установите цвет в ListView Android с SimpleCursorAdapter
Коды
Вот activity_main.xml
файл:
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_centerHorizontal="true"
tools:context=".MainActivity">
<TextView android:text="@string/welcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:id="@+id/topic"
android:textColor="#ff2e7d13"
android:textColorHighlight="#fff4de10"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/newList"
android:layout_below="@+id/topic"
android:choiceMode="multipleChoiceModal"
android:listSelector="@android:color/holo_orange_light"
android:focusableInTouchMode="true"
android:focusable="true"
></ListView>
</RelativeLayout>
И ниже tasks.xml
файл, который управляет отдельные элементы списка:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/task_row"
android:background="@drawable/selector"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c1"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:textSize="26sp"
android:textColor="#ff663ccc"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/c2"
android:layout_below="@+id/c1"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:textColor="#ff4a9fcc"
/>
</RelativeLayout>
я хочу программно изменить цвет отдельных элементов списка на основе некоторых условий. Например, у меня есть дополнительный столбец, который может принимать 3 значения: Низкий, Высокий, Обычный - Я хочу, чтобы элементы с максимумом были красными, Low - зеленым и так далее.
Ниже MainActivity.java
файл:
public class MainActivity extends ActionBarActivity {
DbHelper db;
ListView myList;
RelativeLayout rL;
int checkedCount;
int totalCount;
long arr[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DbHelper(this);
arr = new long[1000];
checkedCount=0; totalCount = 0;
myList = (ListView) findViewById(R.id.newList);
loadData();
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
myList.setItemsCanFocus(true);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
int nameId = listCursor.getInt(listCursor
.getColumnIndex(db._ID));
// Toast.makeText(getApplicationContext(),
//String a = Integer.toString(nameId);
passdata.putInt("keyid", nameId);
Intent passIntent = new Intent(MainActivity.this,
EditActivity.class);
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
/* if (mActionMode != null) {
return false;
}
*/
// Start the CAB using the ActionMode.Callback defined above
// mActionMode = getActivity().startActionMode(mActionMode.Callback);
view.setSelected(true);
return true;
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return false;
}
});
myList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
// Log.d("no.: "+myList.getCheckedItemPosition()+" hi","list");
rL = (RelativeLayout) findViewById(R.id.task_row);
if(checked)
{
//db.delData(id);
checkedCount++; totalCount++;
arr[checkedCount]=id;
// Log.d(" yo "," pos=" + position);
}
else {
for(int i=1;i<=checkedCount;i++)
{
if(arr[i]==id)
arr[i]=0;
}
// checkedCount--;
totalCount--;
}
mode.setTitle(totalCount+" selected");
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
// deleteSelectedItems();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Delete");
// Setting Dialog Message
alertDialog.setMessage("Delete "+checkedCount+" items?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.abc_list_selector_holo_dark);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "Successfully deleted "+checkedCount+" items.", Toast.LENGTH_SHORT).show();
for (int i = 1; i <= checkedCount; ++i) {
db.delData(arr[i]);
// Log.d("TAG", arr[i] + " got here");
loadData();
}
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "Cancelled Delete.", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
menu.clear();
checkedCount=0; totalCount=0;
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_main, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
loadData();
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
}
@Override
public void onResume()
{
super.onResume();
loadData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_add) {
addNew();
return true;
}
return super.onOptionsItemSelected(item);
}
public void loadData()
{
Cursor cursor = null;
cursor = db.fetchData();
final int[] colors = new int[] { 0xAA5F82A6 , 0xAA1A4C80 };
ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
cursor,
new String[]{db.COLUMN_1, db.COLUMN_2},
new int[]{R.id.c1, R.id.c2}, 0){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) findViewById(R.id.c1);
//text.setBackgroundColor(0xAA1A4C80);
int colorPos = position % colors.length;
try {
text.setTextColor(colors[colorPos]);
}
catch(Exception e){
e.printStackTrace();
}
return view;
}
};
myList.setAdapter(myAdapter);
}
public void addNew()
{
Intent intent = new Intent(this,AddActivity.class);
startActivity(intent);
}
}
Вещи, которые я пытался
Как видно из кода, я попробовал этот link. Это не дает мне никакой ошибки, но окрашивает только первый элемент. Если я удалю блок
try-catch
, он дастNullPointerException
.Если я использую
Binder
путь, от этого answer (1 го кода) или это link, я получаю две ошибки - ожидаемый;
после последнего}
, и не может решить методSetViewBinder(...)
.Большинство ответов других ссылок предлагает использовать пользовательский адаптер, но я должен использовать
SimpleCursorAdapter
только потому, что другие части кода структурированы таким образом (используя курсоры для извлечения данных из базы данных sqlite и т. Д.).
Примечание
Я новичок, поэтому, пожалуйста, руководство меня о том, как правильно цвета элементов. В частности, я хочу что-то вроде этого:
if(db.Column_Priority=="High")
listItem.setColor("Red");
else if(db.Column_Priority=="Low")
listItem.setColor("Green");
else
listItem.setColor("Blue");
Нет ли простого способа достичь этого, кроме определения дополнительных классов?