姬長信(Redy)

java – 单击时创建一个带有可选行的ListVi…


问题

我正在尝试使用可选项创建ListView.我希望能够单击ListView中的项目并让项目在列表中更改颜色,然后继续使用行中的数据执行其他操作.

我正在使用SimpleAdapter.

如何制作它,以便当我点击一行时,它会变成另一种颜色,然后当我点击另一行时,新行被选中并更改为新颜色,旧行变回正常?

到目前为止,这是我的代码. DBTools类具有我希望在ListView中显示的所有数据,并且需要处理. getAllReceivers()方法返回包含所有数据的HashMap的ArrayList.

MainActivity.java

public class MainActivity extends ListActivity {
    DBTools dbTools = new DBTools(this);

    ArrayList> receiverList;

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

        receiverList = dbTools.getAllReceivers();
        dbTools.close();
        ListView listView = getListView();
        if(receiverList.size() != 0) {

            SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] {"receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath});
            setListAdapter(adapter);
        }
    }
}

activity_main.xml中:





    

        

    

        


receiver_entry.xml