ListView 以编程方式突出显示单行或多行
Posted
技术标签:
【中文标题】ListView 以编程方式突出显示单行或多行【英文标题】:ListView highlight a single or multiple rows programmatically 【发布时间】:2020-11-04 15:22:12 【问题描述】:这是我的代码,它使用从 firebase 存储的数据填充 ListView。我希望能够根据我之前选择的项目(存储在 firebase 中)以编程方式突出显示项目列表中的一个项目或几个项目。感谢任何帮助/指针。
我已将choiceMode 设置为multipleChoice。
谢谢。
<ListView
android:id="@+id/listv"
android:layout_
android:layout_
android:layout_below="@id/holidayChecklist"
android:layout_marginTop="100dp"
android:divider="@color/midGreen"
android:dividerHeight="2dp"
android:padding="15dp"
android:choiceMode="multipleChoice"/>
public class ChecklistActivity2 extends AppCompatActivity
ImageButton back;
ListView checklist;
ArrayAdapter<String> displayChecklist;
private Firebase mRef;
ArrayList<String> items = new ArrayList<>();
ArrayList<Boolean> values = new ArrayList<>();
ArrayList<Boolean> valuesog = new ArrayList<>();
TextView text;
public int SIZE_OF_CHECKLIST = 16;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist);
Firebase.setAndroidContext(this);
mRef = new Firebase("https://vacay.firebaseio.com/");
back = findViewById(R.id.backButton);
checklist = findViewById(R.id.listv);
text = findViewById(R.id.txtitem);
back.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(ChecklistActivity2.this, HomeScreenActivity.class);
startActivity(intent);
);
getChecklist();
checklist.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
String item = (String) checklist.getItemAtPosition(position);
System.out.println("HERE " + item + " position: " + position);
if (values.get(items.indexOf(item)))
values.set(items.indexOf(item), false);
view.setSelected(false);
System.out.println(item+" NOW FALSE");
else
values.set(items.indexOf(item), true);
view.setSelected(true);
System.out.println(item+ " NOW TRUE");
);
@Override
protected void onPause()
super.onPause();
setChecklist();
items.clear();
values.clear();
valuesog.clear();
@Override
protected void onStop()
super.onStop();
setChecklist();
items.clear();
values.clear();
valuesog.clear();
public void getChecklist()
System.out.println("RUNNING GETCHECKLIST, SIZE " + items.size());
Firebase mRefChild = mRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("holiday_checklist");
mRefChild.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
System.out.println("data changed!");
if (dataSnapshot.getValue() != null)
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
for (Map.Entry<String, Object> entry : map.entrySet())
//Get user map
Boolean value = (Boolean) entry.getValue();
String item = entry.getKey();
items.add(item);
valuesog.add(value);
values.addAll(valuesog);
displayChecklist = new ArrayAdapter<String>(
ChecklistActivity2.this,
R.layout.list_item,
items
);
checklist.setAdapter(displayChecklist);
for (int i = 0; i < SIZE_OF_CHECKLIST; i++)
if (values.get(i))
System.out.println("owo " + checklist.getItemAtPosition(i));
// displayChecklist.getView(i, null, checklist).setBackgroundResource(R.drawable.selected_item);
// TextView t = (TextView) displayChecklist.getView(3, text, checklist);
// t.setBackgroundColor(Color.BLUE);
// System.out.println(displayChecklist.getItemViewType(0));
@Override
public void onCancelled(FirebaseError firebaseError)
System.out.println("firebase canceled oops");
);
public void setChecklist()
System.out.println("SETTING CHECKLIST, SIZE " + values.size());
if (values.size() > 0)
for (int i = 0; i < SIZE_OF_CHECKLIST; i++)
Firebase mRefChild = mRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("holiday_checklist")
.child(items.get(i));
if (values.get(i) != valuesog.get(i))
System.out.println(items.get(i) + " CHANGED");
if (valuesog.get(i))
System.out.println(items.get(i) + " SET TO FALSE");
mRefChild.setValue(false);
else
System.out.println(items.get(i) + " SET TO TRUE");
mRefChild.setValue(true);
System.out.println("FINISHED SETTING, SIZE " + values.size());
下面是当用户点击项目时将背景变为绿色的选择器。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<item
android:drawable="@color/midGreen"
android:state_activated="true"/>
</selector>
【问题讨论】:
【参考方案1】:你必须先创建数据模型并添加isSelected
Boolean
状态变量。示例:
class YourDataModel
String data;
String whatever;
Boolean isSelected;
【讨论】:
以上是关于ListView 以编程方式突出显示单行或多行的主要内容,如果未能解决你的问题,请参考以下文章