我的应用是一个简单的列表,其中主屏幕通过listview
从数据库中获取了几个项目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个值:低,高,正常 - 我希望高的项目为红色,低的项目为绿色等等。
以下是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 st 代码)或此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");
除了定义额外的类之外,有没有简单的方法来实现这个目标?
答案 0 :(得分:1)
好吧,在tasks.xml中,你有listView中每个项目的自定义视图,所以你可以以编程方式更改RelativeLayout颜色 是的,您需要使用SimpleCursorAdapter,这样的类:
public class ItemAdapter extends ArrayAdapter<ItemList>
{
public ItemAdapter(Context context, List<ItemList> objects)
{
super(context, 0, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ItemList currentItem = getItem(position);
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
convertView = inflater.inflate(R.layout.tasks, parent, false);
}
//Instanciate text and color
TextView name = (TextView)convertView.findViewById(R.id.text1);
RelativeLayout rl = (RelativeLayout)convertView.findViewById(R.id.container);
//Asignar valores
name.setText(currentItem.getName());
rl.setImageResource(currentItem.getCategoria(setBackgroundColor(getResources().getColor(currentItem.getColor()))));
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getView(position,convertView,parent);
}
}
您需要一个适配器对象(ItemList.java):
public class ItemList
{
private String name;
private int color;
public ItemList(String nm, int c)
{
this.name = nm;
this.color = c;
}
public void setName(String n)
{ this.name = n; }
public void setColor(int c){
this.color=c;
}
public String getName(){return name;}
public int getColor(){return color;}
@Override
public String toString(){return name;}
}
以及您创建的活动:
public class MyActivity extends Activity implements AdapterView.OnItemClickListener
{
ArrayAdapter adapter;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tramites);
list = (ListView)findViewById(R.id.listviewfromlayout);
List items = new ArrayList<ItemList>();
// add your items
items.add(new ItemList("item 1",Color.RED));
items.add(new ItemList("item 2",Color.BLUE));
items.add(new ItemList("item 3",Color.GREEN));
//Initialize the adapter with data
adaptador = new ItemListaTramitesAdapter(this,items);
//linked list to adapter
lista.setAdapter(adaptador);
//set listener
lista.setOnItemClickListener((AdapterView.OnItemClickListener) this);
}
// listener
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ItemList task = (ItemList)adapter.getItem(position);
Toast.makeText(this, "selected: "+task.getName(), Toast.LENGTH_LONG).show();
}
}